From 3e5e937541dd368f3d70c7557ceaa7a07fd284e2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Mar 2023 16:07:24 -1000 Subject: [PATCH] Use a filter for the PersonStorageCollection EVENT_ENTITY_REGISTRY_UPDATED listener (#89335) Avoids creating a task unless a device_tracker is removed --- homeassistant/components/person/__init__.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index e3b719d166f..a5e56d00731 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -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