Implementing more bridges

This commit is contained in:
Maximilian Giller 2024-07-31 04:13:55 +02:00
parent c1a5b61e30
commit 0749f72c60
6 changed files with 80 additions and 3 deletions

View file

@ -1,15 +1,15 @@
database: mash_database.sqlite database: mash_database.sqlite
bridges: bridges:
- id: &hue hue - &hue id: hue
type: hue type: hue
ip: 192.168.178.23 ip: 192.168.178.23
- id: &z2m z2m - &z2m id: z2m
type: zigbee2mqtt type: zigbee2mqtt
ip: 192.168.178.115 ip: 192.168.178.115
port: 1883 port: 1883
topic: zigbee2mqtt topic: zigbee2mqtt
- id: &fritz fritz - &fritz id: fritz
type: fritzbox type: fritzbox
ip: 192.168.178.1 ip: 192.168.178.1

View file

@ -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})

View file

View file

View file

View file