From e91c8e5f733cccc041575b6221f48dce1f6edf29 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Thu, 12 Nov 2026 20:11:53 +0100 Subject: [PATCH 1/4] Lower complexity in legacy/helper/py _appliance_measurements() --- plugwise/legacy/helper.py | 62 +++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/plugwise/legacy/helper.py b/plugwise/legacy/helper.py index 370df0954..6dd3f9b67 100644 --- a/plugwise/legacy/helper.py +++ b/plugwise/legacy/helper.py @@ -323,31 +323,55 @@ def _appliance_measurements( ) -> None: """Helper-function for _get_measurement_data() - collect appliance measurement data.""" for measurement, attrs in measurements.items(): - p_locator = f'.//logs/point_log[type="{measurement}"]/period/measurement' - if (appl_p_loc := appliance.find(p_locator)) is not None: - if measurement == "domestic_hot_water_state": - continue + measurement = self._appliance_point_measurement( + appliance, data, measurement, attrs + ) + if measurement is None: + continue - if skip_obsolete_measurements(appliance, measurement): - continue # pragma: no cover + self._appliance_interval_measurement(appliance, data, measurement, attrs) - if new_name := getattr(attrs, ATTR_NAME, None): - measurement = new_name + self._count = count_data_items(self._count, data) - common_match_cases(measurement, attrs, appl_p_loc, data) + def _appliance_point_measurement( + self, + appliance: etree.Element, + data: GwEntityData, + measurement: str, + attrs: DATA | UOM, + ) -> str | None: + """Collect point-log data and return its measurement name, or None to skip.""" + p_locator = f'.//logs/point_log[type="{measurement}"]/period/measurement' + if (appl_p_loc := appliance.find(p_locator)) is None: + return measurement + + if measurement == "domestic_hot_water_state": + return None - # Only power-measurements have a matching energy interval_log, skip the others - if getattr(attrs, ATTR_UNIT_OF_MEASUREMENT) != POWER_WATT: - continue + if skip_obsolete_measurements(appliance, measurement): + return None # pragma: no cover - i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement' - if (appl_i_loc := appliance.find(i_locator)) is not None: - name = cast(SensorType, f"{measurement}_interval") - data["sensors"][name] = format_measure( - appl_i_loc.text, ENERGY_WATT_HOUR - ) + if new_name := getattr(attrs, ATTR_NAME, None): + measurement = new_name - self._count = count_data_items(self._count, data) + common_match_cases(measurement, attrs, appl_p_loc, data) + return measurement + + def _appliance_interval_measurement( + self, + appliance: etree.Element, + data: GwEntityData, + measurement: str, + attrs: DATA | UOM, + ) -> None: + """Collect interval-log data for power measurements.""" + if getattr(attrs, ATTR_UNIT_OF_MEASUREMENT) != POWER_WATT: + return + + i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement' + if (appl_i_loc := appliance.find(i_locator)) is not None: + name = cast(SensorType, f"{measurement}_interval") + data["sensors"][name] = format_measure(appl_i_loc.text, ENERGY_WATT_HOUR) def _get_actuator_functionalities( self, From 8e5b0dcc3117799560c5358f6bdbd00fdbe93b85 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Thu, 12 Nov 2026 20:14:14 +0100 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4155f6571..65276560a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Ongoing + +- Improve code, lower complexity via PR + ## v1.17.0 - Contribution PR [#949](https://github.com/plugwise/python-plugwise/pull/949) by @bilhert: From 8289f3838d83b662d298a7d0788be78b8604d605 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Thu, 12 Nov 2026 20:17:00 +0100 Subject: [PATCH 3/4] Fix mypy error, method error --- plugwise/legacy/helper.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugwise/legacy/helper.py b/plugwise/legacy/helper.py index 6dd3f9b67..ab60d9e30 100644 --- a/plugwise/legacy/helper.py +++ b/plugwise/legacy/helper.py @@ -323,10 +323,10 @@ def _appliance_measurements( ) -> None: """Helper-function for _get_measurement_data() - collect appliance measurement data.""" for measurement, attrs in measurements.items(): - measurement = self._appliance_point_measurement( - appliance, data, measurement, attrs - ) - if measurement is None: + if ( + self._appliance_point_measurement(appliance, data, measurement, attrs) + is None + ): continue self._appliance_interval_measurement(appliance, data, measurement, attrs) From 40c91182d1acd34d951d27060351f8c277c64cac Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Thu, 12 Nov 2026 20:27:47 +0100 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65276560a..bf97a699c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Ongoing -- Improve code, lower complexity via PR +- Improve code, lower complexity via PR [#951](https://github.com/plugwise/python-plugwise/pull/951) ## v1.17.0