Skip to content

Commit 196275e

Browse files
committed
Improve _set_groupswitch_member_state() functions as suggested
1 parent 2b0bf39 commit 196275e

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

plugwise/legacy/smile.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,17 @@ async def _set_groupswitch_member_state(
301301
Return the current group-state when none of the switches has changed its state, the requested state otherwise.
302302
"""
303303
current_state = self.gw_entities[appl_id]["switches"]["relay"]
304+
end_states: list[bool] = []
304305
requested_state = state == STATE_ON
305-
switched = 0
306306
for member in members:
307307
if not self.gw_entities[member]["switches"]["lock"]:
308308
uri = f"{APPLIANCES};id={member}/relay"
309309
await self.call_request(uri, method="put", data=data)
310-
switched += 1
310+
311+
end_states.append(self.gw_entities[member]["switches"].get("relay", False))
311312

312-
if switched > 0:
313-
return requested_state
314-
315-
return current_state # pragma: no cover
313+
# Return True only if all members match the requested state
314+
return all(end_states) if requested_state else not any(end_states)
316315

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

plugwise/smile.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,20 @@ async def _set_groupswitch_member_state(
452452
Return the current group-state when none of the switches has changed its state, the requested state otherwise.
453453
"""
454454
current_state = self.gw_entities[appl_id]["switches"]["relay"]
455+
end_states: list[bool] = []
455456
requested_state = state == STATE_ON
456-
switched = 0
457457
for member in members:
458458
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
459459
switch_id = self._domain_objects.find(locator).attrib["id"]
460460
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
461-
if not self.gw_entities[member]["switches"].get("lock"):
461+
lock_blocked = self.gw_entities[member]["switches"].get("lock")
462+
if lock_blocked is not None and not lock_blocked:
462463
await self.call_request(uri, method="put", data=data)
463-
switched += 1
464464

465-
if switched > 0:
466-
return requested_state
465+
end_states.append(self.gw_entities[member]["switches"].get("relay", False))
467466

468-
return current_state
467+
# Return True only if all members match the requested state
468+
return all(end_states) if requested_state else not any(end_states)
469469

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

0 commit comments

Comments
 (0)