Skip to content

Commit e584329

Browse files
committed
Improve error-handling as suggested
1 parent ae27f8d commit e584329

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

plugwise/legacy/helper.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,11 @@ def _presets(self) -> dict[str, list[float]]:
393393
"""Helper-function for presets() - collect Presets for a legacy Anna."""
394394
presets: dict[str, list[float]] = {}
395395
for directive in self._domain_objects.findall("rule/directives/when/then"):
396-
if directive is not None and directive.get("icon") is not None:
396+
if (
397+
directive is not None
398+
and directive.get("icon") is not None
399+
and directive.get("temperature") is not None
400+
):
397401
# Ensure list of heating_setpoint, cooling_setpoint
398402
presets[directive.get("icon")] = [
399403
float(directive.get("temperature")),

plugwise/legacy/smile.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ async def set_offset(self, dev_id: str, offset: float) -> None:
152152

153153
async def set_preset(self, _: str, preset: str) -> None:
154154
"""Set the given Preset on the relevant Thermostat - from DOMAIN_OBJECTS."""
155-
if (presets := self._presets()) is None:
155+
if not (presets := self._presets()):
156156
raise PlugwiseError("Plugwise: no presets available.") # pragma: no cover
157157
if preset not in list(presets):
158158
raise PlugwiseError("Plugwise: invalid preset.")
159159

160160
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
161-
rule_id = self._domain_objects.find(locator).get("id")
161+
if (rule_id := self._domain_objects.find(locator)) is None:
162+
raise PlugwiseError("Plugwise: no preset rule found.") # pragma: no cover
163+
if rule_id.get("id") is None:
164+
raise PlugwiseError("Plugwise: no preset id found.") # pragma: no cover
165+
162166
data = f"<rules><rule id='{rule_id}'><active>true</active></rule></rules>"
163167
await self.call_request(RULES, method="put", data=data)
164168

plugwise/util.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,36 @@ def check_heater_central(xml: etree.Element) -> str:
8282
for a system that has two heater_central appliances.
8383
"""
8484
locator = "./appliance[type='heater_central']"
85-
hc_count = 0
86-
hc_list: list[dict[str, bool]] = []
87-
for heater_central in xml.findall(locator):
88-
hc_count += 1
89-
hc_id: str = heater_central.get("id")
90-
has_actuators: bool = (
91-
heater_central.find("actuator_functionalities/") is not None
92-
)
85+
heater_central_count = 0
86+
heater_central_list: list[dict[str, bool]] = []
87+
if not (result := xml.findall(locator)):
88+
return NONE # pragma: no cover
89+
90+
for heater_central in result:
91+
if (heater_central_id := heater_central.get("id")) is None:
92+
continue # pragma: no cover
93+
94+
if (heater_central_name := heater_central.find("name")) is None:
95+
continue # pragma: no cover
96+
97+
has_actuators = heater_central.find("actuator_functionalities/") is not None
9398
# Filter for Plug/Circle/Stealth heater_central -- Pw-Beta Issue #739
94-
if heater_central.find("name").text == "Central heating boiler":
95-
hc_list.append({hc_id: has_actuators})
99+
if heater_central_name.text == "Central heating boiler":
100+
heater_central_list.append({heater_central_id: has_actuators})
101+
heater_central_count += 1
96102

97-
if not hc_list:
103+
if not heater_central_list:
98104
return NONE # pragma: no cover
99105

100-
heater_central_id = list(hc_list[0].keys())[0]
101-
if hc_count > 1:
102-
for item in hc_list:
103-
hc_id, has_actuators = next(iter(item.items()))
106+
heater_id = list(heater_central_list[0].keys())[0]
107+
if heater_central_count > 1:
108+
for item in heater_central_list:
109+
heater_central_id, has_actuators = next(iter(item.items()))
104110
if has_actuators:
105-
heater_central_id = hc_id
111+
heater_id = heater_central_id
106112
break
107113

108-
return heater_central_id
114+
return heater_id
109115

110116

111117
def check_model(name: str | None, vendor_name: str | None) -> str | None:

0 commit comments

Comments
 (0)