From 6ccf82d7b1385d902382b7e05c5bb38a528768e4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 4 Feb 2024 14:12:24 -0600 Subject: [PATCH] Avoid linear search of entity registry in keenetic_ndms2 (#109635) --- homeassistant/components/keenetic_ndms2/__init__.py | 9 ++++----- .../components/keenetic_ndms2/device_tracker.py | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/keenetic_ndms2/__init__.py b/homeassistant/components/keenetic_ndms2/__init__.py index 207c9e353a1..6f33b11742a 100644 --- a/homeassistant/components/keenetic_ndms2/__init__.py +++ b/homeassistant/components/keenetic_ndms2/__init__.py @@ -75,11 +75,10 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> for mac, device in router.last_devices.items() if device.interface in new_tracked_interfaces } - for entity_entry in list(ent_reg.entities.values()): - if ( - entity_entry.config_entry_id == config_entry.entry_id - and entity_entry.domain == Platform.DEVICE_TRACKER - ): + for entity_entry in ent_reg.entities.get_entries_for_config_entry_id( + config_entry.entry_id + ): + if entity_entry.domain == Platform.DEVICE_TRACKER: mac = entity_entry.unique_id.partition("_")[0] if mac not in keep_devices: _LOGGER.debug("Removing entity %s", entity_entry.entity_id) diff --git a/homeassistant/components/keenetic_ndms2/device_tracker.py b/homeassistant/components/keenetic_ndms2/device_tracker.py index c51d30431be..c9e81071ad7 100644 --- a/homeassistant/components/keenetic_ndms2/device_tracker.py +++ b/homeassistant/components/keenetic_ndms2/device_tracker.py @@ -43,11 +43,10 @@ async def async_setup_entry( registry = er.async_get(hass) # Restore devices that are not a part of active clients list. restored = [] - for entity_entry in registry.entities.values(): - if ( - entity_entry.config_entry_id == config_entry.entry_id - and entity_entry.domain == DEVICE_TRACKER_DOMAIN - ): + for entity_entry in registry.entities.get_entries_for_config_entry_id( + config_entry.entry_id + ): + if entity_entry.domain == DEVICE_TRACKER_DOMAIN: mac = entity_entry.unique_id.partition("_")[0] if mac not in tracked: tracked.add(mac)