From 88455a8a8b3b4a3255e04b44fa34be3e941b62e3 Mon Sep 17 00:00:00 2001 From: Jc2k Date: Wed, 17 Apr 2019 18:02:04 +0100 Subject: [PATCH] homekit_controller: Support cover stop (#23046) --- .../components/homekit_controller/cover.py | 45 ++++++++++--------- .../homekit_controller/test_cover.py | 12 +++++ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/homekit_controller/cover.py b/homeassistant/components/homekit_controller/cover.py index 0c9ce2bc528..bd466d074d0 100644 --- a/homeassistant/components/homekit_controller/cover.py +++ b/homeassistant/components/homekit_controller/cover.py @@ -3,7 +3,7 @@ import logging from homeassistant.components.cover import ( ATTR_POSITION, ATTR_TILT_POSITION, SUPPORT_CLOSE, SUPPORT_CLOSE_TILT, - SUPPORT_OPEN, SUPPORT_OPEN_TILT, SUPPORT_SET_POSITION, + SUPPORT_OPEN, SUPPORT_OPEN_TILT, SUPPORT_SET_POSITION, SUPPORT_STOP, SUPPORT_SET_TILT_POSITION, CoverDevice) from homeassistant.const import ( STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING) @@ -135,9 +135,10 @@ class HomeKitWindowCover(HomeKitEntity, CoverDevice): self._state = None self._position = None self._tilt_position = None - self._hold = None self._obstruction_detected = None self.lock_state = None + self._features = ( + SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION) def get_characteristic_types(self): """Define the homekit characteristics the entity cares about.""" @@ -155,15 +156,25 @@ class HomeKitWindowCover(HomeKitEntity, CoverDevice): CharacteristicsTypes.OBSTRUCTION_DETECTED, ] + def _setup_position_hold(self, char): + self._features |= SUPPORT_STOP + + def _setup_vertical_tilt_current(self, char): + self._features |= ( + SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT | + SUPPORT_SET_TILT_POSITION) + + def _setup_horizontal_tilt_current(self, char): + self._features |= ( + SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT | + SUPPORT_SET_TILT_POSITION) + def _update_position_state(self, value): self._state = CURRENT_WINDOW_STATE_MAP[value] def _update_position_current(self, value): self._position = value - def _update_position_hold(self, value): - self._hold = value - def _update_vertical_tilt_current(self, value): self._tilt_position = value @@ -173,21 +184,10 @@ class HomeKitWindowCover(HomeKitEntity, CoverDevice): def _update_obstruction_detected(self, value): self._obstruction_detected = value - def _update_name(self, value): - self._hold = value - @property def supported_features(self): """Flag supported features.""" - supported_features = ( - SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION) - - if self._tilt_position is not None: - supported_features |= ( - SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT | - SUPPORT_SET_TILT_POSITION) - - return supported_features + return self._features @property def current_cover_position(self): @@ -209,6 +209,13 @@ class HomeKitWindowCover(HomeKitEntity, CoverDevice): """Return if the cover is opening or not.""" return self._state == STATE_OPENING + async def async_stop_cover(self, **kwargs): + """Send hold command.""" + characteristics = [{'aid': self._aid, + 'iid': self._chars['position.hold'], + 'value': 1}] + await self._accessory.put_characteristics(characteristics) + async def async_open_cover(self, **kwargs): """Send open command.""" await self.async_set_cover_position(position=100) @@ -253,8 +260,4 @@ class HomeKitWindowCover(HomeKitEntity, CoverDevice): state_attributes['obstruction-detected'] = \ self._obstruction_detected - if self._hold is not None: - state_attributes['hold-position'] = \ - self._hold - return state_attributes diff --git a/tests/components/homekit_controller/test_cover.py b/tests/components/homekit_controller/test_cover.py index 62fce4325c7..19ccc21b7e8 100644 --- a/tests/components/homekit_controller/test_cover.py +++ b/tests/components/homekit_controller/test_cover.py @@ -5,6 +5,7 @@ from tests.components.homekit_controller.common import ( POSITION_STATE = ('window-covering', 'position.state') POSITION_CURRENT = ('window-covering', 'position.current') POSITION_TARGET = ('window-covering', 'position.target') +POSITION_HOLD = ('window-covering', 'position.hold') H_TILT_CURRENT = ('window-covering', 'horizontal-tilt.current') H_TILT_TARGET = ('window-covering', 'horizontal-tilt.target') @@ -166,6 +167,17 @@ async def test_write_window_cover_tilt_vertical(hass, utcnow): assert helper.characteristics[V_TILT_TARGET].value == 90 +async def test_window_cover_stop(hass, utcnow): + """Test that vertical tilt is written correctly.""" + window_cover = create_window_covering_service_with_v_tilt() + helper = await setup_test_component(hass, [window_cover]) + + await hass.services.async_call('cover', 'stop_cover', { + 'entity_id': helper.entity_id, + }, blocking=True) + assert helper.characteristics[POSITION_HOLD].value == 1 + + def create_garage_door_opener_service(): """Define a garage-door-opener chars as per page 217 of HAP spec.""" service = FakeService('public.hap.service.garage-door-opener')