Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for actuators #23

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions homepilot/actuator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import asyncio
from .const import (
APICAP_DEVICE_TYPE_LOC,
APICAP_ID_DEVICE_LOC,
APICAP_NAME_DEVICE_LOC,
APICAP_PING_CMD,
APICAP_PROD_CODE_DEVICE_LOC,
APICAP_PROT_ID_DEVICE_LOC,
APICAP_VERSION_CFG,
SUPPORTED_DEVICES,
)
from .api import HomePilotApi
from .device import HomePilotDevice


class HomePilotActuator(HomePilotDevice):
_is_on: bool
_brightness: int

def __init__(
self,
api: HomePilotApi,
did: int,
uid: str,
name: str,
device_number: str,
model: str,
fw_version: str,
device_group: int,
has_ping_cmd: bool = False,
) -> None:
super().__init__(
api=api,
did=did,
uid=uid,
name=name,
device_number=device_number,
model=model,
fw_version=fw_version,
device_group=device_group,
has_ping_cmd=has_ping_cmd,
)

@staticmethod
def build_from_api(api: HomePilotApi, did: str):
return asyncio.run(HomePilotActuator.async_build_from_api(api, did))

@staticmethod
async def async_build_from_api(api: HomePilotApi, did):
"""Build a new HomePilotDevice from the response of API"""
device = await api.get_device(did)
device_map = HomePilotDevice.get_capabilities_map(device)
return HomePilotActuator(
api=api,
did=device_map[APICAP_ID_DEVICE_LOC]["value"],
uid=device_map[APICAP_PROT_ID_DEVICE_LOC]["value"],
name=device_map[APICAP_NAME_DEVICE_LOC]["value"],
device_number=device_map[APICAP_PROD_CODE_DEVICE_LOC]["value"],
model=SUPPORTED_DEVICES[device_map[APICAP_PROD_CODE_DEVICE_LOC]["value"]][
"name"
]
if device_map[APICAP_PROD_CODE_DEVICE_LOC]["value"] in SUPPORTED_DEVICES
else "Generic Device",
fw_version=device_map[APICAP_VERSION_CFG]["value"]
if APICAP_VERSION_CFG in device_map else "",
device_group=device_map[APICAP_DEVICE_TYPE_LOC]["value"],
has_ping_cmd=APICAP_PING_CMD in device_map,
)

def update_state(self, state):
super().update_state(state)
self.is_on = state["statusesMap"]["Position"] != 0
self.brightness = state["statusesMap"]["Position"]

@property
def is_on(self) -> bool:
return self._is_on

@is_on.setter
def is_on(self, is_on):
self._is_on = is_on

@property
def brightness(self) -> bool:
return self._brightness

@brightness.setter
def is_on(self, brightness):
self._brightness = brightness

async def async_turn_on(self) -> None:
await self.api.async_turn_on(self.did)

async def async_turn_off(self) -> None:
await self.api.async_turn_off(self.did)

async def async_set_brightness(self, new_brightness) -> None:
await self.api.async_set_cover_position(self.did, new_brightness)

async def async_toggle(self) -> None:
if self.is_on:
await self.async_turn_off()
else:
await self.async_turn_on()
5 changes: 4 additions & 1 deletion homepilot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .switch import HomePilotSwitch
from .cover import HomePilotCover
from .thermostat import HomePilotThermostat
from .actuator import HomePilotActuator
from .api import HomePilotApi

from .device import HomePilotDevice
Expand All @@ -31,7 +32,7 @@ async def async_build_manager(api: HomePilotApi):
manager.devices = {
id_type["did"]: await HomePilotManager.async_build_device(manager.api, id_type)
for id_type in await manager.get_device_ids_types()
if id_type["type"] in ["-1", "1", "2", "3", "5", "8"]
if id_type["type"] in ["-1", "1", "2", "3", "4", "5", "8"]
}
return manager

Expand All @@ -45,6 +46,8 @@ async def async_build_device(api, id_type):
return await HomePilotCover.async_build_from_api(api, id_type["did"])
if id_type["type"] == "3":
return await HomePilotSensor.async_build_from_api(api, id_type["did"])
if id_type["type"] == "4":
return await HomePilotActuator.async_build_from_api(api, id_type["did"])
if id_type["type"] == "5":
return await HomePilotThermostat.async_build_from_api(api, id_type["did"])
if id_type["type"] == "8":
Expand Down