Skip to content

Commit

Permalink
Yield after calls to timezone_at in update_zones (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner authored Feb 13, 2024
1 parent 2649b57 commit 092c495
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions custom_components/entity_tz/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Entity Time Zone Sensor Helpers."""
from __future__ import annotations

import asyncio
from collections.abc import Container, Mapping
from dataclasses import dataclass
from datetime import tzinfo
Expand Down Expand Up @@ -107,21 +108,23 @@ def init_tz_data() -> None:

await hass.async_add_executor_job(init_tz_data)

@callback
def update_zones(_: Event | None = None) -> None:
async def update_zones(_: Event | None = None) -> None:
"""Update list of zones to use."""
zones = []
for state in hass.states.async_all(ZONE_DOMAIN):
if not_ha_tz(get_tz(hass, state)):
zones.append(state.entity_id)
# get_tz, since it might call timezone_at, can take a while, so give other
# tasks a chance to run.
await asyncio.sleep(0)
etzd.zones = zones

@callback
def zones_filter(event: Event) -> bool:
"""Return if the state changed event is for a zone."""
return split_entity_id(event.data["entity_id"])[0] == ZONE_DOMAIN

update_zones()
await update_zones()
hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, update_zones)
hass.bus.async_listen(EVENT_STATE_CHANGED, update_zones, zones_filter)

Expand Down

0 comments on commit 092c495

Please sign in to comment.