Skip to content

Commit

Permalink
Expose device_type property on switches (#49)
Browse files Browse the repository at this point in the history
* Expose device_type property on switches

* Clarify duplicate device type

Source of information: myStrom support
  • Loading branch information
dbrgn authored Dec 25, 2024
1 parent 09f21ed commit 0d62b65
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## next release

- None changes
- Expose `device_type` property on switches

## 2.2.0 (2023-05-21)

Expand Down
1 change: 1 addition & 0 deletions examples/example-switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async def main():
# Collect the data of the current state
await switch.get_state()

print("Device type:", switch.device_type)
print("Power consumption:", switch.consumption)
print("Energy consumed:", switch.consumedWs)
print("Relay state:", switch.relay)
Expand Down
33 changes: 33 additions & 0 deletions pymystrom/device_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Device types.
See https://api.mystrom.ch/#f37a4be7-0233-4d93-915e-c6f92656f129
"""

DEVICE_MAPPING_NUMERIC = {
101: "Switch CH v1",
102: "Bulb",
103: "Button+",
104: "Button",
105: "LED Strip",
106: "Switch CH v2",
107: "Switch EU",
110: "Motion Sensor",
113: "modulo® STECCO / CUBO",
118: "Button Plus 2nd",
120: "Switch Zero",
}

DEVICE_MAPPING_LITERAL = {
"WSW": DEVICE_MAPPING_NUMERIC[101],
"WRB": DEVICE_MAPPING_NUMERIC[102],
"WBP": DEVICE_MAPPING_NUMERIC[103],
"WBS": DEVICE_MAPPING_NUMERIC[104],
"WRS": DEVICE_MAPPING_NUMERIC[105],
"WS2": DEVICE_MAPPING_NUMERIC[106],
"WSE": DEVICE_MAPPING_NUMERIC[107],
"WMS": DEVICE_MAPPING_NUMERIC[110],
"WLL": DEVICE_MAPPING_NUMERIC[113],
"BP2": DEVICE_MAPPING_NUMERIC[118],
"LCS": DEVICE_MAPPING_NUMERIC[120],
}
17 changes: 3 additions & 14 deletions pymystrom/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@
import logging
from typing import Optional, List

_LOGGER = logging.getLogger(__name__)

DEVICE_MAPPING = {
"101": "myStrom Switch v1",
"102": "myStrom Bulb",
"103": "myStrom Button+",
"104": "myStrom Button",
"105": "myStrom LED strip",
"106": "myStzrom Switch v2",
"107": "myStrom Switch EU",
"110": "myStrom Motion sensor",
"120": "myStrom Switch Zero",
}
from .device_types import DEVICE_MAPPING_NUMERIC

_LOGGER = logging.getLogger(__name__)

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

if device.type == "102":
device.hardware = DEVICE_MAPPING[str(announce_msg[6])]
device.hardware = DEVICE_MAPPING_NUMERIC[int(announce_msg[6])]
else:
device.hardware = "non_mystrom"
status = announce_msg[7]
Expand Down
14 changes: 13 additions & 1 deletion pymystrom/switch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Support for communicating with myStrom plugs/switches."""
import aiohttp
from yarl import URL
from typing import Optional
from typing import Optional, Union

from . import _request as request
from .device_types import DEVICE_MAPPING_NUMERIC, DEVICE_MAPPING_LITERAL


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

async def turn_on(self) -> None:
Expand Down Expand Up @@ -70,6 +72,16 @@ async def get_state(self) -> None:

self._firmware = response["version"]
self._mac = response["mac"]
self._device_type = response["type"]

@property
def device_type(self) -> Optional[str]:
"""Return the device type as string (e.g. "Switch CH v1" or "Button+")."""
if isinstance(self._device_type, int):
return DEVICE_MAPPING_NUMERIC.get(self._device_type)
elif isinstance(self._device_type, str):
return DEVICE_MAPPING_LITERAL.get(self._device_type)
return None

@property
def relay(self) -> bool:
Expand Down

0 comments on commit 0d62b65

Please sign in to comment.