Skip to content

Commit 73eaba2

Browse files
committed
Implement improvements as suggested
1 parent 5978d43 commit 73eaba2

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

plugwise/legacy/smile.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ async def set_switch_state(
242242
For group switches, sets the state for each member in the group separately.
243243
For switch-locks, sets the lock state using a different data format.
244244
"""
245+
current_state = self.gw_entities[appl_id]["switches"]["relay"]
245246
requested_state = state == STATE_ON
246247
switch = Munch()
247248
switch.actuator = "actuator_functionalities"
@@ -277,25 +278,26 @@ async def set_switch_state(
277278
data = f"<{switch.func_type}><state>{state}</state></{switch.func_type}>"
278279
if members is not None:
279280
return await self._set_groupswitch_member_state(
280-
data, members, state, switch
281+
appl_id, data, members, state, switch
281282
)
282283

283284
# Handle individual relay switches
284285
uri = f"{APPLIANCES};id={appl_id}/relay"
285-
if model == "relay" and self.gw_entities[appl_id]["switches"]["lock"]:
286+
if model == "relay" and self.gw_entities[appl_id]["switches"].get("lock"):
286287
# Don't bother switching a relay when the corresponding lock-state is true
287-
return not requested_state
288+
return current_state
288289

289290
await self.call_request(uri, method="put", data=data)
290291
return requested_state
291292

292293
async def _set_groupswitch_member_state(
293-
self, data: str, members: list[str], state: str, switch: Munch
294+
self, appl_id: str, data: str, members: list[str], state: str, switch: Munch
294295
) -> bool:
295296
"""Helper-function for set_switch_state().
296297
297298
Set the given State of the relevant Switch (relay) within a group of members.
298299
"""
300+
current_state = self.gw_entities[appl_id]["switches"]["relay"]
299301
requested_state = state == STATE_ON
300302
switched = 0
301303
for member in members:
@@ -307,7 +309,7 @@ async def _set_groupswitch_member_state(
307309
if switched > 0:
308310
return requested_state
309311

310-
return not requested_state # pragma: no cover
312+
return current_state # pragma: no cover
311313

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

plugwise/smile.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ async def set_switch_state(
380380
self, appl_id: str, members: list[str] | None, model: str, state: str
381381
) -> bool:
382382
"""Set the given State of the relevant Switch."""
383+
current_state = self.gw_entities[appl_id]["switches"]["relay"]
383384
requested_state = state == STATE_ON
384385
switch = Munch()
385386
switch.actuator = "actuator_functionalities"
@@ -400,8 +401,16 @@ async def set_switch_state(
400401
switch.func = "lock"
401402
state = "true" if state == STATE_ON else "false"
402403

404+
data = (
405+
f"<{switch.func_type}>"
406+
f"<{switch.func}>{state}</{switch.func}>"
407+
f"</{switch.func_type}>"
408+
)
409+
403410
if members is not None:
404-
return await self._set_groupswitch_member_state(members, state, switch)
411+
return await self._set_groupswitch_member_state(
412+
appl_id, data, members, state, switch
413+
)
405414

406415
locator = f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}'
407416
found = self._domain_objects.findall(locator)
@@ -414,45 +423,36 @@ async def set_switch_state(
414423
else: # actuators with a single item like relay_functionality
415424
switch_id = item.attrib["id"]
416425

417-
data = (
418-
f"<{switch.func_type}>"
419-
f"<{switch.func}>{state}</{switch.func}>"
420-
f"</{switch.func_type}>"
421-
)
422426
uri = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
423-
if model == "relay" and self.gw_entities[appl_id]["switches"]["lock"]:
427+
if model == "relay" and self.gw_entities[appl_id]["switches"].get("lock"):
424428
# Don't switch a relay when its corresponding lock-state is true
425-
return not requested_state
429+
return current_state
426430

427431
await self.call_request(uri, method="put", data=data)
428432
return requested_state
429433

430434
async def _set_groupswitch_member_state(
431-
self, members: list[str], state: str, switch: Munch
435+
self, appl_id: str, data: str, members: list[str], state: str, switch: Munch
432436
) -> bool:
433437
"""Helper-function for set_switch_state().
434438
435439
Set the given State of the relevant Switch within a group of members.
436440
"""
441+
current_state = self.gw_entities[appl_id]["switches"]["relay"]
437442
requested_state = state == STATE_ON
438443
switched = 0
439444
for member in members:
440445
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
441446
switch_id = self._domain_objects.find(locator).attrib["id"]
442447
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
443-
data = (
444-
f"<{switch.func_type}>"
445-
f"<{switch.func}>{state}</{switch.func}>"
446-
f"</{switch.func_type}>"
447-
)
448448
if not self.gw_entities[member]["switches"].get("lock"):
449449
await self.call_request(uri, method="put", data=data)
450450
switched += 1
451451

452452
if switched > 0:
453453
return requested_state
454454

455-
return not requested_state
455+
return current_state
456456

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

0 commit comments

Comments
 (0)