From 8db0d3b2cdd626b4879e43fc53204b142ba29e0d Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 18 Jun 2021 17:39:02 +0200 Subject: [PATCH 1/3] Working prototype --- pydeconz/__main__.py | 11 +++++++++++ pydeconz/device.py | 38 ++++++++++++++++++++++++++++++++++++++ pydeconz/gateway.py | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 pydeconz/device.py diff --git a/pydeconz/__main__.py b/pydeconz/__main__.py index 091e2fd1..106cee13 100644 --- a/pydeconz/__main__.py +++ b/pydeconz/__main__.py @@ -67,6 +67,17 @@ async def main(host: str, port: int, api_key: str) -> None: await gateway.refresh_state() gateway.start() + ##### + from pprint import pprint + + model_unique_ids = { + sensor.modelid: sensor.uniqueid + for sensor in gateway.sensors.values() + if sensor.type == "ZHASwitch" + } + button_events = await gateway.devices.introspect_button_event(model_unique_ids) + pprint(button_events) + ##### try: while True: await asyncio.sleep(1) diff --git a/pydeconz/device.py b/pydeconz/device.py new file mode 100644 index 00000000..702ae8e8 --- /dev/null +++ b/pydeconz/device.py @@ -0,0 +1,38 @@ +"""Expose device and introspection capabilities from deCONZ.""" + +import logging +from typing import Callable, Dict, Optional, Tuple, Union + +from .api import APIItem, APIItems + +# from .deconzdevice import DeconzDevice + +LOGGER = logging.getLogger(__name__) +# RESOURCE_TYPE = "devices" +URL = "/devices" + + +class Device(APIItem): + pass + + +class Devices(APIItems): + """Represent deCONZ devices.""" + + def __init__( + self, + raw: dict, + request: Callable[..., Optional[dict]], + ) -> None: + """Initialize device manager.""" + super().__init__(raw, request, URL, Device) + + async def introspect_button_event(self, model_unique_ids: Dict[str, str]) -> dict: + """Introspect button event for unique ID.""" + button_events = {} + for model_id, unique_id in model_unique_ids.items(): + path = f"/{URL}/{unique_id}/state/buttonevent/introspect" + raw = await self._request("get", path) + button_events[model_id] = raw + + return button_events diff --git a/pydeconz/gateway.py b/pydeconz/gateway.py index ffd1a4a8..1b59bfc3 100644 --- a/pydeconz/gateway.py +++ b/pydeconz/gateway.py @@ -11,6 +11,7 @@ from .alarm_system import RESOURCE_TYPE as ALARM_SYSTEM_RESOURCE, AlarmSystems from .config import RESOURCE_TYPE as CONFIG_RESOURCE, Config +from .device import Devices from .errors import RequestError, ResponseError, raise_error from .group import RESOURCE_TYPE as GROUP_RESOURCE, DeconzScene, Groups from .light import RESOURCE_TYPE as LIGHT_RESOURCE, Light, Lights @@ -67,6 +68,7 @@ def __init__( self.alarmsystems = AlarmSystems({}, self.request) self.config: Config | None = None + self.devices = Devices({}, self.request) self.groups = Groups({}, self.request) self.lights = Lights({}, self.request) self.scenes: dict[str, DeconzScene] = {} From 88415ca990ad63c4d41efbfd741a53bc409173d8 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Mon, 21 Jun 2021 20:53:56 +0200 Subject: [PATCH 2/3] Define global constants for button names and actions --- pydeconz/device.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pydeconz/device.py b/pydeconz/device.py index 702ae8e8..8bb1a343 100644 --- a/pydeconz/device.py +++ b/pydeconz/device.py @@ -11,6 +11,26 @@ # RESOURCE_TYPE = "devices" URL = "/devices" +BUTTON_ACTION_INITIAL_PRESS = "INITIAL_PRESS" +BUTTON_ACTION_SHORT_RELEASE = "SHORT_RELEASE" +BUTTON_ACTION_DOUBLE_PRESS = "DOUBLE_PRESS" +BUTTON_ACTION_TREBLE_PRESS = "TREBLE_PRESS" +BUTTON_ACTION_HOLD = "HOLD" +BUTTON_ACTION_LONG_RELEASE = "LONG_RELEASE" + +BUTTON_NAME_BUTTON_1 = "Button 1" +BUTTON_NAME_CLOSE = "Close" +BUTTON_NAME_DIM_DOWN = "Dim Down" +BUTTON_NAME_DIM_UP = "Dim Up" +BUTTON_NAME_NEXT = "Next Scene" +BUTTON_NAME_OFF = "Off" +BUTTON_NAME_ON = "On" +BUTTON_NAME_ONOFF = "On/OFF" +BUTTON_NAME_OPEN = "Open" +BUTTON_NAME_PREVIOUS = "Previous Scene" +BUTTON_NAME_ROTATE_CLOCKWISE = "Rotate clockwise" +BUTTON_NAME_ROTATE_COUNTER_CLOCKWISE = "Rotate counter clockwise" + class Device(APIItem): pass @@ -30,6 +50,7 @@ def __init__( async def introspect_button_event(self, model_unique_ids: Dict[str, str]) -> dict: """Introspect button event for unique ID.""" button_events = {} + for model_id, unique_id in model_unique_ids.items(): path = f"/{URL}/{unique_id}/state/buttonevent/introspect" raw = await self._request("get", path) From 049b849862766bda633ebd78fe400a69b890de43 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 18 Sep 2021 09:20:55 +0200 Subject: [PATCH 3/3] Add test file --- tests/test_devices.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/test_devices.py diff --git a/tests/test_devices.py b/tests/test_devices.py new file mode 100644 index 00000000..e69de29b