Skip to content

Commit 627630e

Browse files
committed
Rework to combined dev_zones dict
1 parent 31f9254 commit 627630e

File tree

7 files changed

+93
-101
lines changed

7 files changed

+93
-101
lines changed

plugwise/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ async def full_update_device(self) -> None:
302302
"""Helper-function used for testing."""
303303
await self._smile_api.full_update_device()
304304

305-
def get_all_devices(self) -> None:
305+
def get_all_device_zones(self) -> None:
306306
"""Helper-function used for testing."""
307307
self._smile_api.get_all_devices()
308308

309309
async def async_update(self) -> PlugwiseData:
310310
"""Perform an incremental update for updating the various device states."""
311-
data = PlugwiseData(devices={}, gateway={}, zones={})
311+
data = PlugwiseData(device_zones={}, gateway={})
312312
try:
313313
data = await self._smile_api.async_update()
314314
self.gateway_id = data.gateway["gateway_id"]

plugwise/common.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self) -> None:
4040
self._heater_id: str
4141
self._on_off_device: bool
4242
self._opentherm_device: bool
43-
self.gw_devices: dict[str, DeviceZoneData]
43+
self.gw_device_zones: dict[str, DeviceZoneData]
4444
self.smile_name: str
4545
self.smile_type: str
4646

@@ -186,9 +186,9 @@ def _power_data_energy_diff(
186186

187187
return direct_data
188188

189-
def _create_gw_devices(self, appl: Munch) -> None:
190-
"""Helper-function for creating/updating gw_devices."""
191-
self.gw_devices[appl.dev_id] = {"dev_class": appl.pwclass}
189+
def _create_gw_device_zones(self, appl: Munch) -> None:
190+
"""Helper-function for creating/updating gw_device_zones."""
191+
self.gw_device_zones[appl.dev_id] = {"dev_class": appl.pwclass}
192192
self._count += 1
193193
for key, value in {
194194
"available": appl.available,
@@ -204,26 +204,26 @@ def _create_gw_devices(self, appl: Munch) -> None:
204204
}.items():
205205
if value is not None or key == "location":
206206
appl_key = cast(ApplianceType, key)
207-
self.gw_devices[appl.dev_id][appl_key] = value
207+
self.gw_device_zones[appl.dev_id][appl_key] = value
208208
self._count += 1
209209

210210
def _device_data_switching_group(
211211
self, device: DeviceZoneData, data: DeviceZoneData
212212
) -> None:
213-
"""Helper-function for _get_device_data().
213+
"""Helper-function for _get_device_zone_data().
214214
215215
Determine switching group device data.
216216
"""
217217
if device["dev_class"] in SWITCH_GROUP_TYPES:
218218
counter = 0
219219
for member in device["members"]:
220-
if self.gw_devices[member]["switches"].get("relay"):
220+
if self.gw_device_zones[member]["switches"].get("relay"):
221221
counter += 1
222222
data["switches"]["relay"] = counter != 0
223223
self._count += 1
224224

225225
def _get_group_switches(self) -> dict[str, DeviceZoneData]:
226-
"""Helper-function for smile.py: get_all_devices().
226+
"""Helper-function for smile.py: get_all_device_zones().
227227
228228
Collect switching- or pump-group info.
229229
"""
@@ -240,7 +240,7 @@ def _get_group_switches(self) -> dict[str, DeviceZoneData]:
240240
group_appliances = group.findall("appliances/appliance")
241241
for item in group_appliances:
242242
# Check if members are not orphaned - stretch
243-
if item.attrib["id"] in self.gw_devices:
243+
if item.attrib["id"] in self.gw_device_zones:
244244
members.append(item.attrib["id"])
245245

246246
if group_type in SWITCH_GROUP_TYPES and members:

plugwise/constants.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,5 +583,4 @@ class PlugwiseData:
583583
"""Plugwise data provided as output."""
584584

585585
gateway: GatewayData
586-
devices: dict[str, DeviceZoneData]
587-
zones: dict[str, DeviceZoneData]
586+
device_zones: dict[str, DeviceZoneData]

plugwise/data.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ def __init__(self) -> None:
2828
SmileHelper.__init__(self)
2929

3030

31-
def _all_device_data(self) -> None:
32-
"""Helper-function for get_all_devices().
31+
def _all_device_zone_data(self) -> None:
32+
"""Helper-function for get_all_device_zones().
3333
34-
Collect data for each device and add to self.gw_data and self.gw_devices.
34+
Collect data for each device/zone and add to self.gw_data and self.gw_device_zones.
3535
"""
36-
self._update_gw_devices()
36+
self._update_gw_device_zones()
3737
if self.smile(ADAM):
3838
self._update_zones()
39+
self.gw_device_zones.update(self.zone_data)
40+
3941
self.gw_data.update(
4042
{
4143
"gateway_id": self.gateway_id,
@@ -51,39 +53,39 @@ def _all_device_data(self) -> None:
5153
)
5254

5355
def _update_zones(self) -> None:
54-
"""Helper-function for _all_device_data() and async_update().
56+
"""Helper-function for _all_device_zone_data() and async_update().
5557
5658
Collect data for each zone/location and add to self.zone_data.
5759
"""
5860
for location_id, zone in self.zone_data.items():
5961
data = self._get_location_data(location_id)
6062
zone.update(data)
6163

62-
def _update_gw_devices(self) -> None:
63-
"""Helper-function for _all_device_data() and async_update().
64+
def _update_gw_device_zones(self) -> None:
65+
"""Helper-function for _all_device_zone_data() and async_update().
6466
65-
Collect data for each device and add to self.gw_devices.
67+
Collect data for each device and add to self.gw_device_zones.
6668
"""
6769
mac_list: list[str] = []
68-
for device_id, device in self.gw_devices.items():
69-
data = self._get_device_data(device_id)
70-
if device_id == self.gateway_id:
70+
for devzone_id, devzone in self.gw_device_zones.items():
71+
data = self._get_device_zone_data(devzone_id)
72+
if devzone_id == self.gateway_id:
7173
mac_list = self._detect_low_batteries()
72-
self._add_or_update_notifications(device_id, device, data)
74+
self._add_or_update_notifications(devzone_id, devzone, data)
7375

74-
device.update(data)
76+
devzone.update(data)
7577
is_battery_low = (
7678
mac_list
77-
and "low_battery" in device["binary_sensors"]
78-
and device["zigbee_mac_address"] in mac_list
79-
and device["dev_class"] in ("thermo_sensor", "thermostatic_radiator_valve", "zone_thermometer", "zone_thermostat")
79+
and "low_battery" in devzone["binary_sensors"]
80+
and devzone["zigbee_mac_address"] in mac_list
81+
and devzone["dev_class"] in ("thermo_sensor", "thermostatic_radiator_valve", "zone_thermometer", "zone_thermostat")
8082
)
8183
if is_battery_low:
82-
device["binary_sensors"]["low_battery"] = True
84+
devzone["binary_sensors"]["low_battery"] = True
8385

84-
self._update_for_cooling(device)
86+
self._update_for_cooling(devzone)
8587

86-
remove_empty_platform_dicts(device)
88+
remove_empty_platform_dicts(devzone)
8789

8890
def _detect_low_batteries(self) -> list[str]:
8991
"""Helper-function updating the low-battery binary_sensor status from a Battery-is-low message."""
@@ -107,31 +109,31 @@ def _detect_low_batteries(self) -> list[str]:
107109
return mac_address_list
108110

109111
def _add_or_update_notifications(
110-
self, device_id: str, device: DeviceZoneData, data: DeviceZoneData
112+
self, devzone_id: str, devzone: DeviceZoneData, data: DeviceZoneData
111113
) -> None:
112114
"""Helper-function adding or updating the Plugwise notifications."""
113115
if (
114-
device_id == self.gateway_id
116+
devzone_id == self.gateway_id
115117
and (
116118
self._is_thermostat or self.smile_type == "power"
117119
)
118120
) or (
119-
"binary_sensors" in device
120-
and "plugwise_notification" in device["binary_sensors"]
121+
"binary_sensors" in devzone
122+
and "plugwise_notification" in devzone["binary_sensors"]
121123
):
122124
data["binary_sensors"]["plugwise_notification"] = bool(self._notifications)
123125
self._count += 1
124126

125-
def _update_for_cooling(self, device: DeviceZoneData) -> None:
127+
def _update_for_cooling(self, devzone: DeviceZoneData) -> None:
126128
"""Helper-function for adding/updating various cooling-related values."""
127129
# For Anna and heating + cooling, replace setpoint with setpoint_high/_low
128130
if (
129131
self.smile(ANNA)
130132
and self._cooling_present
131-
and device["dev_class"] == "thermostat"
133+
and devzone["dev_class"] == "thermostat"
132134
):
133-
thermostat = device["thermostat"]
134-
sensors = device["sensors"]
135+
thermostat = devzone["thermostat"]
136+
sensors = devzone["sensors"]
135137
temp_dict: ActuatorData = {
136138
"setpoint_low": thermostat["setpoint"],
137139
"setpoint_high": MAX_SETPOINT,
@@ -143,7 +145,7 @@ def _update_for_cooling(self, device: DeviceZoneData) -> None:
143145
}
144146
thermostat.pop("setpoint")
145147
temp_dict.update(thermostat)
146-
device["thermostat"] = temp_dict
148+
devzone["thermostat"] = temp_dict
147149
if "setpoint" in sensors:
148150
sensors.pop("setpoint")
149151
sensors["setpoint_low"] = temp_dict["setpoint_low"]
@@ -152,7 +154,7 @@ def _update_for_cooling(self, device: DeviceZoneData) -> None:
152154

153155

154156
def _get_location_data(self, loc_id: str) -> DeviceZoneData:
155-
"""Helper-function for _all_device_data() and async_update().
157+
"""Helper-function for _all_device_zone_data() and async_update().
156158
157159
Provide device-data, based on Location ID (= loc_id).
158160
"""
@@ -163,16 +165,16 @@ def _get_location_data(self, loc_id: str) -> DeviceZoneData:
163165
self._count += 1
164166

165167
# Thermostat data (presets, temperatures etc)
166-
self._device_data_climate(loc_id, zone, data)
168+
self._devzone_data_climate(loc_id, zone, data)
167169

168170
return data
169171

170-
def _get_device_data(self, dev_id: str) -> DeviceZoneData:
172+
def _get_device_zone_data(self, dev_id: str) -> DeviceZoneData:
171173
"""Helper-function for _update_gw_devices() and async_update().
172174
173-
Provide device-data, based on appliance_id ()= dev_id).
175+
Provide device-data, based on appliance_id (= dev_id).
174176
"""
175-
device = self.gw_devices[dev_id]
177+
device = self.gw_device_zones[dev_id]
176178
data = self._get_measurement_data(dev_id)
177179

178180
# Check availability of wired-connected devices
@@ -193,14 +195,14 @@ def _get_device_data(self, dev_id: str) -> DeviceZoneData:
193195

194196
# Thermostat data for Anna (presets, temperatures etc)
195197
if self.smile(ANNA) and device["dev_class"] == "thermostat":
196-
self._device_data_climate(dev_id, device, data)
198+
self._devzone_data_climate(dev_id, device, data)
197199

198200
return data
199201

200202
def _check_availability(
201203
self, device: DeviceZoneData, dev_class: str, data: DeviceZoneData, message: str
202204
) -> None:
203-
"""Helper-function for _get_device_data().
205+
"""Helper-function for _get_device_zone_data().
204206
205207
Provide availability status for the wired-commected devices.
206208
"""
@@ -213,7 +215,7 @@ def _check_availability(
213215
data["available"] = False
214216

215217
def _device_data_adam(self, device: DeviceZoneData, data: DeviceZoneData) -> None:
216-
"""Helper-function for _get_device_data().
218+
"""Helper-function for _get_device_zone_data().
217219
218220
Determine Adam heating-status for on-off heating via valves,
219221
available regulations_modes and thermostat control_states.
@@ -237,19 +239,19 @@ def _device_data_adam(self, device: DeviceZoneData, data: DeviceZoneData) -> Non
237239
self._count += 1
238240

239241

240-
def _device_data_climate(
242+
def _devzone_data_climate(
241243
self,
242244
location_id: str,
243-
device: DeviceZoneData,
245+
devzone: DeviceZoneData,
244246
data: DeviceZoneData
245247
) -> None:
246-
"""Helper-function for _get_device_data().
248+
"""Helper-function for _get_device_zone_data().
247249
248250
Determine climate-control device data.
249251
"""
250252
loc_id = location_id
251-
if device.get("location") is not None:
252-
loc_id = device["location"]
253+
if devzone.get("location") is not None:
254+
loc_id = devzone["location"]
253255

254256
# Presets
255257
data["preset_modes"] = None
@@ -284,7 +286,7 @@ def _device_data_climate(
284286

285287
def check_reg_mode(self, mode: str) -> bool:
286288
"""Helper-function for device_data_climate()."""
287-
gateway = self.gw_devices[self.gateway_id]
289+
gateway = self.gw_device_zones[self.gateway_id]
288290
return (
289291
"regulation_modes" in gateway and gateway["select_regulation_mode"] == mode
290292
)

0 commit comments

Comments
 (0)