From 0749f72c6083a2bc6ab3b9ff9cde51d353e6ff15 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Wed, 31 Jul 2024 04:13:55 +0200 Subject: [PATCH] Implementing more bridges --- example.conf.yaml | 6 +-- src/mash/bridges/hue.py | 77 ++++++++++++++++++++++++++++++ src/mash/bridges/openweatherapi.py | 0 src/mash/bridges/peoplecounter.py | 0 src/mash/bridges/restapi.py | 0 src/mash/bridges/spotify.py | 0 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/mash/bridges/openweatherapi.py create mode 100644 src/mash/bridges/peoplecounter.py create mode 100644 src/mash/bridges/restapi.py create mode 100644 src/mash/bridges/spotify.py diff --git a/example.conf.yaml b/example.conf.yaml index ae2cc6d..18fc03a 100644 --- a/example.conf.yaml +++ b/example.conf.yaml @@ -1,15 +1,15 @@ database: mash_database.sqlite bridges: - - id: &hue hue + - &hue id: hue type: hue ip: 192.168.178.23 - - id: &z2m z2m + - &z2m id: z2m type: zigbee2mqtt ip: 192.168.178.115 port: 1883 topic: zigbee2mqtt - - id: &fritz fritz + - &fritz id: fritz type: fritzbox ip: 192.168.178.1 diff --git a/src/mash/bridges/hue.py b/src/mash/bridges/hue.py index e69de29..85b1e07 100644 --- a/src/mash/bridges/hue.py +++ b/src/mash/bridges/hue.py @@ -0,0 +1,77 @@ +import logging +from mash.bridges.bridge import Bridge +from phue import Bridge as phueBridge +from time import sleep + + +class HueBridge(Bridge): + + def __init__( + self, + *, + id: str, + ip: str, + retry_limit: int = 10, + retry_timeout_seconds: int = 5, + ) -> None: + super().__init__(id=id, type="hue") + self._retry_limit = retry_limit + self._retry_timeout_seconds = retry_timeout_seconds + self._hue: phueBridge = phueBridge(ip) + + def disconnect(self) -> None: + self._hue = None + logging.info(f"Disconnected from Hue Bridge [{self.id}].") + + def connect(self) -> None: + for _ in range(self._retry_limit): + try: + self._hue.connect() + break + except Exception as e: + logging.exception( + f"Failed to connect to Hue bridge [ip {self._hue.ip}]. Retrying in [{self._retry_timeout_seconds}] seconds.", + exc_info=e, + ) + sleep(self._retry_timeout_seconds) + + logging.info(f"Connected to Hue Bridge [{self.id}].") + + @property + def is_connected(self) -> bool | None: + return self._hue is not None + + def list_api(self) -> dict: + return self._hue.get_api() + + def list_scenes(self) -> dict: + return self._hue.get_scene() + + def get_scene_by_name(self, name) -> dict | None: + for key, scene in self.list_scenes().items(): + if scene["name"] == name: + scene["id"] = key + return scene + return None + + def in_room_activate_scene(self, room_name: str, scene_name: str): + """Activate a scene in a room. + + Args: + scene (str): The name of the scene to activate. + room (str): The name of the room to activate the scene in. + """ + scene_id = self.get_scene_by_name(scene_name)["id"] + if scene_id is None: + raise "Scene not found." + + self._hue.set_group(room_name, {"scene": scene_id}) + + def in_room_set_lights_active(self, room_name: str, active: bool): + """Deactivate all lights in a room. + + Args: + room_name (str): The name of the room to deactivate the lights in. + active (bool): True, to turn all lights on, or False to turn them off. + """ + self._hue.set_group(room_name, {"on": active}) diff --git a/src/mash/bridges/openweatherapi.py b/src/mash/bridges/openweatherapi.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mash/bridges/peoplecounter.py b/src/mash/bridges/peoplecounter.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mash/bridges/restapi.py b/src/mash/bridges/restapi.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mash/bridges/spotify.py b/src/mash/bridges/spotify.py new file mode 100644 index 0000000..e69de29