Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up async_active_zone by avoiding dict lookups #93427

Merged
merged 1 commit into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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