Skip to content
Closed
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
29 changes: 25 additions & 4 deletions homeassistant/components/homekit/type_covers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class GarageDoorOpener(HomeAccessory):
and support no more than open, close, and stop.
"""

HASS_TO_HK_STATE = {
STATE_OPEN: 0,
STATE_CLOSED: 1,
STATE_OPENING: 2,
STATE_CLOSING: 3,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just compared this to other files of homekit/type_xxx.py and now I would recommend to move this dict outside of the class GarageDoorOpener. See the next example for reference:
https://github.com/home-assistant/home-assistant/blob/48402d49dcc54cdba26e44cdfb45e95e6e3edd15/homeassistant/components/homekit/type_locks.py#L15-L20


def __init__(self, *args):
"""Initialize a GarageDoorOpener accessory object."""
super().__init__(*args, category=CATEGORY_GARAGE_DOOR_OPENER)
Expand Down Expand Up @@ -76,13 +83,27 @@ def set_state(self, value):
def update_state(self, new_state):
"""Update cover state after state changed."""
hass_state = new_state.state
if hass_state in (STATE_OPEN, STATE_CLOSED):
current_state = 0 if hass_state == STATE_OPEN else 1
self.char_current_state.set_value(current_state)
if hass_state in self.HASS_TO_HK_STATE:
self.char_current_state.set_value(self.HASS_TO_HK_STATE[hass_state])
if not self._flag_state:
self.char_target_state.set_value(current_state)
self.char_target_state.set_value(
self.target_state_for_hass_state(hass_state)
)
self._flag_state = False

def target_state_for_hass_state(self, hass_state):
"""Return the target state for the current state.

If the current state is opening, the target state
should be open in order for Homekit to detect the
opening state. The same goes for the closing state.
"""
if hass_state == STATE_OPENING:
return self.HASS_TO_HK_STATE[STATE_OPEN]
if hass_state == STATE_CLOSING:
return self.HASS_TO_HK_STATE[STATE_CLOSED]
return self.HASS_TO_HK_STATE[hass_state]


@TYPES.register("WindowCovering")
class WindowCovering(HomeAccessory):
Expand Down
20 changes: 20 additions & 0 deletions tests/components/homekit/test_type_covers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ async def test_garage_door_open_close(hass, hk_driver, cls, events):
assert len(events) == 4
assert events[-1].data[ATTR_VALUE] is None

hass.states.async_set(entity_id, STATE_OPENING)
await hass.async_block_till_done()
assert acc.char_current_state.value == 2
assert acc.char_target_state.value == 0

hass.states.async_set(entity_id, STATE_OPEN)
await hass.async_block_till_done()
assert acc.char_current_state.value == 0
assert acc.char_target_state.value == 0

hass.states.async_set(entity_id, STATE_CLOSING)
await hass.async_block_till_done()
assert acc.char_current_state.value == 3
assert acc.char_target_state.value == 1

hass.states.async_set(entity_id, STATE_CLOSED)
await hass.async_block_till_done()
assert acc.char_current_state.value == 1
assert acc.char_target_state.value == 1


async def test_window_set_cover_position(hass, hk_driver, cls, events):
"""Test if accessory and HA are updated accordingly."""
Expand Down