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

Improve performance of tracking time changes #91433

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions homeassistant/helpers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,9 +1425,9 @@ def interval_listener(now: datetime) -> None:
hass.async_run_hass_job(job, now)

if name:
job_name = f"{name}: track time interval {interval}"
job_name = f"{name}: track time interval {interval} {action}"
else:
job_name = f"track time interval {interval}"
job_name = f"track time interval {interval} {action}"

interval_listener_job = HassJob(
interval_listener, job_name, cancel_on_shutdown=cancel_on_shutdown
Expand Down Expand Up @@ -1578,23 +1578,30 @@ def calculate_next(now: datetime) -> datetime:
).replace(microsecond=microsecond)

time_listener: CALLBACK_TYPE | None = None
pattern_time_change_listener_job: HassJob[[datetime], Any] | None = None

@callback
def pattern_time_change_listener(_: datetime) -> None:
"""Listen for matching time_changed events."""
nonlocal time_listener
nonlocal pattern_time_change_listener_job

now = time_tracker_utcnow()
hass.async_run_hass_job(job, dt_util.as_local(now) if local else now)
assert pattern_time_change_listener_job is not None

time_listener = async_track_point_in_utc_time(
hass,
pattern_time_change_listener,
pattern_time_change_listener_job,
calculate_next(now + timedelta(seconds=1)),
)

pattern_time_change_listener_job = HassJob(
pattern_time_change_listener,
"time change listener {hour}:{minute}:{second} {action}",
)
time_listener = async_track_point_in_utc_time(
hass, pattern_time_change_listener, calculate_next(dt_util.utcnow())
hass, pattern_time_change_listener_job, calculate_next(dt_util.utcnow())
)

@callback
Expand Down