Use call_at for events instead call_later (#93431)

Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>
This commit is contained in:
J. Nick Koston 2023-05-24 14:04:07 -05:00 committed by GitHub
parent 30d9d7d905
commit 5c6ed8f6d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 8 deletions

View file

@ -1322,6 +1322,7 @@ def async_track_point_in_utc_time(
# Since this is called once, we accept a HassJob so we can avoid
# having to figure out how to call the action every time its called.
cancel_callback: asyncio.TimerHandle | None = None
loop = hass.loop
@callback
def run_action(job: HassJob[[datetime], Coroutine[Any, Any, None] | None]) -> None:
@ -1335,7 +1336,7 @@ def async_track_point_in_utc_time(
if (delta := (expected_fire_timestamp - time_tracker_timestamp())) > 0:
_LOGGER.debug("Called %f seconds too early, rearming", delta)
cancel_callback = hass.loop.call_later(delta, run_action, job)
cancel_callback = loop.call_at(loop.time() + delta, run_action, job)
return
hass.async_run_hass_job(job, utc_point_in_time)
@ -1346,11 +1347,11 @@ def async_track_point_in_utc_time(
else HassJob(action, f"track point in utc time {utc_point_in_time}")
)
delta = expected_fire_timestamp - time.time()
cancel_callback = hass.loop.call_later(delta, run_action, job)
cancel_callback = loop.call_at(loop.time() + delta, run_action, job)
@callback
def unsub_point_in_time_listener() -> None:
"""Cancel the call_later."""
"""Cancel the call_at."""
assert cancel_callback is not None
cancel_callback.cancel()
@ -1382,7 +1383,7 @@ def async_call_later(
if isinstance(action, HassJob)
else HassJob(action, f"call_later {delay}")
)
cancel_callback = hass.loop.call_later(delay, run_action, job)
cancel_callback = hass.loop.call_at(hass.loop.time() + delay, run_action, job)
@callback
def unsub_call_later_listener() -> None:

View file

@ -1,7 +1,7 @@
"""Tests for the Home Assistant auth module."""
from datetime import timedelta
from typing import Any
from unittest.mock import Mock, patch
from unittest.mock import patch
from freezegun import freeze_time
import jwt
@ -31,10 +31,8 @@ from tests.common import (
@pytest.fixture
def mock_hass(event_loop):
def mock_hass(hass: HomeAssistant) -> HomeAssistant:
"""Home Assistant mock with minimum amount of data set to make it work with auth."""
hass = Mock()
hass.config.skip_pip = True
return hass