Skip to content

Commit 20b7108

Browse files
authored
Merge pull request #837 from plugwise/rework-data
Optimize data collection
2 parents 5f4371c + 8487ea7 commit 20b7108

File tree

6 files changed

+96
-106
lines changed

6 files changed

+96
-106
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Ongoing
4+
5+
- Code optimizations via PR [#837](https://github.com/plugwise/python-plugwise/pull/837)
6+
37
## v1.11.0
48

59
- Extend feature: support pumping group, add group sensors

plugwise/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _reorder_devices(self) -> None:
175175
break
176176
self.gw_entities = {**reordered, **self.gw_entities}
177177

178-
def _entity_switching_group(self, entity: GwEntityData, data: GwEntityData) -> None:
178+
def _entity_switching_group(self, entity: GwEntityData) -> None:
179179
"""Helper-function for _get_device_zone_data().
180180
181181
Determine switching group device data.
@@ -185,7 +185,7 @@ def _entity_switching_group(self, entity: GwEntityData, data: GwEntityData) -> N
185185
for member in entity["members"]:
186186
if self.gw_entities[member]["switches"].get("relay"):
187187
counter += 1
188-
data["switches"]["relay"] = counter != 0
188+
entity["switches"]["relay"] = counter != 0
189189
self._count += 1
190190

191191
def _get_groups(self) -> dict[str, GwEntityData]:

plugwise/data.py

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def _update_zones(self) -> None:
4545
Collect data for each zone/location and add to self._zones.
4646
"""
4747
for location_id, zone in self._zones.items():
48-
data = self._get_location_data(location_id)
49-
zone.update(data)
48+
self._get_location_data(location_id, zone)
5049

5150
def _update_gw_entities(self) -> None:
5251
"""Helper-function for _all_entities_data() and async_update().
@@ -55,12 +54,11 @@ def _update_gw_entities(self) -> None:
5554
"""
5655
mac_list: list[str] = []
5756
for entity_id, entity in self.gw_entities.items():
58-
data = self._get_entity_data(entity_id)
57+
self._get_entity_data(entity_id, entity)
5958
if entity_id == self._gateway_id:
6059
mac_list = self._detect_low_batteries()
61-
self._add_or_update_notifications(entity_id, entity, data)
60+
self._add_or_update_notifications(entity_id, entity)
6261

63-
entity.update(data)
6462
is_battery_low = (
6563
mac_list
6664
and "low_battery" in entity["binary_sensors"]
@@ -106,7 +104,7 @@ def _detect_low_batteries(self) -> list[str]:
106104
return mac_address_list
107105

108106
def _add_or_update_notifications(
109-
self, entity_id: str, entity: GwEntityData, data: GwEntityData
107+
self, entity_id: str, entity: GwEntityData
110108
) -> None:
111109
"""Helper-function adding or updating the Plugwise notifications."""
112110
if (
@@ -116,8 +114,10 @@ def _add_or_update_notifications(
116114
"binary_sensors" in entity
117115
and "plugwise_notification" in entity["binary_sensors"]
118116
):
119-
data["binary_sensors"]["plugwise_notification"] = bool(self._notifications)
120-
data["notifications"] = self._notifications
117+
entity["binary_sensors"]["plugwise_notification"] = bool(
118+
self._notifications
119+
)
120+
entity["notifications"] = self._notifications
121121
self._count += 2
122122

123123
def _update_for_cooling(self, entity: GwEntityData) -> None:
@@ -154,82 +154,76 @@ def _update_for_cooling(self, entity: GwEntityData) -> None:
154154
3 # add 4 total, remove 1, count the conditional remove separately
155155
)
156156

157-
def _get_location_data(self, loc_id: str) -> GwEntityData:
157+
def _get_location_data(self, loc_id: str, zone: GwEntityData) -> None:
158158
"""Helper-function for _all_entity_data() and async_update().
159159
160160
Provide entity-data, based on Location ID (= loc_id).
161161
"""
162-
zone = self._zones[loc_id]
163-
data = self._get_zone_data(loc_id)
164-
self._regulation_control(data)
162+
self._get_zone_data(loc_id, zone)
163+
self._regulation_control(zone)
165164

166-
data["control_state"] = "idle"
165+
zone["control_state"] = "idle"
167166
self._count += 1
168-
if (ctrl_state := self._control_state(data)) and ctrl_state in (
167+
if (ctrl_state := self._control_state(zone)) and ctrl_state in (
169168
"cooling",
170169
"heating",
171170
"preheating",
172171
):
173-
data["control_state"] = str(ctrl_state)
172+
zone["control_state"] = str(ctrl_state)
174173

175-
if "setpoint" in data["sensors"]:
176-
data["sensors"].pop("setpoint") # remove, only used in _control_state()
174+
if "setpoint" in zone["sensors"]:
175+
zone["sensors"].pop("setpoint") # remove, only used in _control_state()
177176
self._count -= 1
178177

179178
# Thermostat data (presets, temperatures etc)
180-
self._climate_data(loc_id, zone, data)
181-
182-
return data
179+
self._climate_data(loc_id, zone)
183180

184-
def _get_entity_data(self, entity_id: str) -> GwEntityData:
181+
def _get_entity_data(self, entity_id: str, entity: GwEntityData) -> None:
185182
"""Helper-function for _update_gw_entities() and async_update().
186183
187184
Provide entity-data, based on appliance_id (= entity_id).
188185
"""
189-
entity = self.gw_entities[entity_id]
190-
data = self._get_measurement_data(entity_id)
186+
self._get_measurement_data(entity_id, entity)
191187

192188
# Check availability of wired-connected entities
193189
# Smartmeter
194190
self._check_availability(
195-
entity, "smartmeter", data, "P1 does not seem to be connected"
191+
entity, "smartmeter", "P1 does not seem to be connected"
196192
)
197193
# OpenTherm entity
198194
if entity["name"] != "OnOff":
199195
self._check_availability(
200-
entity, "heater_central", data, "no OpenTherm communication"
196+
entity, "heater_central", "no OpenTherm communication"
201197
)
202198

203199
# Switching groups data
204-
self._entity_switching_group(entity, data)
200+
self._entity_switching_group(entity)
205201
# Adam data
206202
if self.check_name(ADAM):
207-
self._get_adam_data(entity, data)
203+
self._get_adam_data(entity)
208204

209205
# Thermostat data for Anna (presets, temperatures etc)
210206
if self.check_name(ANNA) and entity["dev_class"] == "thermostat":
211-
self._climate_data(entity_id, entity, data)
212-
self._get_anna_control_state(data)
213-
214-
return data
207+
self._climate_data(entity_id, entity)
208+
self._get_anna_control_state(entity)
215209

216210
def _check_availability(
217-
self, entity: GwEntityData, dev_class: str, data: GwEntityData, message: str
211+
self, entity: GwEntityData, dev_class: str, message: str
218212
) -> None:
219213
"""Helper-function for _get_entity_data().
220214
221215
Provide availability status for the wired-connected devices.
222216
"""
223217
if entity["dev_class"] == dev_class:
224-
data["available"] = True
218+
entity["available"] = True
225219
self._count += 1
226220
for item in self._notifications.values():
227221
for msg in item.values():
228222
if message in msg:
229-
data["available"] = False
223+
entity["available"] = False
230224
break
231225

232-
def _get_adam_data(self, entity: GwEntityData, data: GwEntityData) -> None:
226+
def _get_adam_data(self, entity: GwEntityData) -> None:
233227
"""Helper-function for _get_entity_data().
234228
235229
Determine Adam heating-status for on-off heating via valves,
@@ -239,28 +233,26 @@ def _get_adam_data(self, entity: GwEntityData, data: GwEntityData) -> None:
239233
if entity["dev_class"] == "heater_central":
240234
# Indicate heating_state based on valves being open in case of city-provided heating
241235
if self._on_off_device and isinstance(self._heating_valves(), int):
242-
data["binary_sensors"]["heating_state"] = self._heating_valves() != 0
236+
entity["binary_sensors"]["heating_state"] = self._heating_valves() != 0
243237
# Add cooling_enabled binary_sensor
244238
if (
245-
"binary_sensors" in data
246-
and "cooling_enabled" not in data["binary_sensors"]
239+
"binary_sensors" in entity
240+
and "cooling_enabled" not in entity["binary_sensors"]
247241
and self._cooling_present
248242
):
249-
data["binary_sensors"]["cooling_enabled"] = self._cooling_enabled
243+
entity["binary_sensors"]["cooling_enabled"] = self._cooling_enabled
250244
self._count += 1
251245

252246
# Show the allowed regulation_modes and gateway_modes
253247
if entity["dev_class"] == "gateway":
254248
if self._reg_allowed_modes:
255-
data["regulation_modes"] = self._reg_allowed_modes
249+
entity["regulation_modes"] = self._reg_allowed_modes
256250
self._count += 1
257251
if self._gw_allowed_modes:
258-
data["gateway_modes"] = self._gw_allowed_modes
252+
entity["gateway_modes"] = self._gw_allowed_modes
259253
self._count += 1
260254

261-
def _climate_data(
262-
self, location_id: str, entity: GwEntityData, data: GwEntityData
263-
) -> None:
255+
def _climate_data(self, location_id: str, entity: GwEntityData) -> None:
264256
"""Helper-function for _get_entity_data().
265257
266258
Determine climate-control entity data.
@@ -270,38 +262,38 @@ def _climate_data(
270262
loc_id = entity["location"]
271263

272264
# Presets
273-
data["preset_modes"] = None
274-
data["active_preset"] = None
265+
entity["preset_modes"] = None
266+
entity["active_preset"] = None
275267
self._count += 2
276268
if presets := self._presets(loc_id):
277-
data["preset_modes"] = list(presets)
278-
data["active_preset"] = self._preset(loc_id)
269+
entity["preset_modes"] = list(presets)
270+
entity["active_preset"] = self._preset(loc_id)
279271

280272
# Schedule
281-
data["available_schedules"] = []
282-
data["select_schedule"] = None
273+
entity["available_schedules"] = []
274+
entity["select_schedule"] = None
283275
self._count += 2
284276
avail_schedules, sel_schedule = self._schedules(loc_id)
285277
if avail_schedules != [NONE]:
286-
data["available_schedules"] = avail_schedules
287-
data["select_schedule"] = sel_schedule
278+
entity["available_schedules"] = avail_schedules
279+
entity["select_schedule"] = sel_schedule
288280

289281
# Set HA climate HVACMode: auto, heat, heat_cool, cool and off
290-
data["climate_mode"] = "auto"
282+
entity["climate_mode"] = "auto"
291283
self._count += 1
292284
if sel_schedule in (NONE, OFF):
293-
data["climate_mode"] = "heat"
285+
entity["climate_mode"] = "heat"
294286
if self._cooling_present:
295-
data["climate_mode"] = (
287+
entity["climate_mode"] = (
296288
"cool" if self.check_reg_mode("cooling") else "heat_cool"
297289
)
298290

299291
if self.check_reg_mode("off"):
300-
data["climate_mode"] = "off"
292+
entity["climate_mode"] = "off"
301293

302294
if NONE not in avail_schedules:
303295
self._get_schedule_states_with_off(
304-
loc_id, avail_schedules, sel_schedule, data
296+
loc_id, avail_schedules, sel_schedule, entity
305297
)
306298

307299
def check_reg_mode(self, mode: str) -> bool:
@@ -326,7 +318,7 @@ def _get_anna_control_state(self, data: GwEntityData) -> None:
326318
data["control_state"] = "cooling"
327319

328320
def _get_schedule_states_with_off(
329-
self, location: str, schedules: list[str], selected: str, data: GwEntityData
321+
self, location: str, schedules: list[str], selected: str, entity: GwEntityData
330322
) -> None:
331323
"""Collect schedules with states for each thermostat.
332324
@@ -335,11 +327,11 @@ def _get_schedule_states_with_off(
335327
all_off = True
336328
self._schedule_old_states[location] = {}
337329
for schedule in schedules:
338-
active: bool = schedule == selected and data["climate_mode"] == "auto"
330+
active: bool = schedule == selected and entity["climate_mode"] == "auto"
339331
self._schedule_old_states[location][schedule] = "off"
340332
if active:
341333
self._schedule_old_states[location][schedule] = "on"
342334
all_off = False
343335

344336
if all_off:
345-
data["select_schedule"] = OFF
337+
entity["select_schedule"] = OFF

plugwise/helper.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,39 +305,37 @@ def _get_appliances_with_offset_functionality(self) -> list[str]:
305305

306306
return therm_list
307307

308-
def _get_zone_data(self, loc_id: str) -> GwEntityData:
308+
def _get_zone_data(self, loc_id: str, zone: GwEntityData) -> None:
309309
"""Helper-function for smile.py: _get_entity_data().
310310
311-
Collect the location-data based on location id.
311+
Collect the location/zone-data based on location id.
312312
"""
313313
data: GwEntityData = {"sensors": {}}
314-
zone = self._zones[loc_id]
315314
measurements = ZONE_MEASUREMENTS
316315
if (
317316
location := self._domain_objects.find(f'./location[@id="{loc_id}"]')
318317
) is not None:
319318
self._appliance_measurements(location, data, measurements)
320319
self._get_actuator_functionalities(location, zone, data)
321320

322-
return data
321+
zone.update(data)
323322

324-
def _get_measurement_data(self, entity_id: str) -> GwEntityData:
323+
def _get_measurement_data(self, entity_id: str, entity: GwEntityData) -> None:
325324
"""Helper-function for smile.py: _get_entity_data().
326325
327326
Collect the appliance-data based on entity_id.
328327
"""
329328
data: GwEntityData = {"binary_sensors": {}, "sensors": {}, "switches": {}}
330-
entity = self.gw_entities[entity_id]
331329

332330
# Get P1 smartmeter data from LOCATIONS
331+
is_smartmeter = entity.get("dev_class") == "smartmeter"
333332
smile_is_power = self.smile.type == "power"
334-
if (smile_is_power or self.smile.anna_p1) and entity.get(
335-
"dev_class"
336-
) == "smartmeter":
333+
if is_smartmeter and (smile_is_power or self.smile.anna_p1):
337334
data.update(self._power_data_from_location())
338335

339336
if smile_is_power and not self.smile.anna_p1:
340-
return data
337+
entity.update(data)
338+
return
341339

342340
# Get group data
343341
if "members" in entity:
@@ -372,7 +370,7 @@ def _get_measurement_data(self, entity_id: str) -> GwEntityData:
372370

373371
self._cleanup_data(data)
374372

375-
return data
373+
entity.update(data)
376374

377375
def _collect_group_sensors(
378376
self,

0 commit comments

Comments
 (0)