Use a filter for the PersonStorageCollection EVENT_ENTITY_REGISTRY_UPDATED listener (#89335)

Avoids creating a task unless a device_tracker is removed
pull/89341/head^2
J. Nick Koston 2023-03-07 16:07:24 -10:00 committed by GitHub
parent 008a30618c
commit 3e5e937541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 9 deletions

View File

@ -226,19 +226,22 @@ class PersonStorageCollection(collection.StorageCollection):
"""Load the Storage collection."""
await super().async_load()
self.hass.bus.async_listen(
er.EVENT_ENTITY_REGISTRY_UPDATED, self._entity_registry_updated
er.EVENT_ENTITY_REGISTRY_UPDATED,
self._entity_registry_updated,
event_filter=self._entity_registry_filter,
)
async def _entity_registry_updated(self, event) -> None:
@callback
def _entity_registry_filter(self, event: Event) -> bool:
"""Filter entity registry events."""
return (
event.data["action"] == "remove"
and split_entity_id(event.data[ATTR_ENTITY_ID])[0] == "device_tracker"
)
async def _entity_registry_updated(self, event: Event) -> None:
"""Handle entity registry updated."""
if event.data["action"] != "remove":
return
entity_id = event.data[ATTR_ENTITY_ID]
if split_entity_id(entity_id)[0] != "device_tracker":
return
for person in list(self.data.values()):
if entity_id not in person[CONF_DEVICE_TRACKERS]:
continue