Implementing more bridges
This commit is contained in:
parent
c1a5b61e30
commit
0749f72c60
6 changed files with 80 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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})
|
0
src/mash/bridges/openweatherapi.py
Normal file
0
src/mash/bridges/openweatherapi.py
Normal file
0
src/mash/bridges/peoplecounter.py
Normal file
0
src/mash/bridges/peoplecounter.py
Normal file
0
src/mash/bridges/restapi.py
Normal file
0
src/mash/bridges/restapi.py
Normal file
0
src/mash/bridges/spotify.py
Normal file
0
src/mash/bridges/spotify.py
Normal file
Loading…
Reference in a new issue