Skip to content

Commit b743e1b

Browse files
committed
Less complex
1 parent a2c05d7 commit b743e1b

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

plugwise/helper.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,13 @@ def _get_measurement_data(self, entity_id: str) -> GwEntityData:
327327
data: GwEntityData = {"binary_sensors": {}, "sensors": {}, "switches": {}}
328328
# Get P1 smartmeter data from LOCATIONS
329329
entity = self.gw_entities[entity_id]
330-
# !! DON'T CHANGE below two if-lines, will break stuff !!
331-
if self.smile.type == "power" or self.smile.anna_p1:
332-
if entity["dev_class"] == "smartmeter":
333-
data.update(self._power_data_from_location())
330+
dev_class = entity.get("dev_class")
331+
smile_is_power = self.smile.type == "power"
332+
if (smile_is_power or self.smile.anna_p1) and dev_class == "smartmeter":
333+
data.update(self._power_data_from_location())
334334

335-
if not self.smile.anna_p1:
336-
return data
335+
if smile_is_power and not self.smile.anna_p1:
336+
return data
337337

338338
# Get non-P1 data from APPLIANCES
339339
measurements = DEVICE_MEASUREMENTS
@@ -345,20 +345,13 @@ def _get_measurement_data(self, entity_id: str) -> GwEntityData:
345345
# Counting of this item is done in _appliance_measurements()
346346

347347
if (
348-
appliance := self._domain_objects.find(f'./appliance[@id="{entity_id}"]')
348+
appliance := self._collect_appliance_data(
349+
data, entity, entity_id, measurements
350+
)
349351
) is not None:
350-
self._appliance_measurements(appliance, data, measurements)
351-
self._get_lock_state(appliance, data)
352-
353-
for toggle, name in TOGGLES.items():
354-
self._get_toggle_state(appliance, toggle, name, data)
355-
356-
if appliance.find("type").text in ACTUATOR_CLASSES:
357-
self._get_actuator_functionalities(appliance, entity, data)
358-
359-
self._get_regulation_mode(appliance, entity_id, data)
360-
self._get_gateway_mode(appliance, entity_id, data)
361-
self._get_gateway_outdoor_temp(entity_id, data)
352+
self._get_regulation_mode(appliance, entity_id, data)
353+
self._get_gateway_mode(appliance, entity_id, data)
354+
self._get_gateway_outdoor_temp(entity_id, data)
362355

363356
if "c_heating_state" in data:
364357
self._process_c_heating_state(data)
@@ -373,6 +366,30 @@ def _get_measurement_data(self, entity_id: str) -> GwEntityData:
373366

374367
return data
375368

369+
def _collect_appliance_data(
370+
self,
371+
data: GwEntityData,
372+
entity: GwEntityData,
373+
entity_id: str,
374+
measurements: dict[str, DATA | UOM],
375+
) -> etree.Element | None:
376+
"""Collect initial appliance data."""
377+
if (
378+
appliance := self._domain_objects.find(f'./appliance[@id="{entity_id}"]')
379+
) is not None:
380+
self._appliance_measurements(appliance, data, measurements)
381+
self._get_lock_state(appliance, data)
382+
383+
for toggle, name in TOGGLES.items():
384+
self._get_toggle_state(appliance, toggle, name, data)
385+
386+
if appliance.find("type").text in ACTUATOR_CLASSES:
387+
self._get_actuator_functionalities(appliance, entity, data)
388+
389+
return appliance
390+
391+
return None
392+
376393
def _power_data_from_location(self) -> GwEntityData:
377394
"""Helper-function for smile.py: _get_entity_data().
378395

0 commit comments

Comments
 (0)