core/tests/patch_time.py

29 lines
775 B
Python
Raw Normal View History

2023-08-08 17:16:52 +00:00
"""Patch time related functions."""
2023-08-08 17:16:52 +00:00
from __future__ import annotations
import datetime
import time
2023-08-08 17:16:52 +00:00
from homeassistant import runner, util
from homeassistant.helpers import event as event_helper
2023-08-08 17:16:52 +00:00
from homeassistant.util import dt as dt_util
def _utcnow() -> datetime.datetime:
"""Make utcnow patchable by freezegun."""
return datetime.datetime.now(datetime.UTC)
def _monotonic() -> float:
"""Make monotonic patchable by freezegun."""
return time.monotonic()
# Replace partial functions which are not found by freezegun
2023-08-08 17:16:52 +00:00
dt_util.utcnow = _utcnow # type: ignore[assignment]
event_helper.time_tracker_utcnow = _utcnow # type: ignore[assignment]
2023-08-08 17:16:52 +00:00
util.utcnow = _utcnow # type: ignore[assignment]
runner.monotonic = _monotonic # type: ignore[assignment]