Fix delay adding entities in HKC (#75273)

This commit is contained in:
J. Nick Koston 2022-07-15 18:21:09 +02:00 committed by GitHub
parent 97fd669924
commit 2106c9f247
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 8 deletions

View file

@ -76,7 +76,9 @@ class HomeKitEntity(Entity):
) )
self._accessory.add_pollable_characteristics(self.pollable_characteristics) 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: async def async_will_remove_from_hass(self) -> None:
"""Prepare to be removed from hass.""" """Prepare to be removed from hass."""

View file

@ -145,12 +145,12 @@ class HKDevice:
char for char in self.pollable_characteristics if char[0] != accessory_id 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]] self, characteristics: list[tuple[int, int]]
) -> None: ) -> None:
"""Add (aid, iid) pairs that we need to poll.""" """Add (aid, iid) pairs that we need to poll."""
self.watchable_characteristics.extend(characteristics) 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: def remove_watchable_characteristics(self, accessory_id: int) -> None:
"""Remove all pollable characteristics by accessory id.""" """Remove all pollable characteristics by accessory id."""

View file

@ -1,7 +1,7 @@
"""Provides device automations for homekit devices.""" """Provides device automations for homekit devices."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Generator from collections.abc import Callable, Generator
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
@ -59,7 +59,9 @@ HK_TO_HA_INPUT_EVENT_VALUES = {
class TriggerSource: class TriggerSource:
"""Represents a stateless source of event data from HomeKit.""" """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.""" """Initialize a set of triggers for a device."""
self._hass = connection.hass self._hass = connection.hass
self._connection = connection self._connection = connection
@ -67,7 +69,7 @@ class TriggerSource:
self._triggers: dict[tuple[str, str], dict[str, Any]] = {} self._triggers: dict[tuple[str, str], dict[str, Any]] = {}
for trigger in triggers: for trigger in triggers:
self._triggers[(trigger["type"], trigger["subtype"])] = trigger self._triggers[(trigger["type"], trigger["subtype"])] = trigger
self._callbacks = {} self._callbacks: dict[int, list[Callable[[Any], None]]] = {}
def fire(self, iid, value): def fire(self, iid, value):
"""Process events that have been received from a HomeKit accessory.""" """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]] trigger = self._triggers[config[CONF_TYPE], config[CONF_SUBTYPE]]
iid = trigger["characteristic"] 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) self._callbacks.setdefault(iid, []).append(event_handler)
def async_remove_handler(): def async_remove_handler():
@ -196,7 +198,7 @@ TRIGGER_FINDERS = {
async def async_setup_triggers_for_entry(hass: HomeAssistant, config_entry): 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.""" """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"] hkid = config_entry.data["AccessoryPairingID"]
conn = hass.data[KNOWN_DEVICES][hkid] conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
@callback @callback
def async_add_service(service): def async_add_service(service):