From 56cc6633f573d0f66a0daf04fc079b7cad739578 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 14 Apr 2023 15:02:54 -1000 Subject: [PATCH] Use fast path for track_time_change that fires every second (#91432) We were missing a check for `*` and were only checking `None`. Automations use `*`, python code uses `None`. --- homeassistant/helpers/event.py | 2 +- tests/helpers/test_event.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index b7d02b48bd3..c8d02309e3d 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -1553,7 +1553,7 @@ def async_track_utc_time_change( """Add a listener that will fire if time matches a pattern.""" # We do not have to wrap the function with time pattern matching logic # if no pattern given - if all(val is None for val in (hour, minute, second)): + if all(val is None or val == "*" for val in (hour, minute, second)): # Previously this relied on EVENT_TIME_FIRED # which meant it would not fire right away because # the caller would always be misaligned with the call diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 2f08f6e95cc..9d90ef1b26c 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -3666,6 +3666,7 @@ async def test_track_sunset(hass: HomeAssistant) -> None: async def test_async_track_time_change(hass: HomeAssistant) -> None: """Test tracking time change.""" + none_runs = [] wildcard_runs = [] specific_runs = [] @@ -3678,12 +3679,17 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None: with patch( "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away ): - unsub = async_track_time_change( - hass, callback(lambda x: wildcard_runs.append(x)) - ) + unsub = async_track_time_change(hass, callback(lambda x: none_runs.append(x))) unsub_utc = async_track_utc_time_change( hass, callback(lambda x: specific_runs.append(x)), second=[0, 30] ) + unsub_wildcard = async_track_time_change( + hass, + callback(lambda x: wildcard_runs.append(x)), + second="*", + minute="*", + hour="*", + ) async_fire_time_changed( hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999, tzinfo=dt_util.UTC) @@ -3691,6 +3697,7 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert len(specific_runs) == 1 assert len(wildcard_runs) == 1 + assert len(none_runs) == 1 async_fire_time_changed( hass, datetime(now.year + 1, 5, 24, 12, 0, 15, 999999, tzinfo=dt_util.UTC) @@ -3698,6 +3705,7 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert len(specific_runs) == 1 assert len(wildcard_runs) == 2 + assert len(none_runs) == 2 async_fire_time_changed( hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999, tzinfo=dt_util.UTC) @@ -3705,9 +3713,11 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert len(specific_runs) == 2 assert len(wildcard_runs) == 3 + assert len(none_runs) == 3 unsub() unsub_utc() + unsub_wildcard() async_fire_time_changed( hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999, tzinfo=dt_util.UTC) @@ -3715,6 +3725,7 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert len(specific_runs) == 2 assert len(wildcard_runs) == 3 + assert len(none_runs) == 3 async def test_periodic_task_minute(hass: HomeAssistant) -> None: