diff --git a/homepilot/api.py b/homepilot/api.py index cee23e3..d11661c 100644 --- a/homepilot/api.py +++ b/homepilot/api.py @@ -403,6 +403,46 @@ async def async_update_firmware(self): ) as response: return await response.json() +# Scenes + async def async_get_scenes(self): + await self.authenticate() + async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session: + async with session.get( + f"http://{self.host}/scenes" + ) as response: + if response.status == 401: + raise AuthError() + if response.status == 200: + responseJson = await response.json() + if "scenes" in responseJson: + scenes = responseJson["scenes"] + return scenes + return [] + + async def async_get_scenes_v4(self): + await self.authenticate() + async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session: + async with session.get( + f"http://{self.host}/v4/scenes" + ) as response: + if response.status == 401: + raise AuthError() + if response.status == 200: + responseJson = await response.json() + if "scenes" in responseJson: + scenes = responseJson["scenes"] + return scenes + return [] + + async def async_execute_scene(self, sid: int): + await self.authenticate() + async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session: + async with session.post( + f"http://{self.host}/scenes/{sid}/actions", + json={ "request_type": "EXECUTESCENE", "trigger_event": "TRIGGER_SCENE_MANUALLY_EVT" } + ) as response: + return await response.json() + @property def host(self): return self._host diff --git a/homepilot/manager.py b/homepilot/manager.py index 535f5fb..85873b1 100644 --- a/homepilot/manager.py +++ b/homepilot/manager.py @@ -10,6 +10,7 @@ from .actuator import HomePilotActuator from .api import HomePilotApi, AuthError from .wallcontroller import HomePilotWallController +from .scenes import HomePilotScene from .device import HomePilotDevice @@ -19,6 +20,7 @@ class HomePilotManager: _api: HomePilotApi _devices: Dict[str, HomePilotDevice] + _scenes: Dict[str, HomePilotScene] def __init__(self, api: HomePilotApi) -> None: self._api = api @@ -35,6 +37,14 @@ async def async_build_manager(api: HomePilotApi): for id_type in await manager.get_device_ids_types() if id_type["type"] in ["-1", "1", "2", "3", "4", "5", "8", "10"] } + try: + manager.scenes = { + scene["id"]: HomePilotScene(manager.api, scene["id"], scene["name"], scene["description"]) + for scene in await manager.api.async_get_scenes() + if scene["is_manual_executable"] == 1 + } + except Exception(): + manager.scenes = {} return manager @staticmethod diff --git a/homepilot/scenes.py b/homepilot/scenes.py new file mode 100644 index 0000000..1bb5ac3 --- /dev/null +++ b/homepilot/scenes.py @@ -0,0 +1,40 @@ +from .api import HomePilotApi + +class HomePilotScene: + """HomePilot Scene""" + + _api: HomePilotApi + _sid: int + _name: str + _description: str + + def __init__( + self, + api: HomePilotApi, + sid: int, + name: str, + description: str, + ) -> None: + self._api = api + self._sid = sid + self._name = name + self._description = description + + async def async_execute_scene(self) -> None: + await self.api.async_execute_scene(self.sid) + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + self._name = name + + @property + def description(self): + return self._description + + @description.setter + def description(self, description): + self._description = description