Skip to content

Commit 0d62b65

Browse files
authored
Expose device_type property on switches (#49)
* Expose device_type property on switches * Clarify duplicate device type Source of information: myStrom support
1 parent 09f21ed commit 0d62b65

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## next release
44

5-
- None changes
5+
- Expose `device_type` property on switches
66

77
## 2.2.0 (2023-05-21)
88

examples/example-switch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async def main():
1212
# Collect the data of the current state
1313
await switch.get_state()
1414

15+
print("Device type:", switch.device_type)
1516
print("Power consumption:", switch.consumption)
1617
print("Energy consumed:", switch.consumedWs)
1718
print("Relay state:", switch.relay)

pymystrom/device_types.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Device types.
3+
4+
See https://api.mystrom.ch/#f37a4be7-0233-4d93-915e-c6f92656f129
5+
"""
6+
7+
DEVICE_MAPPING_NUMERIC = {
8+
101: "Switch CH v1",
9+
102: "Bulb",
10+
103: "Button+",
11+
104: "Button",
12+
105: "LED Strip",
13+
106: "Switch CH v2",
14+
107: "Switch EU",
15+
110: "Motion Sensor",
16+
113: "modulo® STECCO / CUBO",
17+
118: "Button Plus 2nd",
18+
120: "Switch Zero",
19+
}
20+
21+
DEVICE_MAPPING_LITERAL = {
22+
"WSW": DEVICE_MAPPING_NUMERIC[101],
23+
"WRB": DEVICE_MAPPING_NUMERIC[102],
24+
"WBP": DEVICE_MAPPING_NUMERIC[103],
25+
"WBS": DEVICE_MAPPING_NUMERIC[104],
26+
"WRS": DEVICE_MAPPING_NUMERIC[105],
27+
"WS2": DEVICE_MAPPING_NUMERIC[106],
28+
"WSE": DEVICE_MAPPING_NUMERIC[107],
29+
"WMS": DEVICE_MAPPING_NUMERIC[110],
30+
"WLL": DEVICE_MAPPING_NUMERIC[113],
31+
"BP2": DEVICE_MAPPING_NUMERIC[118],
32+
"LCS": DEVICE_MAPPING_NUMERIC[120],
33+
}

pymystrom/discovery.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,9 @@
33
import logging
44
from typing import Optional, List
55

6-
_LOGGER = logging.getLogger(__name__)
7-
8-
DEVICE_MAPPING = {
9-
"101": "myStrom Switch v1",
10-
"102": "myStrom Bulb",
11-
"103": "myStrom Button+",
12-
"104": "myStrom Button",
13-
"105": "myStrom LED strip",
14-
"106": "myStzrom Switch v2",
15-
"107": "myStrom Switch EU",
16-
"110": "myStrom Motion sensor",
17-
"120": "myStrom Switch Zero",
18-
}
6+
from .device_types import DEVICE_MAPPING_NUMERIC
197

8+
_LOGGER = logging.getLogger(__name__)
209

2110
class DiscoveredDevice(object):
2211
"""Representation of discovered device."""
@@ -39,7 +28,7 @@ def create_from_announce_msg(raw_addr, announce_msg):
3928
device.type = announce_msg[6]
4029

4130
if device.type == "102":
42-
device.hardware = DEVICE_MAPPING[str(announce_msg[6])]
31+
device.hardware = DEVICE_MAPPING_NUMERIC[int(announce_msg[6])]
4332
else:
4433
device.hardware = "non_mystrom"
4534
status = announce_msg[7]

pymystrom/switch.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Support for communicating with myStrom plugs/switches."""
22
import aiohttp
33
from yarl import URL
4-
from typing import Optional
4+
from typing import Optional, Union
55

66
from . import _request as request
7+
from .device_types import DEVICE_MAPPING_NUMERIC, DEVICE_MAPPING_LITERAL
78

89

910
class MyStromSwitch:
@@ -20,6 +21,7 @@ def __init__(self, host: str, session: aiohttp.client.ClientSession = None) -> N
2021
self._temperature = None
2122
self._firmware = None
2223
self._mac = None
24+
self._device_type: Optional[Union[str, int]] = None
2325
self.uri = URL.build(scheme="http", host=self._host)
2426

2527
async def turn_on(self) -> None:
@@ -70,6 +72,16 @@ async def get_state(self) -> None:
7072

7173
self._firmware = response["version"]
7274
self._mac = response["mac"]
75+
self._device_type = response["type"]
76+
77+
@property
78+
def device_type(self) -> Optional[str]:
79+
"""Return the device type as string (e.g. "Switch CH v1" or "Button+")."""
80+
if isinstance(self._device_type, int):
81+
return DEVICE_MAPPING_NUMERIC.get(self._device_type)
82+
elif isinstance(self._device_type, str):
83+
return DEVICE_MAPPING_LITERAL.get(self._device_type)
84+
return None
7385

7486
@property
7587
def relay(self) -> bool:

0 commit comments

Comments
 (0)