Skip to content

Commit

Permalink
Merge pull request #50 from peribeir/dev
Browse files Browse the repository at this point in the history
Add support for scenes
  • Loading branch information
peribeir authored Nov 26, 2023
2 parents 5262a1f + 4a418cd commit 010a332
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
40 changes: 40 additions & 0 deletions homepilot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions homepilot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions homepilot/scenes.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 010a332

Please sign in to comment.