Convert homekit fans to use service callbacks (#34229)

* Convert homekit fans to use service callbacks

* Convert homekit fans to use service callbacks

Service callbacks allow us ensure that we call
fan services in the correct order.

* Avoid calling turn_on if a speed is sent and the device is on

* Fix test to not leave files behind

* Fix test

* Update homeassistant/components/homekit/type_fans.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
J. Nick KostonandPaulus Schoutsen authored and GitHub committed 2020-04-15 21:39:31 -05:00
1 parent d6a47cb3e0
commit e8710002b1
5 files changed
+420 -138

No files matched your search

+46 -38
View File
@@ -28,7 +28,7 @@ from homeassistant.const import (
)
from . import TYPES
from .accessories import HomeAccessory, debounce
from .accessories import HomeAccessory
from .const import (
CHAR_ACTIVE,
CHAR_ROTATION_DIRECTION,
@@ -51,17 +51,11 @@ class Fan(HomeAccessory):
def __init__(self, *args):
"""Initialize a new Light accessory object."""
super().__init__(*args, category=CATEGORY_FAN)
self._flag = {
CHAR_ACTIVE: False,
CHAR_ROTATION_DIRECTION: False,
CHAR_SWING_MODE: False,
}
self._state = 0
chars = []
features = self.hass.states.get(self.entity_id).attributes.get(
ATTR_SUPPORTED_FEATURES
)
state = self.hass.states.get(self.entity_id)
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if features & SUPPORT_DIRECTION:
chars.append(CHAR_ROTATION_DIRECTION)
if features & SUPPORT_OSCILLATE:
@@ -74,9 +68,7 @@ class Fan(HomeAccessory):
chars.append(CHAR_ROTATION_SPEED)
serv_fan = self.add_preload_service(SERV_FANV2, chars)
self.char_active = serv_fan.configure_char(
CHAR_ACTIVE, value=0, setter_callback=self.set_state
)
self.char_active = serv_fan.configure_char(CHAR_ACTIVE, value=0)
self.char_direction = None
self.char_speed = None
@@ -84,26 +76,52 @@ class Fan(HomeAccessory):
if CHAR_ROTATION_DIRECTION in chars:
self.char_direction = serv_fan.configure_char(
CHAR_ROTATION_DIRECTION, value=0, setter_callback=self.set_direction
CHAR_ROTATION_DIRECTION, value=0
)
if CHAR_ROTATION_SPEED in chars:
# Initial value is set to 100 because 0 is a special value (off). 100 is
# an arbitrary non-zero value. It is updated immediately by update_state
# to set to the correct initial value.
self.char_speed = serv_fan.configure_char(
CHAR_ROTATION_SPEED, value=100, setter_callback=self.set_speed
)
self.char_speed = serv_fan.configure_char(CHAR_ROTATION_SPEED, value=100)
if CHAR_SWING_MODE in chars:
self.char_swing = serv_fan.configure_char(
CHAR_SWING_MODE, value=0, setter_callback=self.set_oscillating
)
self.char_swing = serv_fan.configure_char(CHAR_SWING_MODE, value=0)
self.update_state(state)
serv_fan.setter_callback = self._set_chars
def _set_chars(self, char_values):
_LOGGER.debug("Fan _set_chars: %s", char_values)
if CHAR_ACTIVE in char_values:
if char_values[CHAR_ACTIVE]:
is_on = False
state = self.hass.states.get(self.entity_id)
if state and state.state == STATE_ON:
is_on = True
# Only set the state to active if we
# did not get a rotation speed or its off
if not is_on or CHAR_ROTATION_SPEED not in char_values:
self.set_state(1)
else:
# Its off, nothing more to do as setting the
# other chars will likely turn it back on which
# is what we want to avoid
self.set_state(0)
return
if CHAR_SWING_MODE in char_values:
self.set_oscillating(char_values[CHAR_SWING_MODE])
if CHAR_ROTATION_DIRECTION in char_values:
self.set_direction(char_values[CHAR_ROTATION_DIRECTION])
# We always do this LAST to ensure they
# get the speed they asked for
if CHAR_ROTATION_SPEED in char_values:
self.set_speed(char_values[CHAR_ROTATION_SPEED])
def set_state(self, value):
"""Set state if call came from HomeKit."""
_LOGGER.debug("%s: Set state to %d", self.entity_id, value)
self._flag[CHAR_ACTIVE] = True
service = SERVICE_TURN_ON if value == 1 else SERVICE_TURN_OFF
params = {ATTR_ENTITY_ID: self.entity_id}
self.call_service(DOMAIN, service, params)
@@ -111,7 +129,6 @@ class Fan(HomeAccessory):
def set_direction(self, value):
"""Set state if call came from HomeKit."""
_LOGGER.debug("%s: Set direction to %d", self.entity_id, value)
self._flag[CHAR_ROTATION_DIRECTION] = True
direction = DIRECTION_REVERSE if value == 1 else DIRECTION_FORWARD
params = {ATTR_ENTITY_ID: self.entity_id, ATTR_DIRECTION: direction}
self.call_service(DOMAIN, SERVICE_SET_DIRECTION, params, direction)
@@ -119,12 +136,10 @@ class Fan(HomeAccessory):
def set_oscillating(self, value):
"""Set state if call came from HomeKit."""
_LOGGER.debug("%s: Set oscillating to %d", self.entity_id, value)
self._flag[CHAR_SWING_MODE] = True
oscillating = value == 1
params = {ATTR_ENTITY_ID: self.entity_id, ATTR_OSCILLATING: oscillating}
self.call_service(DOMAIN, SERVICE_OSCILLATE, params, oscillating)
@debounce
def set_speed(self, value):
"""Set state if call came from HomeKit."""
_LOGGER.debug("%s: Set speed to %d", self.entity_id, value)
@@ -138,21 +153,16 @@ class Fan(HomeAccessory):
state = new_state.state
if state in (STATE_ON, STATE_OFF):
self._state = 1 if state == STATE_ON else 0
if not self._flag[CHAR_ACTIVE] and self.char_active.value != self._state:
if self.char_active.value != self._state:
self.char_active.set_value(self._state)
self._flag[CHAR_ACTIVE] = False
# Handle Direction
if self.char_direction is not None:
direction = new_state.attributes.get(ATTR_DIRECTION)
if not self._flag[CHAR_ROTATION_DIRECTION] and direction in (
DIRECTION_FORWARD,
DIRECTION_REVERSE,
):
if direction in (DIRECTION_FORWARD, DIRECTION_REVERSE):
hk_direction = 1 if direction == DIRECTION_REVERSE else 0
if self.char_direction.value != hk_direction:
self.char_direction.set_value(hk_direction)
self._flag[CHAR_ROTATION_DIRECTION] = False
# Handle Speed
if self.char_speed is not None:
@@ -170,17 +180,15 @@ class Fan(HomeAccessory):
# Therefore, if the hk_speed_value is 0 and the device is still on,
# the rotation speed is mapped to 1 otherwise the update is ignored
# in order to avoid this incorrect behavior.
if hk_speed_value == 0:
if state == STATE_ON:
self.char_speed.set_value(1)
else:
if hk_speed_value == 0 and state == STATE_ON:
hk_speed_value = 1
if self.char_speed.value != hk_speed_value:
self.char_speed.set_value(hk_speed_value)
# Handle Oscillating
if self.char_swing is not None:
oscillating = new_state.attributes.get(ATTR_OSCILLATING)
if not self._flag[CHAR_SWING_MODE] and oscillating in (True, False):
if isinstance(oscillating, bool):
hk_oscillating = 1 if oscillating else 0
if self.char_swing.value != hk_oscillating:
self.char_swing.set_value(hk_oscillating)
self._flag[CHAR_SWING_MODE] = False