Skip to content

Commit

Permalink
Use call_at for events instead call_later (home-assistant#93431)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>
  • Loading branch information
bdraco and jbouwh authored May 24, 2023
1 parent 30d9d7d commit 5c6ed8f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 5 additions & 4 deletions homeassistant/helpers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -1335,7 +1336,7 @@ def run_action(job: HassJob[[datetime], Coroutine[Any, Any, None] | None]) -> No
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)
Expand All @@ -1346,11 +1347,11 @@ def run_action(job: HassJob[[datetime], Coroutine[Any, Any, None] | None]) -> No
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()

Expand Down Expand Up @@ -1382,7 +1383,7 @@ def run_action(job: HassJob[[datetime], Coroutine[Any, Any, None] | None]) -> No
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:
Expand Down
6 changes: 2 additions & 4 deletions tests/auth/test_init.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,10 +31,8 @@


@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


Expand Down

0 comments on commit 5c6ed8f

Please sign in to comment.