From 9814fe42372f45c52c2dd8b8d7576fd0931196a3 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 08:10:00 +0100 Subject: [PATCH 01/13] Use find not findall --- plugwise/legacy/smile.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugwise/legacy/smile.py b/plugwise/legacy/smile.py index 7cd22032c..aaef8a136 100644 --- a/plugwise/legacy/smile.py +++ b/plugwise/legacy/smile.py @@ -215,8 +215,7 @@ async def set_schedule_state( new_state = "true" locator = f'.//*[@id="{schedule_rule_id}"]/template' - for rule in self._domain_objects.findall(locator): - template_id = rule.attrib["id"] + template_id = self._domain_objects.find(locator).attrib["id"] data = ( "" From 3f76d737f335abf1e04c875c64e06c28edbf23b2 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 08:17:05 +0100 Subject: [PATCH 02/13] Exit for-loop when id is found --- plugwise/legacy/smile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugwise/legacy/smile.py b/plugwise/legacy/smile.py index aaef8a136..2d1e42eef 100644 --- a/plugwise/legacy/smile.py +++ b/plugwise/legacy/smile.py @@ -204,6 +204,7 @@ async def set_schedule_state( for rule in self._domain_objects.findall("rule"): if rule.find("name").text == name: schedule_rule_id = rule.attrib["id"] + break if schedule_rule_id is None: raise PlugwiseError( From b2aa5398ca5ad15051dc6467430cb8afc62e24d2 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 08:35:45 +0100 Subject: [PATCH 03/13] Add comment, move break to optimize --- plugwise/smile.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugwise/smile.py b/plugwise/smile.py index aec1a6fa8..fa093373b 100644 --- a/plugwise/smile.py +++ b/plugwise/smile.py @@ -401,12 +401,13 @@ async def set_switch_state( locator = f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}' found: list[etree] = self._domain_objects.findall(locator) for item in found: + # multiple types of e.g. toggle_functionality present if (sw_type := item.find("type")) is not None: if sw_type.text == switch.act_type: switch_id = item.attrib["id"] - else: + break + else: # actuators with a single item like relay_functionality switch_id = item.attrib["id"] - break data = ( f"<{switch.func_type}>" From 28408bf5df8caca38654d3de6156992f1b68abe7 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 09:56:02 +0100 Subject: [PATCH 04/13] Leave for-loops when done, use constant --- plugwise/__init__.py | 1 + plugwise/data.py | 4 ++++ plugwise/helper.py | 1 + plugwise/legacy/helper.py | 4 +++- 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/plugwise/__init__.py b/plugwise/__init__.py index d2160663f..ce9da34e9 100644 --- a/plugwise/__init__.py +++ b/plugwise/__init__.py @@ -303,6 +303,7 @@ async def _smile_detect_legacy( locator = f"./{network}/mac" if (net_locator := system.find(locator)) is not None: self.smile_mac_address = net_locator.text + break # P1 legacy: elif dsmrmain is not None: status = await self._request(STATUS) diff --git a/plugwise/data.py b/plugwise/data.py index e89720740..edf7a74fd 100644 --- a/plugwise/data.py +++ b/plugwise/data.py @@ -69,6 +69,7 @@ def _update_gw_entities(self) -> None: if entity_id == self._gateway_id: mac_list = self._detect_low_batteries() self._add_or_update_notifications(entity_id, entity, data) + break # one gateway present entity.update(data) is_battery_low = ( @@ -228,6 +229,7 @@ def _check_availability( for msg in item.values(): if message in msg: data["available"] = False + break def _get_adam_data(self, entity: GwEntityData, data: GwEntityData) -> None: """Helper-function for _get_entity_data(). @@ -334,11 +336,13 @@ def _get_schedule_states_with_off( loc_schedule_states[schedule] = "off" if schedule == selected and data["climate_mode"] == "auto": loc_schedule_states[schedule] = "on" + self._schedule_old_states[location] = loc_schedule_states all_off = True for state in self._schedule_old_states[location].values(): if state == "on": all_off = False + if all_off: data["select_schedule"] = OFF diff --git a/plugwise/helper.py b/plugwise/helper.py index d738f70a3..c053cdcdf 100644 --- a/plugwise/helper.py +++ b/plugwise/helper.py @@ -195,6 +195,7 @@ def _sort_gw_entities(self) -> None: other_entities = self.gw_entities priority_entities = {entity_id: priority_entity} self.gw_entities = {**priority_entities, **other_entities} + break def _all_locations(self) -> None: """Collect all locations.""" diff --git a/plugwise/legacy/helper.py b/plugwise/legacy/helper.py index 9cf703c5b..75a04098b 100644 --- a/plugwise/legacy/helper.py +++ b/plugwise/legacy/helper.py @@ -22,6 +22,7 @@ LIMITS, NONE, OFF, + PRIORITY_DEVICE_CLASSES , P1_LEGACY_MEASUREMENTS, TEMP_CELSIUS, THERMOSTAT_CLASSES, @@ -130,7 +131,7 @@ def _all_appliances(self) -> None: self._create_gw_entities(appl) # Place the gateway and optional heater_central devices as 1st and 2nd - for dev_class in ("heater_central", "gateway"): + for dev_class in PRIORITY_DEVICE_CLASSES : for entity_id, entity in dict(self.gw_entities).items(): if entity["dev_class"] == dev_class: tmp_entity = entity @@ -138,6 +139,7 @@ def _all_appliances(self) -> None: cleared_dict = self.gw_entities add_to_front = {entity_id: tmp_entity} self.gw_entities = {**add_to_front, **cleared_dict} + break def _all_locations(self) -> None: """Collect all locations.""" From 84537bc4faccb07f04e534df1760bc25a84dfad5 Mon Sep 17 00:00:00 2001 From: autoruff Date: Tue, 18 Feb 2025 08:57:56 +0000 Subject: [PATCH 05/13] fixup: improve-01 Python code fixed using ruff --- plugwise/legacy/helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugwise/legacy/helper.py b/plugwise/legacy/helper.py index 75a04098b..c4143de6e 100644 --- a/plugwise/legacy/helper.py +++ b/plugwise/legacy/helper.py @@ -22,7 +22,7 @@ LIMITS, NONE, OFF, - PRIORITY_DEVICE_CLASSES , + PRIORITY_DEVICE_CLASSES, P1_LEGACY_MEASUREMENTS, TEMP_CELSIUS, THERMOSTAT_CLASSES, @@ -131,7 +131,7 @@ def _all_appliances(self) -> None: self._create_gw_entities(appl) # Place the gateway and optional heater_central devices as 1st and 2nd - for dev_class in PRIORITY_DEVICE_CLASSES : + for dev_class in PRIORITY_DEVICE_CLASSES: for entity_id, entity in dict(self.gw_entities).items(): if entity["dev_class"] == dev_class: tmp_entity = entity From 3e05a552da0d9980d177982a5cebf4a2d3720d6b Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 10:01:33 +0100 Subject: [PATCH 06/13] Revert breaks --- plugwise/__init__.py | 4 ++-- plugwise/data.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/plugwise/__init__.py b/plugwise/__init__.py index ce9da34e9..c12925eca 100644 --- a/plugwise/__init__.py +++ b/plugwise/__init__.py @@ -298,12 +298,12 @@ async def _smile_detect_legacy( self.smile_version = parse(system.find("./gateway/firmware").text) return_model = str(system.find("./gateway/product").text) self.smile_hostname = system.find("./gateway/hostname").text - # If wlan0 contains data it's active, so eth0 should be checked last + # If wlan0 contains data it's active, eth0 should be checked last as is preferred for network in ("wlan0", "eth0"): locator = f"./{network}/mac" if (net_locator := system.find(locator)) is not None: self.smile_mac_address = net_locator.text - break + # P1 legacy: elif dsmrmain is not None: status = await self._request(STATUS) diff --git a/plugwise/data.py b/plugwise/data.py index edf7a74fd..132b7244c 100644 --- a/plugwise/data.py +++ b/plugwise/data.py @@ -69,7 +69,6 @@ def _update_gw_entities(self) -> None: if entity_id == self._gateway_id: mac_list = self._detect_low_batteries() self._add_or_update_notifications(entity_id, entity, data) - break # one gateway present entity.update(data) is_battery_low = ( From e91a3bb1b3c7aee996c40915c74740f3ac0054d6 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 11:16:22 +0100 Subject: [PATCH 07/13] More updates --- plugwise/data.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/plugwise/data.py b/plugwise/data.py index 132b7244c..65df04244 100644 --- a/plugwise/data.py +++ b/plugwise/data.py @@ -330,18 +330,14 @@ def _get_schedule_states_with_off( Also, replace NONE by OFF when none of the schedules are active. """ - loc_schedule_states: dict[str, str] = {} + self._schedule_old_states[location] = {} for schedule in schedules: - loc_schedule_states[schedule] = "off" - if schedule == selected and data["climate_mode"] == "auto": - loc_schedule_states[schedule] = "on" - - self._schedule_old_states[location] = loc_schedule_states + self._schedule_old_states[location][schedule] = "on" if schedule == selected and data["climate_mode"] == "auto" else "off" all_off = True for state in self._schedule_old_states[location].values(): if state == "on": all_off = False - + break if all_off: data["select_schedule"] = OFF From 15aff4297182011ceb4b08ce573c7b7b2b57bc49 Mon Sep 17 00:00:00 2001 From: autoruff Date: Tue, 18 Feb 2025 10:17:43 +0000 Subject: [PATCH 08/13] fixup: improve-01 Python code fixed using ruff --- plugwise/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugwise/data.py b/plugwise/data.py index 65df04244..964e52b42 100644 --- a/plugwise/data.py +++ b/plugwise/data.py @@ -332,7 +332,11 @@ def _get_schedule_states_with_off( """ self._schedule_old_states[location] = {} for schedule in schedules: - self._schedule_old_states[location][schedule] = "on" if schedule == selected and data["climate_mode"] == "auto" else "off" + self._schedule_old_states[location][schedule] = ( + "on" + if schedule == selected and data["climate_mode"] == "auto" + else "off" + ) all_off = True for state in self._schedule_old_states[location].values(): From 8b9bd067a0c8082d886811ad62b6a1107737ee2a Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 17:43:54 +0100 Subject: [PATCH 09/13] Compact --- plugwise/data.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/plugwise/data.py b/plugwise/data.py index 964e52b42..510732021 100644 --- a/plugwise/data.py +++ b/plugwise/data.py @@ -332,11 +332,8 @@ def _get_schedule_states_with_off( """ self._schedule_old_states[location] = {} for schedule in schedules: - self._schedule_old_states[location][schedule] = ( - "on" - if schedule == selected and data["climate_mode"] == "auto" - else "off" - ) + active: bool = schedule == selected and data["climate_mode"] == "auto" + self._schedule_old_states[location][schedule] = "on" if active else "off" all_off = True for state in self._schedule_old_states[location].values(): From a5543cd745c9c98414854b12a01245a982984d93 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 17:47:12 +0100 Subject: [PATCH 10/13] Sorted imports --- plugwise/legacy/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugwise/legacy/helper.py b/plugwise/legacy/helper.py index c4143de6e..61dbe22ef 100644 --- a/plugwise/legacy/helper.py +++ b/plugwise/legacy/helper.py @@ -22,8 +22,8 @@ LIMITS, NONE, OFF, - PRIORITY_DEVICE_CLASSES, P1_LEGACY_MEASUREMENTS, + PRIORITY_DEVICE_CLASSES, TEMP_CELSIUS, THERMOSTAT_CLASSES, UOM, From ada46e69cdeaabce778cfecfee5484c1b419c048 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 18:12:46 +0100 Subject: [PATCH 11/13] Improve --- plugwise/data.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/plugwise/data.py b/plugwise/data.py index 510732021..b23b4e53a 100644 --- a/plugwise/data.py +++ b/plugwise/data.py @@ -330,15 +330,14 @@ def _get_schedule_states_with_off( Also, replace NONE by OFF when none of the schedules are active. """ + all_off = True self._schedule_old_states[location] = {} for schedule in schedules: active: bool = schedule == selected and data["climate_mode"] == "auto" - self._schedule_old_states[location][schedule] = "on" if active else "off" - - all_off = True - for state in self._schedule_old_states[location].values(): - if state == "on": + self._schedule_old_states[location][schedule] = "off" + if active: + self._schedule_old_states[location][schedule] = "on" all_off = False - break + if all_off: data["select_schedule"] = OFF From 00761fba19e9c95c78aa07e5ade33e6c1c038924 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 18:17:20 +0100 Subject: [PATCH 12/13] Remove typing --- plugwise/smile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugwise/smile.py b/plugwise/smile.py index fa093373b..40df7d333 100644 --- a/plugwise/smile.py +++ b/plugwise/smile.py @@ -399,7 +399,7 @@ async def set_switch_state( return await self._set_groupswitch_member_state(members, state, switch) locator = f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}' - found: list[etree] = self._domain_objects.findall(locator) + found = self._domain_objects.findall(locator) for item in found: # multiple types of e.g. toggle_functionality present if (sw_type := item.find("type")) is not None: From c633ef5edb46c22905b2c8e8c92edc4720a77d25 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Tue, 18 Feb 2025 18:20:35 +0100 Subject: [PATCH 13/13] Update CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87ebf800a..fad6e4e9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Ongoing -- Improve readability of xml-data in POST/PUT requests +- Improve readability of xml-data in POST/PUT requests via [#707](https://github.com/plugwise/python-plugwise/pull/707) and [#708](https://github.com/plugwise/python-plugwise/pull/708) +- Continuous improvements [#711](https://github.com/plugwise/python-plugwise/pull/711) ## v1.7.2