Skip to content

Commit 48f72ec

Browse files
committed
Move preset input-checking to __init__.py, use constants
1 parent b004369 commit 48f72ec

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

plugwise/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ async def set_schedule_state(
359359

360360
async def set_preset(self, loc_id: str, preset: str) -> None:
361361
"""Set the given Preset on the relevant Thermostat."""
362+
if (presets := self._presets(loc_id)) is None:
363+
raise PlugwiseError("Plugwise: no presets available.") # pragma: no cover
364+
if preset not in list(presets):
365+
raise PlugwiseError("Plugwise: invalid preset.")
362366
try:
363367
await self._smile_api.set_preset(loc_id, preset)
364368
except ConnectionFailedError as exc:

plugwise/legacy/smile.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
OFF,
1919
REQUIRE_APPLIANCES,
2020
RULES,
21+
STATE_OFF,
2122
STATE_ON,
2223
GwEntityData,
2324
ThermoLoc,
@@ -168,11 +169,6 @@ async def set_offset(self, dev_id: str, offset: float) -> None:
168169

169170
async def set_preset(self, _: str, preset: str) -> None:
170171
"""Set the given Preset on the relevant Thermostat - from DOMAIN_OBJECTS."""
171-
if (presets := self._presets()) is None:
172-
raise PlugwiseError("Plugwise: no presets available.") # pragma: no cover
173-
if preset not in list(presets):
174-
raise PlugwiseError("Plugwise: invalid preset.")
175-
176172
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
177173
rule_id = self._domain_objects.find(locator).attrib["id"]
178174
data = f"<rules><rule id='{rule_id}'><active>true</active></rule></rules>"
@@ -196,7 +192,7 @@ async def set_schedule_state(
196192
Determined from - DOMAIN_OBJECTS.
197193
Used in HA Core to set the hvac_mode: in practice switch between schedule on - off.
198194
"""
199-
if state not in ("on", "off"):
195+
if state not in (STATE_OFF, STATE_ON):
200196
raise PlugwiseError("Plugwise: invalid schedule state.")
201197

202198
# Handle no schedule-name / Off-schedule provided
@@ -215,7 +211,7 @@ async def set_schedule_state(
215211
) # pragma: no cover
216212

217213
new_state = "false"
218-
if state == "on":
214+
if state == STATE_ON:
219215
new_state = "true"
220216

221217
locator = f'.//*[@id="{schedule_rule_id}"]/template'

plugwise/smile.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
NOTIFICATIONS,
2323
OFF,
2424
RULES,
25+
STATE_OFF,
2526
STATE_ON,
2627
GwEntityData,
2728
SwitchType,
@@ -201,11 +202,6 @@ async def set_offset(self, dev_id: str, offset: float) -> None:
201202

202203
async def set_preset(self, loc_id: str, preset: str) -> None:
203204
"""Set the given Preset on the relevant Thermostat - from LOCATIONS."""
204-
if (presets := self._presets(loc_id)) is None:
205-
raise PlugwiseError("Plugwise: no presets available.") # pragma: no cover
206-
if preset not in list(presets):
207-
raise PlugwiseError("Plugwise: invalid preset.")
208-
209205
current_location = self._domain_objects.find(f'location[@id="{loc_id}"]')
210206
location_name = current_location.find("name").text
211207
location_type = current_location.find("type").text
@@ -311,12 +307,12 @@ async def set_schedule_state(
311307
Used in HA Core to set the hvac_mode: in practice switch between schedule on - off.
312308
"""
313309
# Input checking
314-
if new_state not in ("on", "off"):
310+
if new_state not in (STATE_OFF, STATE_ON):
315311
raise PlugwiseError("Plugwise: invalid schedule state.")
316312

317313
# Translate selection of Off-schedule-option to disabling the active schedule
318314
if name == OFF:
319-
new_state = "off"
315+
new_state = STATE_OFF
320316

321317
# Handle no schedule-name / Off-schedule provided
322318
if name is None or name == OFF:
@@ -369,10 +365,10 @@ def determine_contexts(
369365
subject = f'<context><zone><location id="{loc_id}" /></zone></context>'
370366
subject = etree.fromstring(subject)
371367

372-
if state == "off":
368+
if state == STATE_OFF:
373369
self._last_active[loc_id] = name
374370
contexts.remove(subject)
375-
if state == "on":
371+
if state == STATE_ON:
376372
contexts.append(subject)
377373

378374
return str(etree.tostring(contexts, encoding="unicode").rstrip())

0 commit comments

Comments
 (0)