Speed up async_active_zone by avoiding dict lookups (#93427)

Speed up async_active_zone by avoding dict lookups
This commit is contained in:
J. Nick Koston 2023-05-25 20:37:18 -05:00 committed by GitHub
parent accee4b5ef
commit 10aa49be2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
import logging import logging
from operator import attrgetter
from typing import Any, cast from typing import Any, cast
from typing_extensions import Self from typing_extensions import Self
@ -96,6 +97,8 @@ RELOAD_SERVICE_SCHEMA = vol.Schema({})
STORAGE_KEY = DOMAIN STORAGE_KEY = DOMAIN
STORAGE_VERSION = 1 STORAGE_VERSION = 1
ENTITY_ID_SORTER = attrgetter("entity_id")
@bind_hass @bind_hass
def async_active_zone( def async_active_zone(
@ -106,15 +109,10 @@ def async_active_zone(
This method must be run in the event loop. This method must be run in the event loop.
""" """
# Sort entity IDs so that we are deterministic if equal distance to 2 zones # Sort entity IDs so that we are deterministic if equal distance to 2 zones
zones = (
cast(State, hass.states.get(entity_id))
for entity_id in sorted(hass.states.async_entity_ids(DOMAIN))
)
min_dist = None min_dist = None
closest = None closest = None
for zone in zones: for zone in sorted(hass.states.async_all(DOMAIN), key=ENTITY_ID_SORTER):
if zone.state == STATE_UNAVAILABLE or zone.attributes.get(ATTR_PASSIVE): if zone.state == STATE_UNAVAILABLE or zone.attributes.get(ATTR_PASSIVE):
continue continue