Skip to content

Commit

Permalink
Speed up async_active_zone by avoiding dict lookups (#93427)
Browse files Browse the repository at this point in the history
Speed up async_active_zone by avoding dict lookups
  • Loading branch information
bdraco committed May 26, 2023
1 parent accee4b commit 10aa49b
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions homeassistant/components/zone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections.abc import Callable
import logging
from operator import attrgetter
from typing import Any, cast

from typing_extensions import Self
Expand Down Expand Up @@ -96,6 +97,8 @@ def empty_value(value: Any) -> Any:
STORAGE_KEY = DOMAIN
STORAGE_VERSION = 1

ENTITY_ID_SORTER = attrgetter("entity_id")


@bind_hass
def async_active_zone(
Expand All @@ -106,15 +109,10 @@ def async_active_zone(
This method must be run in the event loop.
"""
# 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
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):
continue

Expand Down

0 comments on commit 10aa49b

Please sign in to comment.