Skip to content

Commit

Permalink
Remove homekit cover precision workarounds (home-assistant#42647)
Browse files Browse the repository at this point in the history
These workarounds inevitably ended up assuming the wrong thing
In issue home-assistant#42444 they would assume it was still opening, and
in issue home-assistant#41645 they would assume is was already closed.
  • Loading branch information
bdraco authored Nov 1, 2020
1 parent 181811b commit 951f166
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions homeassistant/components/homekit/type_covers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
CHAR_TARGET_POSITION,
CHAR_TARGET_TILT_ANGLE,
CONF_LINKED_OBSTRUCTION_SENSOR,
DEVICE_PRECISION_LEEWAY,
HK_DOOR_CLOSED,
HK_DOOR_CLOSING,
HK_DOOR_OPEN,
Expand Down Expand Up @@ -205,7 +204,6 @@ def __init__(self, *args, category, service):

self.features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
self._supports_stop = self.features & SUPPORT_STOP
self._homekit_target_tilt = None
self.chars = []
if self._supports_stop:
self.chars.append(CHAR_HOLD_POSITION)
Expand Down Expand Up @@ -238,7 +236,6 @@ def set_stop(self, value):
@debounce
def set_tilt(self, value):
"""Set tilt to value if call came from HomeKit."""
self._homekit_target_tilt = value
_LOGGER.info("%s: Set tilt to %d", self.entity_id, value)

# HomeKit sends values between -90 and 90.
Expand All @@ -261,17 +258,8 @@ def async_update_state(self, new_state):
current_tilt = int(current_tilt)
if self.char_current_tilt.value != current_tilt:
self.char_current_tilt.set_value(current_tilt)

# We have to assume that the device has worse precision than HomeKit.
# If it reports back a state that is only _close_ to HK's requested
# state, we'll "fix" what HomeKit requested so that it won't appear
# out of sync.
if self._homekit_target_tilt is None or abs(
current_tilt - self._homekit_target_tilt < DEVICE_PRECISION_LEEWAY
):
if self.char_target_tilt.value != current_tilt:
self.char_target_tilt.set_value(current_tilt)
self._homekit_target_tilt = None
if self.char_target_tilt.value != current_tilt:
self.char_target_tilt.set_value(current_tilt)


class OpeningDevice(OpeningDeviceBase, HomeAccessory):
Expand All @@ -284,7 +272,6 @@ def __init__(self, *args, category, service):
"""Initialize a WindowCovering accessory object."""
super().__init__(*args, category=category, service=service)
state = self.hass.states.get(self.entity_id)
self._homekit_target = None

self.char_current_position = self.serv_cover.configure_char(
CHAR_CURRENT_POSITION, value=0
Expand All @@ -301,8 +288,6 @@ def __init__(self, *args, category, service):
def move_cover(self, value):
"""Move cover to value if call came from HomeKit."""
_LOGGER.debug("%s: Set position to %d", self.entity_id, value)
self._homekit_target = value

params = {ATTR_ENTITY_ID: self.entity_id, ATTR_POSITION: value}
self.call_service(DOMAIN, SERVICE_SET_COVER_POSITION, params, value)

Expand All @@ -314,28 +299,12 @@ def async_update_state(self, new_state):
current_position = int(current_position)
if self.char_current_position.value != current_position:
self.char_current_position.set_value(current_position)
if self.char_target_position.value != current_position:
self.char_target_position.set_value(current_position)

# We have to assume that the device has worse precision than HomeKit.
# If it reports back a state that is only _close_ to HK's requested
# state, we'll "fix" what HomeKit requested so that it won't appear
# out of sync.
if (
self._homekit_target is None
or abs(current_position - self._homekit_target)
< DEVICE_PRECISION_LEEWAY
):
if self.char_target_position.value != current_position:
self.char_target_position.set_value(current_position)
self._homekit_target = None
if new_state.state == STATE_OPENING:
if self.char_position_state.value != HK_POSITION_GOING_TO_MAX:
self.char_position_state.set_value(HK_POSITION_GOING_TO_MAX)
elif new_state.state == STATE_CLOSING:
if self.char_position_state.value != HK_POSITION_GOING_TO_MIN:
self.char_position_state.set_value(HK_POSITION_GOING_TO_MIN)
else:
if self.char_position_state.value != HK_POSITION_STOPPED:
self.char_position_state.set_value(HK_POSITION_STOPPED)
position_state = _hass_state_to_position_start(new_state.state)
if self.char_position_state.value != position_state:
self.char_position_state.set_value(position_state)

super().async_update_state(new_state)

Expand Down Expand Up @@ -426,14 +395,17 @@ def async_update_state(self, new_state):
self.char_current_position.set_value(hk_position)
if self.char_target_position.value != hk_position:
self.char_target_position.set_value(hk_position)
if new_state.state == STATE_OPENING:
if self.char_position_state.value != HK_POSITION_GOING_TO_MAX:
self.char_position_state.set_value(HK_POSITION_GOING_TO_MAX)
elif new_state.state == STATE_CLOSING:
if self.char_position_state.value != HK_POSITION_GOING_TO_MIN:
self.char_position_state.set_value(HK_POSITION_GOING_TO_MIN)
else:
if self.char_position_state.value != HK_POSITION_STOPPED:
self.char_position_state.set_value(HK_POSITION_STOPPED)
position_state = _hass_state_to_position_start(new_state.state)
if self.char_position_state.value != position_state:
self.char_position_state.set_value(position_state)

super().async_update_state(new_state)


def _hass_state_to_position_start(state):
"""Convert hass state to homekit position state."""
if state == STATE_OPENING:
return HK_POSITION_GOING_TO_MAX
if state == STATE_CLOSING:
return HK_POSITION_GOING_TO_MIN
return HK_POSITION_STOPPED

0 comments on commit 951f166

Please sign in to comment.