Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Add boot_id, energy_since_boot and time_since_boot to switch (thanks @slyoldfox)
- Expose `device_type` property on switches
- Fix: Tolerate missing 'type' in device info for legacy myStrom v1 switches, preventing KeyError during get_state (related to home-assistant/core#150264)


## 2.2.0 (2023-05-21)

Expand Down
7 changes: 4 additions & 3 deletions pymystrom/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ async def get_state(self) -> None:
url = URL(self.uri).join(URL("info.json"))
response = await request(self, uri=url)

self._firmware = response["version"]
self._mac = response["mac"]
self._device_type = response["type"]
# Tolerate missing keys on legacy firmware (e.g., v1 devices)
self._firmware = response.get("version")
self._mac = response.get("mac")
self._device_type = response.get("type")

@property
def device_type(self) -> Optional[str]:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_switch_missing_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio

from pymystrom.switch import MyStromSwitch
import pymystrom.switch as switch_module


async def _fake_request(self, uri, method="GET", data=None, json_data=None, params=None):
uri_str = str(uri)
if uri_str.endswith("/report"):
return {"relay": True, "power": 1.23, "Ws": 0.5}
if uri_str.endswith("/api/v1/info"):
# Legacy v1 firmware without 'type'
return {"version": "2.68.10", "mac": "AA:BB:CC:DD:EE:FF"}
if uri_str.endswith("/info.json"):
return {"version": "2.68.10", "mac": "AA:BB:CC:DD:EE:FF"}
return {}


def test_get_state_missing_type():
# Patch the request function used by MyStromSwitch
original_request = switch_module.request
switch_module.request = _fake_request
try:
sw = MyStromSwitch("127.0.0.1")
asyncio.run(sw.get_state())

assert sw.relay is True
assert sw.consumption == 1.2
assert sw.consumedWs == 0.5
assert sw.firmware == "2.68.10"
assert sw.mac == "AA:BB:CC:DD:EE:FF"
# Missing 'type' should be tolerated and map to None
assert sw.device_type is None
finally:
# Restore original request function
switch_module.request = original_request

Loading