From 8cf14c92681d92fe658d45a09278de4bbb0ad210 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 17 Apr 2024 08:46:59 -0500 Subject: [PATCH] Avoid linear search to clear labels and areas in the device registry (#115676) --- homeassistant/helpers/device_registry.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 0270c8dc456..4cc9a29d46a 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -1053,18 +1053,14 @@ class DeviceRegistry(BaseRegistry[dict[str, list[dict[str, Any]]]]): @callback def async_clear_area_id(self, area_id: str) -> None: """Clear area id from registry entries.""" - for dev_id, device in self.devices.items(): - if area_id == device.area_id: - self.async_update_device(dev_id, area_id=None) + for device in self.devices.get_devices_for_area_id(area_id): + self.async_update_device(device.id, area_id=None) @callback def async_clear_label_id(self, label_id: str) -> None: """Clear label from registry entries.""" - for device_id, entry in self.devices.items(): - if label_id in entry.labels: - labels = entry.labels.copy() - labels.remove(label_id) - self.async_update_device(device_id, labels=labels) + for device in self.devices.get_devices_for_label(label_id): + self.async_update_device(device.id, labels=device.labels - {label_id}) @callback