Skip to content

Commit 7156d00

Browse files
committed
Let set-switch functions return a bool, improve handling of locked state
1 parent 966ba34 commit 7156d00

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

plugwise/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ async def set_temperature_offset(self, dev_id: str, offset: float) -> None:
398398

399399
async def set_switch_state(
400400
self, appl_id: str, members: list[str] | None, model: str, state: str
401-
) -> None:
401+
) -> bool:
402402
"""Set the given State of the relevant Switch."""
403403
try:
404-
await self._smile_api.set_switch_state(appl_id, members, model, state)
404+
return await self._smile_api.set_switch_state(appl_id, members, model, state)
405405
except ConnectionFailedError as exc:
406406
raise ConnectionFailedError(
407407
f"Failed to set switch state: {str(exc)}"

plugwise/legacy/smile.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ async def set_schedule_state(
234234

235235
async def set_switch_state(
236236
self, appl_id: str, members: list[str] | None, model: str, state: str
237-
) -> None:
237+
) -> bool:
238238
"""Set the given state of the relevant switch.
239239
240240
For individual switches, sets the state directly.
@@ -269,7 +269,7 @@ async def set_switch_state(
269269
"</appliances>"
270270
)
271271
await self.call_request(APPLIANCES, method="post", data=data)
272-
return
272+
return True
273273

274274
# Handle group of switches
275275
data = f"<{switch.func_type}><state>{state}</state></{switch.func_type}>"
@@ -280,26 +280,26 @@ async def set_switch_state(
280280

281281
# Handle individual relay switches
282282
uri = f"{APPLIANCES};id={appl_id}/relay"
283-
if model == "relay":
284-
locator = (
285-
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}/lock'
286-
)
283+
if model == "relay" and self.gw_entities[appl_id]["switches"]["lock"]:
287284
# Don't bother switching a relay when the corresponding lock-state is true
288-
if self._appliances.find(locator).text == "true":
289-
raise PlugwiseError("Plugwise: the locked Relay was not switched.")
285+
return False
290286

291287
await self.call_request(uri, method="put", data=data)
288+
return True
292289

293290
async def _set_groupswitch_member_state(
294291
self, data: str, members: list[str], state: str, switch: Munch
295-
) -> None:
292+
) -> bool:
296293
"""Helper-function for set_switch_state().
297294
298295
Set the given State of the relevant Switch (relay) within a group of members.
299296
"""
300297
for member in members:
301-
uri = f"{APPLIANCES};id={member}/relay"
302-
await self.call_request(uri, method="put", data=data)
298+
if not self.gw_entities[member]["switches"]["lock"]:
299+
uri = f"{APPLIANCES};id={member}/relay"
300+
await self.call_request(uri, method="put", data=data)
301+
302+
return True
303303

304304
async def set_temperature(self, _: str, items: dict[str, float]) -> None:
305305
"""Set the given Temperature on the relevant Thermostat."""

plugwise/smile.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def determine_contexts(
377377

378378
async def set_switch_state(
379379
self, appl_id: str, members: list[str] | None, model: str, state: str
380-
) -> None:
380+
) -> bool:
381381
"""Set the given State of the relevant Switch."""
382382
switch = Munch()
383383
switch.actuator = "actuator_functionalities"
@@ -418,19 +418,16 @@ async def set_switch_state(
418418
f"</{switch.func_type}>"
419419
)
420420
uri = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
421-
if model == "relay":
422-
locator = (
423-
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}/lock'
424-
)
425-
# Don't bother switching a relay when the corresponding lock-state is true
426-
if self._domain_objects.find(locator).text == "true":
427-
raise PlugwiseError("Plugwise: the locked Relay was not switched.")
421+
if model == "relay" and self.gw_entities[appl_id]["switches"]["lock"]:
422+
# Don't switch a relay when its corresponding lock-state is true
423+
return False
428424

429425
await self.call_request(uri, method="put", data=data)
426+
return True
430427

431428
async def _set_groupswitch_member_state(
432429
self, members: list[str], state: str, switch: Munch
433-
) -> None:
430+
) -> bool:
434431
"""Helper-function for set_switch_state().
435432
436433
Set the given State of the relevant Switch within a group of members.
@@ -444,7 +441,10 @@ async def _set_groupswitch_member_state(
444441
f"<{switch.func}>{state}</{switch.func}>"
445442
f"</{switch.func_type}>"
446443
)
447-
await self.call_request(uri, method="put", data=data)
444+
if not self.gw_entities[member]["switches"].get("lock"):
445+
await self.call_request(uri, method="put", data=data)
446+
447+
return True
448448

449449
async def set_temperature(self, loc_id: str, items: dict[str, float]) -> None:
450450
"""Set the given Temperature on the relevant Thermostat."""

0 commit comments

Comments
 (0)