Skip to content

Commit

Permalink
Add support for blocking and obstacle detection of covers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Gersbacher committed Sep 24, 2024
1 parent 3b29029 commit 38502b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions homepilot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
APICAP_BATTERY_LVL_PCT_MEA = "BATTERY_LVL_PCT_MEA"
APICAP_BATT_VALUE_EVT = "BATT_VALUE_EVT"
APICAP_BATT_LOW_EVT = "BATT_LOW_EVT"
APICAP_BLOCK_DET_EVT = "BLOCK_DET_EVT"
APICAP_OBSTACLE_DET_EVT = "OBSTACLE_DET_EVT"
APICAP_MOTION_DETECTION_MEA = "MOTION_DETECTION_MEA"
APICAP_SMOKE_DETECTION_MEA = "SMOKE_DETECTION_MEA"
# Thermostat
Expand Down
44 changes: 42 additions & 2 deletions homepilot/cover.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
from enum import Enum
from .const import (
APICAP_BLOCK_DET_EVT,
APICAP_DEVICE_TYPE_LOC,
APICAP_GOTO_POS_CMD,
APICAP_ID_DEVICE_LOC,
APICAP_NAME_DEVICE_LOC,
APICAP_OBSTACLE_DET_EVT,
APICAP_PING_CMD,
APICAP_PROD_CODE_DEVICE_LOC,
APICAP_PROT_ID_DEVICE_LOC,
Expand Down Expand Up @@ -36,6 +38,10 @@ class HomePilotCover(HomePilotDevice):
_has_ventilation_position_config: bool
_ventilation_position_mode: bool
_ventilation_position: int
_has_blocking_detection: bool
_blocking_detection_status: bool
_has_obstacle_detection: bool
_obstacle_detection_status: bool

def __init__(
self,
Expand All @@ -53,6 +59,8 @@ def __init__(
has_tilt: bool = False,
can_set_tilt_position: bool = False,
has_ventilation_position_config: bool = False,
has_blocking_detection: bool = False,
has_obstacle_detection: bool = False,
) -> None:
super().__init__(
api=api,
Expand All @@ -70,6 +78,8 @@ def __init__(
self._has_tilt = has_tilt
self._can_set_tilt_position = can_set_tilt_position
self._has_ventilation_position_config = has_ventilation_position_config
self._has_blocking_detection = has_blocking_detection
self._has_obstacle_detection = has_obstacle_detection

@staticmethod
def build_from_api(api: HomePilotApi, did: str):
Expand Down Expand Up @@ -100,6 +110,8 @@ async def async_build_from_api(api: HomePilotApi, did):
has_tilt=APICAP_SET_SLAT_POS_CMD in device_map,
can_set_tilt_position=APICAP_SET_SLAT_POS_CMD in device_map,
has_ventilation_position_config=APICAP_VENTIL_POS_MODE_CFG in device_map,
has_blocking_detection=APICAP_BLOCK_DET_EVT in device_map,
has_obstacle_detection=APICAP_OBSTACLE_DET_EVT in device_map,
)

async def update_state(self, state, api):
Expand All @@ -115,11 +127,15 @@ async def update_state(self, state, api):
self.is_closed = self.cover_position == 0
self.is_closing = False
self.is_opening = False
device = await api.get_device(self.did)
device_map = HomePilotDevice.get_capabilities_map(device)
if self.has_ventilation_position_config:
device = await api.get_device(self.did)
device_map = HomePilotDevice.get_capabilities_map(device)
self.ventilation_position_mode = device_map[APICAP_VENTIL_POS_MODE_CFG]["value"] == "true"
self.ventilation_position = 100 - int(device_map[APICAP_VENTIL_POS_CFG]["value"])
if self.has_blocking_detection:
self.blocking_detection_status = device_map[APICAP_BLOCK_DET_EVT]["value"] == "true"
if self.has_obstacle_detection:
self.obstacle_detection_status = device_map[APICAP_OBSTACLE_DET_EVT]["value"] == "true"

async def async_open_cover(self) -> None:
await self.api.async_open_cover(self.did)
Expand Down Expand Up @@ -247,3 +263,27 @@ def ventilation_position(self) -> int:
@ventilation_position.setter
def ventilation_position(self, ventilation_position):
self._ventilation_position = ventilation_position

@property
def has_blocking_detection(self) -> bool:
return self._has_blocking_detection

@property
def blocking_detection_status(self) -> bool:
return self._blocking_detection_status

@blocking_detection_status.setter
def blocking_detection_status(self, blocking_detection_status):
self._blocking_detection_status = blocking_detection_status

@property
def has_obstacle_detection(self) -> bool:
return self._has_obstacle_detection

@property
def obstacle_detection_status(self) -> bool:
return self._obstacle_detection_status

@obstacle_detection_status.setter
def obstacle_detection_status(self, obstacle_detection_status):
self._obstacle_detection_status = obstacle_detection_status
6 changes: 6 additions & 0 deletions tests/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ async def test_build_from_api(self, mocked_api):
assert cover.model == "RolloTron radio beltwinder"
assert cover.has_ping_cmd is True
assert cover.can_set_position is True
assert cover.has_blocking_detection is True
assert cover.has_obstacle_detection is True

@pytest.mark.asyncio
async def test_update_state(self, mocked_api):
Expand All @@ -62,6 +64,8 @@ async def test_update_state(self, mocked_api):
assert cover.is_closing is False
assert cover.is_opening is False
assert cover.available is True
assert cover.blocking_detection_status is False
assert cover.obstacle_detection_status is False

await cover.update_state({
"statusesMap": {
Expand All @@ -76,6 +80,8 @@ async def test_update_state(self, mocked_api):
assert cover.is_closing is False
assert cover.is_opening is False
assert cover.available is False
assert cover.blocking_detection_status is False
assert cover.obstacle_detection_status is False

@pytest.mark.asyncio
async def test_async_open_cover(self, mocked_api):
Expand Down

0 comments on commit 38502b9

Please sign in to comment.