Skip to content

Commit fdd8f1c

Browse files
authored
Merge pull request #706 from plugwise/fix-issue-833
Fix for plugwise-beta issue 833
2 parents e4e3a50 + 6a30c51 commit fdd8f1c

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## v.1.7.1
3+
## v1.7.2
4+
5+
- Bugfix for Plugwise-beta issue [833](https://github.com/plugwise/plugwise-beta/issues/833) solving relay- and lock-switches not switching for the Stretch.
6+
7+
## v1.7.1
48

59
- Avoid None-init for smile_version [#699](https://github.com/plugwise/python-plugwise/pull/699)
610
- Replace string.split() by string.partition() [#702](https://github.com/plugwise/python-plugwise/pull/702)

plugwise/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ async def _smile_detect_legacy(
296296
):
297297
system = await self._request(SYSTEM)
298298
self.smile_version = parse(system.find("./gateway/firmware").text)
299-
return_model = system.find("./gateway/product").text
299+
return_model = str(system.find("./gateway/product").text)
300300
self.smile_hostname = system.find("./gateway/hostname").text
301301
# If wlan0 contains data it's active, so eth0 should be checked last
302302
for network in ("wlan0", "eth0"):
@@ -307,7 +307,7 @@ async def _smile_detect_legacy(
307307
elif dsmrmain is not None:
308308
status = await self._request(STATUS)
309309
self.smile_version = parse(status.find("./system/version").text)
310-
return_model = status.find("./system/product").text
310+
return_model = str(status.find("./system/product").text)
311311
self.smile_hostname = status.find("./network/hostname").text
312312
self.smile_mac_address = status.find("./network/mac_address").text
313313
else: # pragma: no cover

plugwise/legacy/smile.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,50 @@ async def set_schedule_state(
231231
async def set_switch_state(
232232
self, appl_id: str, members: list[str] | None, model: str, state: str
233233
) -> None:
234-
"""Set the given State of the relevant Switch."""
234+
"""Set the given state of the relevant switch.
235+
236+
For individual switches, sets the state directly.
237+
For group switches, sets the state for each member in the group separately.
238+
For switch-locks, sets the lock state using a different data format.
239+
"""
235240
switch = Munch()
236241
switch.actuator = "actuator_functionalities"
237242
switch.func_type = "relay_functionality"
238243
if self._stretch_v2:
239244
switch.actuator = "actuators"
240245
switch.func_type = "relay"
241-
switch.func = "state"
242246

247+
# Handle switch-lock
248+
if model == "lock":
249+
state = "false" if state == "off" else "true"
250+
appliance = self._appliances.find(f'appliance[@id="{appl_id}"]')
251+
appl_name = appliance.find("name").text
252+
appl_type = appliance.find("type").text
253+
data = f'''
254+
<appliances>
255+
<appliance id="{appl_id}">
256+
<name><![CDATA[{appl_name}]]></name>
257+
<description><![CDATA[]]></description>
258+
<type><![CDATA[{appl_type}]]></type>
259+
<{switch.actuator}>
260+
<{switch.func_type}>
261+
<lock>{state}</lock>
262+
</{switch.func_type}>
263+
</{switch.actuator}>
264+
</appliance>
265+
</appliances>'''.strip()
266+
await self.call_request(APPLIANCES, method="post", data=data)
267+
return
268+
269+
# Handle group of switches
270+
data = f"<{switch.func_type}><state>{state}</state></{switch.func_type}>"
243271
if members is not None:
244-
return await self._set_groupswitch_member_state(members, state, switch)
245-
246-
data = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
247-
uri = f"{APPLIANCES};id={appl_id}/{switch.func_type}"
272+
return await self._set_groupswitch_member_state(
273+
data, members, state, switch
274+
)
248275

276+
# Handle individual relay switches
277+
uri = f"{APPLIANCES};id={appl_id}/relay"
249278
if model == "relay":
250279
locator = (
251280
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}/lock'
@@ -257,16 +286,14 @@ async def set_switch_state(
257286
await self.call_request(uri, method="put", data=data)
258287

259288
async def _set_groupswitch_member_state(
260-
self, members: list[str], state: str, switch: Munch
289+
self, data: str, members: list[str], state: str, switch: Munch
261290
) -> None:
262291
"""Helper-function for set_switch_state().
263292
264-
Set the given State of the relevant Switch within a group of members.
293+
Set the given State of the relevant Switch (relay) within a group of members.
265294
"""
266295
for member in members:
267-
uri = f"{APPLIANCES};id={member}/{switch.func_type}"
268-
data = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
269-
296+
uri = f"{APPLIANCES};id={member}/relay"
270297
await self.call_request(uri, method="put", data=data)
271298

272299
async def set_temperature(self, _: str, items: dict[str, float]) -> None:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "plugwise"
7-
version = "1.7.1"
7+
version = "1.7.2"
88
license = {file = "LICENSE"}
99
description = "Plugwise Smile (Adam/Anna/P1) and Stretch module for Python 3."
1010
readme = "README.md"

tests/test_init.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ async def setup_legacy_app(
167167
"PUT", CORE_APPLIANCES_TAIL, self.smile_http_accept
168168
)
169169
else:
170+
app.router.add_route("POST", CORE_APPLIANCES_TAIL, self.smile_http_ok)
170171
app.router.add_route("PUT", CORE_APPLIANCES_TAIL, self.smile_http_ok)
171172
else:
173+
app.router.add_route("POST", CORE_APPLIANCES_TAIL, self.smile_timeout)
172174
app.router.add_route("PUT", CORE_LOCATIONS_TAIL, self.smile_timeout)
173175
app.router.add_route("PUT", CORE_RULES_TAIL, self.smile_timeout)
174176
app.router.add_route("PUT", CORE_APPLIANCES_TAIL, self.smile_timeout)

tests/test_legacy_stretch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ async def test_connect_stretch_v23(self):
7474
smile, "2587a7fcdd7e482dab03fda256076b4b"
7575
)
7676
assert switch_change
77+
switch_change = await self.tinker_switch(
78+
smile, "2587a7fcdd7e482dab03fda256076b4b", model="lock"
79+
)
80+
assert switch_change
7781
switch_change = await self.tinker_switch(
7882
smile,
7983
"f7b145c8492f4dd7a4de760456fdef3e",

0 commit comments

Comments
 (0)