Defer homekit_controller initial poll after all entities are created (#30355)

* Make sure first poll happens after sub-platforms are loaded and entities are created.

* Function dosn't need to return anything

* Don't forward entry if already forwarded it
This commit is contained in:
Jc2k 2020-01-03 20:22:27 +00:00 committed by Paulus Schoutsen
parent 2b26af89df
commit 80701c8f2a

View file

@ -135,20 +135,33 @@ class HKDevice:
self.accessories = cache["accessories"] self.accessories = cache["accessories"]
self.config_num = cache["config_num"] self.config_num = cache["config_num"]
# Ensure the Pairing object has access to the latest version of the self._polling_interval_remover = async_track_time_interval(
# entity map. self.hass, self.async_update, DEFAULT_SCAN_INTERVAL
)
self.hass.async_create_task(self.async_process_entity_map())
return True
async def async_process_entity_map(self):
"""
Process the entity map and load any platforms or entities that need adding.
This is idempotent and will be called at startup and when we detect metadata changes
via the c# counter on the zeroconf record.
"""
# Ensure the Pairing object has access to the latest version of the entity map. This
# is especially important for BLE, as the Pairing instance relies on the entity map
# to map aid/iid to GATT characteristics. So push it to there as well.
self.pairing.pairing_data["accessories"] = self.accessories self.pairing.pairing_data["accessories"] = self.accessories
self.async_load_platforms() await self.async_load_platforms()
self.add_entities() self.add_entities()
await self.async_update() await self.async_update()
self._polling_interval_remover = async_track_time_interval(
self.hass, self.async_update, DEFAULT_SCAN_INTERVAL
)
return True return True
async def async_unload(self): async def async_unload(self):
@ -178,24 +191,14 @@ class HKDevice:
except AccessoryDisconnectedError: except AccessoryDisconnectedError:
# If we fail to refresh this data then we will naturally retry # If we fail to refresh this data then we will naturally retry
# later when Bonjour spots c# is still not up to date. # later when Bonjour spots c# is still not up to date.
return return False
self.hass.data[ENTITY_MAP].async_create_or_update_map( self.hass.data[ENTITY_MAP].async_create_or_update_map(
self.unique_id, config_num, self.accessories self.unique_id, config_num, self.accessories
) )
self.config_num = config_num self.config_num = config_num
self.hass.async_create_task(self.async_process_entity_map())
# For BLE, the Pairing instance relies on the entity map to map
# aid/iid to GATT characteristics. So push it to there as well.
self.pairing.pairing_data["accessories"] = self.accessories
self.async_load_platforms()
# Register and add new entities that are available
self.add_entities()
await self.async_update()
return True return True
@ -225,7 +228,7 @@ class HKDevice:
self.entities.append((aid, iid)) self.entities.append((aid, iid))
break break
def async_load_platforms(self): async def async_load_platforms(self):
"""Load any platforms needed by this HomeKit device.""" """Load any platforms needed by this HomeKit device."""
for accessory in self.accessories: for accessory in self.accessories:
for service in accessory["services"]: for service in accessory["services"]:
@ -237,12 +240,14 @@ class HKDevice:
if platform in self.platforms: if platform in self.platforms:
continue continue
self.hass.async_create_task( self.platforms.add(platform)
self.hass.config_entries.async_forward_entry_setup( try:
await self.hass.config_entries.async_forward_entry_setup(
self.config_entry, platform self.config_entry, platform
) )
) except Exception:
self.platforms.add(platform) self.platforms.remove(platform)
raise
async def async_update(self, now=None): async def async_update(self, now=None):
"""Poll state of all entities attached to this bridge/accessory.""" """Poll state of all entities attached to this bridge/accessory."""