diff --git a/homeassistant/components/homekit_controller/__init__.py b/homeassistant/components/homekit_controller/__init__.py index 461d46eed1d..9175dca50bc 100644 --- a/homeassistant/components/homekit_controller/__init__.py +++ b/homeassistant/components/homekit_controller/__init__.py @@ -76,7 +76,9 @@ class HomeKitEntity(Entity): ) self._accessory.add_pollable_characteristics(self.pollable_characteristics) - self._accessory.add_watchable_characteristics(self.watchable_characteristics) + await self._accessory.add_watchable_characteristics( + self.watchable_characteristics + ) async def async_will_remove_from_hass(self) -> None: """Prepare to be removed from hass.""" diff --git a/homeassistant/components/homekit_controller/connection.py b/homeassistant/components/homekit_controller/connection.py index e1cbedb01e8..85e8e3987bd 100644 --- a/homeassistant/components/homekit_controller/connection.py +++ b/homeassistant/components/homekit_controller/connection.py @@ -145,12 +145,12 @@ class HKDevice: char for char in self.pollable_characteristics if char[0] != accessory_id ] - def add_watchable_characteristics( + async def add_watchable_characteristics( self, characteristics: list[tuple[int, int]] ) -> None: """Add (aid, iid) pairs that we need to poll.""" self.watchable_characteristics.extend(characteristics) - self.hass.async_create_task(self.pairing.subscribe(characteristics)) + await self.pairing.subscribe(characteristics) def remove_watchable_characteristics(self, accessory_id: int) -> None: """Remove all pollable characteristics by accessory id.""" diff --git a/homeassistant/components/homekit_controller/device_trigger.py b/homeassistant/components/homekit_controller/device_trigger.py index dcac7238c8e..49924b30c57 100644 --- a/homeassistant/components/homekit_controller/device_trigger.py +++ b/homeassistant/components/homekit_controller/device_trigger.py @@ -1,7 +1,7 @@ """Provides device automations for homekit devices.""" from __future__ import annotations -from collections.abc import Generator +from collections.abc import Callable, Generator from typing import TYPE_CHECKING, Any from aiohomekit.model.characteristics import CharacteristicsTypes @@ -59,7 +59,9 @@ HK_TO_HA_INPUT_EVENT_VALUES = { class TriggerSource: """Represents a stateless source of event data from HomeKit.""" - def __init__(self, connection, aid, triggers): + def __init__( + self, connection: HKDevice, aid: int, triggers: list[dict[str, Any]] + ) -> None: """Initialize a set of triggers for a device.""" self._hass = connection.hass self._connection = connection @@ -67,7 +69,7 @@ class TriggerSource: self._triggers: dict[tuple[str, str], dict[str, Any]] = {} for trigger in triggers: self._triggers[(trigger["type"], trigger["subtype"])] = trigger - self._callbacks = {} + self._callbacks: dict[int, list[Callable[[Any], None]]] = {} def fire(self, iid, value): """Process events that have been received from a HomeKit accessory.""" @@ -97,7 +99,7 @@ class TriggerSource: trigger = self._triggers[config[CONF_TYPE], config[CONF_SUBTYPE]] iid = trigger["characteristic"] - self._connection.add_watchable_characteristics([(self._aid, iid)]) + await self._connection.add_watchable_characteristics([(self._aid, iid)]) self._callbacks.setdefault(iid, []).append(event_handler) def async_remove_handler(): @@ -196,7 +198,7 @@ TRIGGER_FINDERS = { async def async_setup_triggers_for_entry(hass: HomeAssistant, config_entry): """Triggers aren't entities as they have no state, but we still need to set them up for a config entry.""" hkid = config_entry.data["AccessoryPairingID"] - conn = hass.data[KNOWN_DEVICES][hkid] + conn: HKDevice = hass.data[KNOWN_DEVICES][hkid] @callback def async_add_service(service):