Small performance improvement to template expand (#114086)

* Small performance improvement to template expand

- Avoid fetching entity sources each loop
- Skip already found entities
- Avoid startswith in favor of equality check

* unneeded changes
This commit is contained in:
J. Nick Koston 2024-03-23 18:24:52 -10:00 committed by GitHub
parent f079c1c236
commit de831b6e87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1252,6 +1252,7 @@ def expand(hass: HomeAssistant, *args: Any) -> Iterable[State]:
search = list(args)
found = {}
sources = entity_helper.entity_sources(hass)
while search:
entity = search.pop()
if isinstance(entity, str):
@ -1267,14 +1268,17 @@ def expand(hass: HomeAssistant, *args: Any) -> Iterable[State]:
# ignore other types
continue
if entity_id.startswith(_GROUP_DOMAIN_PREFIX) or (
(source := entity_helper.entity_sources(hass).get(entity_id))
and source["domain"] == "group"
if entity_id in found:
continue
domain = entity.domain
if domain == "group" or (
(source := sources.get(entity_id)) and source["domain"] == "group"
):
# Collect state will be called in here since it's wrapped
if group_entities := entity.attributes.get(ATTR_ENTITY_ID):
search += group_entities
elif entity_id.startswith(_ZONE_DOMAIN_PREFIX):
elif domain == "zone":
if zone_entities := entity.attributes.get(ATTR_PERSONS):
search += zone_entities
else: