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
1 change: 1 addition & 0 deletions homeassistant/components/homekit/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
CHAR_ON = 'On'
CHAR_POSITION_STATE = 'PositionState'
CHAR_ROTATION_DIRECTION = 'RotationDirection'
CHAR_ROTATION_SPEED = 'RotationSpeed'
CHAR_SATURATION = 'Saturation'
CHAR_SERIAL_NUMBER = 'SerialNumber'
CHAR_SMOKE_DETECTED = 'SmokeDetected'
Expand Down
38 changes: 33 additions & 5 deletions homeassistant/components/homekit/type_fans.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
from pyhap.const import CATEGORY_FAN

from homeassistant.components.fan import (
ATTR_DIRECTION, ATTR_OSCILLATING, DIRECTION_FORWARD, DIRECTION_REVERSE,
DOMAIN, SERVICE_OSCILLATE, SERVICE_SET_DIRECTION, SUPPORT_DIRECTION,
SUPPORT_OSCILLATE)
ATTR_DIRECTION, ATTR_OSCILLATING, ATTR_SPEED,
DIRECTION_FORWARD, DIRECTION_REVERSE, DOMAIN,
SERVICE_OSCILLATE, SERVICE_SET_DIRECTION,
SERVICE_SET_SPEED, SUPPORT_DIRECTION, SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED)
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, SERVICE_TURN_OFF,
SERVICE_TURN_ON, STATE_OFF, STATE_ON)

from . import TYPES
from .accessories import HomeAccessory
from .const import (
CHAR_ACTIVE, CHAR_ROTATION_DIRECTION, CHAR_SWING_MODE, SERV_FANV2)
CHAR_ACTIVE, CHAR_ROTATION_DIRECTION, CHAR_ROTATION_SPEED,
CHAR_SWING_MODE, SERV_FANV2)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -31,7 +34,8 @@ def __init__(self, *args):
super().__init__(*args, category=CATEGORY_FAN)
self._flag = {CHAR_ACTIVE: False,
CHAR_ROTATION_DIRECTION: False,
CHAR_SWING_MODE: False}
CHAR_SWING_MODE: False,
CHAR_ROTATION_SPEED: False}
self._state = 0

self.chars = []
Expand All @@ -41,6 +45,8 @@ def __init__(self, *args):
self.chars.append(CHAR_ROTATION_DIRECTION)
if features & SUPPORT_OSCILLATE:
self.chars.append(CHAR_SWING_MODE)
if features & SUPPORT_SET_SPEED:
self.chars.append(CHAR_ROTATION_SPEED)

serv_fan = self.add_preload_service(SERV_FANV2, self.chars)
self.char_active = serv_fan.configure_char(
Expand All @@ -55,6 +61,11 @@ def __init__(self, *args):
self.char_swing = serv_fan.configure_char(
CHAR_SWING_MODE, value=0, setter_callback=self.set_oscillating)

if CHAR_ROTATION_SPEED in self.chars:
self.char_speed = serv_fan.configure_char(
CHAR_ROTATION_SPEED, value=0,
setter_callback=self.set_speed)

def set_state(self, value):
"""Set state if call came from HomeKit."""
if self._state == value:
Expand Down Expand Up @@ -83,6 +94,14 @@ def set_oscillating(self, value):
ATTR_OSCILLATING: oscillating}
self.hass.services.call(DOMAIN, SERVICE_OSCILLATE, params)

def set_speed(self, value):
"""Set state if call came from HomeKit."""
_LOGGER.debug('%s: Set speed to %d', self.entity_id, value)
self._flag[CHAR_ROTATION_SPEED] = True
speed = str(value)
params = {ATTR_ENTITY_ID: self.entity_id, ATTR_SPEED: speed}
self.hass.services.call(DOMAIN, SERVICE_SET_SPEED, params)

def update_state(self, new_state):
"""Update fan after state change."""
# Handle State
Expand Down Expand Up @@ -113,3 +132,12 @@ def update_state(self, new_state):
if self.char_swing.value != hk_oscillating:
self.char_swing.set_value(hk_oscillating)
self._flag[CHAR_SWING_MODE] = False

# Handle Speed
if CHAR_ROTATION_SPEED in self.chars:
speed = new_state.attributes.get(ATTR_SPEED)
if not self._flag[CHAR_ROTATION_SPEED]:
hk_speed = float(speed)
if self.char_speed.value != hk_speed:
self.char_speed.set_value(hk_speed)
self._flag[CHAR_ROTATION_SPEED] = False