Avoid linear search of entity registry in async_extract_referenced_entity_ids (#109667)

* Index area_ids in the entity registry

I missed that these are used in _resolve_area in search.

Eventually we can make async_extract_referenced_entity_ids
a bit faster with this as well

* Avoid linear search of entity registry in async_extract_referenced_entity_ids

needs https://github.com/home-assistant/core/pull/109660
This commit is contained in:
J. Nick Koston 2024-02-09 08:42:30 -06:00 committed by GitHub
parent 206aaac700
commit 9689cb448d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -496,26 +496,37 @@ def async_extract_referenced_entity_ids(
if not selector.area_ids and not selected.referenced_devices:
return selected
for ent_entry in ent_reg.entities.values():
entities = ent_reg.entities
# Add indirectly referenced by area
selected.indirectly_referenced.update(
entry.entity_id
for area_id in selector.area_ids
# The entity's area matches a targeted area
for entry in entities.get_entries_for_area_id(area_id)
# Do not add entities which are hidden or which are config
# or diagnostic entities.
if entry.entity_category is None and entry.hidden_by is None
)
# Add indirectly referenced by device
selected.indirectly_referenced.update(
entry.entity_id
for device_id in selected.referenced_devices
for entry in entities.get_entries_for_device_id(device_id)
# Do not add entities which are hidden or which are config
# or diagnostic entities.
if ent_entry.entity_category is not None or ent_entry.hidden_by is not None:
continue
if (
# The entity's area matches a targeted area
ent_entry.area_id in selector.area_ids
# The entity's device matches a device referenced by an area and the entity
# has no explicitly set area
or (
not ent_entry.area_id
and ent_entry.device_id in selected.referenced_devices
entry.entity_category is None
and entry.hidden_by is None
and (
# The entity's device matches a device referenced
# by an area and the entity
# has no explicitly set area
not entry.area_id
# The entity's device matches a targeted device
or device_id in selector.device_ids
)
# The entity's device matches a targeted device
or ent_entry.device_id in selector.device_ids
):
selected.indirectly_referenced.add(ent_entry.entity_id)
)
)
return selected