Skip to content
Merged
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
41 changes: 20 additions & 21 deletions homeassistant/components/homekit/accessories.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ async def run_handler(self):
Run inside the Home Assistant event loop.
"""
state = self.hass.states.get(self.entity_id)
await self.async_update_state_callback(None, None, state)
self.async_update_state_callback(None, None, state)
async_track_state_change(
self.hass, self.entity_id, self.async_update_state_callback
)
Expand All @@ -329,7 +329,9 @@ async def run_handler(self):
ATTR_BATTERY_CHARGING
)
async_track_state_change(
self.hass, self.linked_battery_sensor, self.async_update_linked_battery
self.hass,
self.linked_battery_sensor,
self.async_update_linked_battery_callback,
)
else:
battery_state = state.attributes.get(ATTR_BATTERY_LEVEL)
Expand All @@ -341,17 +343,16 @@ async def run_handler(self):
async_track_state_change(
self.hass,
self.linked_battery_charging_sensor,
self.async_update_linked_battery_charging,
self.async_update_linked_battery_charging_callback,
)
elif battery_charging_state is None:
battery_charging_state = state.attributes.get(ATTR_BATTERY_CHARGING)

if battery_state is not None or battery_charging_state is not None:
self.hass.async_add_executor_job(
self.update_battery, battery_state, battery_charging_state
)
self.async_update_battery(battery_state, battery_charging_state)

async def async_update_state_callback(
@ha_callback
def async_update_state_callback(
self, entity_id=None, old_state=None, new_state=None
):
"""Handle state change listener callback."""
Expand All @@ -371,32 +372,29 @@ async def async_update_state_callback(
):
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
if battery_state is not None or battery_charging_state is not None:
await self.hass.async_add_executor_job(
self.update_battery, battery_state, battery_charging_state
)
await self.hass.async_add_executor_job(self.update_state, new_state)
self.async_update_battery(battery_state, battery_charging_state)
self.async_update_state(new_state)

async def async_update_linked_battery(
@ha_callback
def async_update_linked_battery_callback(
self, entity_id=None, old_state=None, new_state=None
):
"""Handle linked battery sensor state change listener callback."""
if self.linked_battery_charging_sensor:
battery_charging_state = None
else:
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
await self.hass.async_add_executor_job(
self.update_battery, new_state.state, battery_charging_state,
)
self.async_update_battery(new_state.state, battery_charging_state)

async def async_update_linked_battery_charging(
@ha_callback
def async_update_linked_battery_charging_callback(
self, entity_id=None, old_state=None, new_state=None
):
"""Handle linked battery charging sensor state change listener callback."""
await self.hass.async_add_executor_job(
self.update_battery, None, new_state.state == STATE_ON
)
self.async_update_battery(None, new_state.state == STATE_ON)

def update_battery(self, battery_level, battery_charging):
@ha_callback
def async_update_battery(self, battery_level, battery_charging):
"""Update battery service if available.

Only call this function if self._support_battery_level is True.
Expand Down Expand Up @@ -427,7 +425,8 @@ def update_battery(self, battery_level, battery_charging):
"%s: Updated battery charging to %d", self.entity_id, hk_charging
)

def update_state(self, new_state):
@ha_callback
def async_update_state(self, new_state):
"""Handle state change to update HomeKit value.

Overridden by accessory types.
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/homekit/type_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from homeassistant.components.camera.const import DOMAIN as DOMAIN_CAMERA
from homeassistant.components.ffmpeg import DATA_FFMPEG
from homeassistant.core import callback
from homeassistant.util import get_local_ip

from .accessories import TYPES, HomeAccessory
Expand Down Expand Up @@ -142,7 +143,8 @@ def __init__(self, hass, driver, name, entity_id, aid, config):
options=options,
)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Handle state change to update HomeKit value."""
pass # pylint: disable=unnecessary-pass

Expand Down
23 changes: 14 additions & 9 deletions homeassistant/components/homekit/type_covers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
STATE_OPEN,
STATE_OPENING,
)
from homeassistant.core import callback

from .accessories import TYPES, HomeAccessory, debounce
from .const import (
Expand Down Expand Up @@ -92,7 +93,7 @@ def __init__(self, *args):
self.char_target_state = serv_garage_door.configure_char(
CHAR_TARGET_DOOR_STATE, value=0, setter_callback=self.set_state
)
self.update_state(state)
self.async_update_state(state)

def set_state(self, value):
"""Change garage state if call came from HomeKit."""
Expand All @@ -108,7 +109,8 @@ def set_state(self, value):
self.char_current_state.set_value(HK_DOOR_CLOSING)
self.call_service(DOMAIN, SERVICE_CLOSE_COVER, params)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update cover state after state changed."""
hass_state = new_state.state
target_door_state = DOOR_TARGET_HASS_TO_HK.get(hass_state)
Expand Down Expand Up @@ -184,7 +186,8 @@ def set_tilt(self, value):

self.call_service(DOMAIN, SERVICE_SET_COVER_TILT_POSITION, params, value)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
Comment thread
bdraco marked this conversation as resolved.
"""Update cover position and tilt after state changed."""
# update tilt
current_tilt = new_state.attributes.get(ATTR_CURRENT_TILT_POSITION)
Expand Down Expand Up @@ -230,7 +233,7 @@ def __init__(self, *args):
self.char_position_state = self.serv_cover.configure_char(
CHAR_POSITION_STATE, value=HK_POSITION_STOPPED
)
self.update_state(state)
self.async_update_state(state)

@debounce
def move_cover(self, value):
Expand All @@ -241,7 +244,8 @@ def move_cover(self, value):
params = {ATTR_ENTITY_ID: self.entity_id, ATTR_POSITION: value}
self.call_service(DOMAIN, SERVICE_SET_COVER_POSITION, params, value)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update cover position and tilt after state changed."""
current_position = new_state.attributes.get(ATTR_CURRENT_POSITION)
if isinstance(current_position, (float, int)):
Expand Down Expand Up @@ -271,7 +275,7 @@ def update_state(self, new_state):
if self.char_position_state.value != HK_POSITION_STOPPED:
self.char_position_state.set_value(HK_POSITION_STOPPED)

super().update_state(new_state)
super().async_update_state(new_state)


@TYPES.register("WindowCoveringBasic")
Expand All @@ -295,7 +299,7 @@ def __init__(self, *args):
self.char_position_state = self.serv_cover.configure_char(
CHAR_POSITION_STATE, value=HK_POSITION_STOPPED
)
self.update_state(state)
self.async_update_state(state)

@debounce
def move_cover(self, value):
Expand All @@ -322,7 +326,8 @@ def move_cover(self, value):
self.char_current_position.set_value(position)
self.char_target_position.set_value(position)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update cover position after state changed."""
position_mapping = {STATE_OPEN: 100, STATE_CLOSED: 0}
hk_position = position_mapping.get(new_state.state)
Expand All @@ -341,4 +346,4 @@ def update_state(self, new_state):
if self.char_position_state.value != HK_POSITION_STOPPED:
self.char_position_state.set_value(HK_POSITION_STOPPED)

super().update_state(new_state)
super().async_update_state(new_state)
8 changes: 5 additions & 3 deletions homeassistant/components/homekit/type_fans.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
STATE_OFF,
STATE_ON,
)
from homeassistant.core import callback

from .accessories import TYPES, HomeAccessory
from .const import (
Expand Down Expand Up @@ -80,13 +81,13 @@ def __init__(self, *args):

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
# an arbitrary non-zero value. It is updated immediately by async_update_state
# to set to the correct initial value.
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)
self.update_state(state)
self.async_update_state(state)
serv_fan.setter_callback = self._set_chars

def _set_chars(self, char_values):
Expand Down Expand Up @@ -146,7 +147,8 @@ def set_speed(self, value):
params = {ATTR_ENTITY_ID: self.entity_id, ATTR_SPEED: speed}
self.call_service(DOMAIN, SERVICE_SET_SPEED, params, speed)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update fan after state change."""
# Handle State
state = new_state.state
Expand Down
8 changes: 5 additions & 3 deletions homeassistant/components/homekit/type_lights.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
STATE_OFF,
STATE_ON,
)
from homeassistant.core import callback

from .accessories import TYPES, HomeAccessory
from .const import (
Expand Down Expand Up @@ -77,7 +78,7 @@ def __init__(self, *args):

if CHAR_BRIGHTNESS in self.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
# an arbitrary non-zero value. It is updated immediately by async_update_state
# to set to the correct initial value.
self.char_brightness = serv_light.configure_char(CHAR_BRIGHTNESS, value=100)

Expand All @@ -100,7 +101,7 @@ def __init__(self, *args):
if CHAR_SATURATION in self.chars:
self.char_saturation = serv_light.configure_char(CHAR_SATURATION, value=75)

self.update_state(state)
self.async_update_state(state)

serv_light.setter_callback = self._set_chars

Expand Down Expand Up @@ -138,7 +139,8 @@ def _set_chars(self, char_values):

self.call_service(DOMAIN, service, params, ", ".join(events))

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update light after state change."""
# Handle State
state = new_state.state
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/homekit/type_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from homeassistant.components.lock import DOMAIN, STATE_LOCKED, STATE_UNLOCKED
from homeassistant.const import ATTR_CODE, ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import callback

from .accessories import TYPES, HomeAccessory
from .const import CHAR_LOCK_CURRENT_STATE, CHAR_LOCK_TARGET_STATE, SERV_LOCK
Expand Down Expand Up @@ -45,7 +46,7 @@ def __init__(self, *args):
value=HASS_TO_HOMEKIT[STATE_LOCKED],
setter_callback=self.set_state,
)
self.update_state(state)
self.async_update_state(state)

def set_state(self, value):
"""Set lock state to value if call came from HomeKit."""
Expand All @@ -62,7 +63,8 @@ def set_state(self, value):
params[ATTR_CODE] = self._code
self.call_service(DOMAIN, service, params)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update lock after state changed."""
hass_state = new_state.state
if hass_state in HASS_TO_HOMEKIT:
Expand Down
11 changes: 7 additions & 4 deletions homeassistant/components/homekit/type_media_players.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
STATE_STANDBY,
STATE_UNKNOWN,
)
from homeassistant.core import callback

from .accessories import TYPES, HomeAccessory
from .const import (
Expand Down Expand Up @@ -143,7 +144,7 @@ def __init__(self, *args):
self.chars[FEATURE_TOGGLE_MUTE] = serv_toggle_mute.configure_char(
CHAR_ON, value=False, setter_callback=self.set_toggle_mute
)
self.update_state(state)
self.async_update_state(state)

def generate_service_name(self, mode):
"""Generate name for individual service."""
Expand Down Expand Up @@ -182,7 +183,8 @@ def set_toggle_mute(self, value):
params = {ATTR_ENTITY_ID: self.entity_id, ATTR_MEDIA_VOLUME_MUTED: value}
self.call_service(DOMAIN, SERVICE_VOLUME_MUTE, params)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update switch state after state changed."""
current_state = new_state.state

Expand Down Expand Up @@ -319,7 +321,7 @@ def __init__(self, *args):
serv_input.configure_char(CHAR_CURRENT_VISIBILITY_STATE, value=False)
_LOGGER.debug("%s: Added source %s.", self.entity_id, source)

self.update_state(state)
self.async_update_state(state)

def set_on_off(self, value):
"""Move switch state to value if call came from HomeKit."""
Expand Down Expand Up @@ -373,7 +375,8 @@ def set_remote_key(self, value):
params = {ATTR_ENTITY_ID: self.entity_id}
self.call_service(DOMAIN, service, params)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update Television state after state changed."""
current_state = new_state.state

Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/homekit/type_security_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
STATE_ALARM_DISARMED,
STATE_ALARM_TRIGGERED,
)
from homeassistant.core import callback

from .accessories import TYPES, HomeAccessory
from .const import (
Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(self, *args):
)
# Set the state so it is in sync on initial
# GET to avoid an event storm after homekit startup
self.update_state(state)
self.async_update_state(state)

def set_security_state(self, value):
"""Move security state to value if call came from HomeKit."""
Expand All @@ -77,7 +78,8 @@ def set_security_state(self, value):
params[ATTR_CODE] = self._alarm_code
self.call_service(DOMAIN, service, params)

def update_state(self, new_state):
@callback
def async_update_state(self, new_state):
"""Update security state after state changed."""
hass_state = new_state.state
if hass_state in HASS_TO_HOMEKIT:
Expand Down
Loading