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`.
pull/89456/head^2
J. Nick Koston 2023-04-14 15:02:54 -10:00 committed by GitHub
parent 5ffd833fdf
commit 56cc6633f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -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

View File

@ -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: