Dont rely on config flow to monitor homekit_controller c# changes (#76861)

This commit is contained in:
Jc2k
2022-08-20 21:58:59 +01:00
committed by GitHub
parent 8b1713a691
commit eb0828efdb
11 changed files with 36 additions and 83 deletions

View File

@ -30,7 +30,6 @@ from .const import (
CHARACTERISTIC_PLATFORMS,
CONTROLLER,
DOMAIN,
ENTITY_MAP,
HOMEKIT_ACCESSORY_DISPATCH,
IDENTIFIER_ACCESSORY_ID,
IDENTIFIER_LEGACY_ACCESSORY_ID,
@ -38,7 +37,6 @@ from .const import (
IDENTIFIER_SERIAL_NUMBER,
)
from .device_trigger import async_fire_triggers, async_setup_triggers_for_entry
from .storage import EntityMapStorage
RETRY_INTERVAL = 60 # seconds
MAX_POLL_FAILURES_TO_DECLARE_UNAVAILABLE = 3
@ -182,14 +180,10 @@ class HKDevice:
async def async_setup(self) -> None:
"""Prepare to use a paired HomeKit device in Home Assistant."""
entity_storage: EntityMapStorage = self.hass.data[ENTITY_MAP]
pairing = self.pairing
transport = pairing.transport
entry = self.config_entry
if cache := entity_storage.get_map(self.unique_id):
pairing.restore_accessories_state(cache["accessories"], cache["config_num"])
# We need to force an update here to make sure we have
# the latest values since the async_update we do in
# async_process_entity_map will no values to poll yet
@ -203,7 +197,7 @@ class HKDevice:
try:
await self.pairing.async_populate_accessories_state(force_update=True)
except AccessoryNotFoundError:
if transport != Transport.BLE or not cache:
if transport != Transport.BLE or not pairing.accessories:
# BLE devices may sleep and we can't force a connection
raise
@ -217,9 +211,6 @@ class HKDevice:
await self.async_process_entity_map()
if not cache:
# If its missing from the cache, make sure we save it
self.async_save_entity_map()
# If everything is up to date, we can create the entities
# since we know the data is not stale.
await self.async_add_new_entities()
@ -438,31 +429,18 @@ class HKDevice:
self.config_entry, self.platforms
)
def async_notify_config_changed(self, config_num: int) -> None:
"""Notify the pairing of a config change."""
self.pairing.notify_config_changed(config_num)
def process_config_changed(self, config_num: int) -> None:
"""Handle a config change notification from the pairing."""
self.hass.async_create_task(self.async_update_new_accessories_state())
async def async_update_new_accessories_state(self) -> None:
"""Process a change in the pairings accessories state."""
self.async_save_entity_map()
await self.async_process_entity_map()
if self.watchable_characteristics:
await self.pairing.subscribe(self.watchable_characteristics)
await self.async_update()
await self.async_add_new_entities()
@callback
def async_save_entity_map(self) -> None:
"""Save the entity map."""
entity_storage: EntityMapStorage = self.hass.data[ENTITY_MAP]
entity_storage.async_create_or_update_map(
self.unique_id, self.config_num, self.entity_map.serialize()
)
def add_accessory_factory(self, add_entities_cb) -> None:
"""Add a callback to run when discovering new entities for accessories."""
self.accessory_factories.append(add_entities_cb)