2023-08-08 19:16:52 +02:00
|
|
|
"""Patch time related functions."""
|
2024-03-08 16:36:11 +01:00
|
|
|
|
2023-08-08 19:16:52 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import datetime
|
2023-08-13 19:37:45 -05:00
|
|
|
import time
|
2023-08-08 19:16:52 +02:00
|
|
|
|
2023-08-13 19:37:45 -05:00
|
|
|
from homeassistant import runner, util
|
2024-04-27 14:08:56 +02:00
|
|
|
from homeassistant.helpers import event as event_helper
|
2023-08-08 19:16:52 +02:00
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
|
|
|
|
|
|
|
def _utcnow() -> datetime.datetime:
|
|
|
|
"""Make utcnow patchable by freezegun."""
|
|
|
|
return datetime.datetime.now(datetime.UTC)
|
|
|
|
|
|
|
|
|
2023-08-13 19:37:45 -05:00
|
|
|
def _monotonic() -> float:
|
|
|
|
"""Make monotonic patchable by freezegun."""
|
|
|
|
return time.monotonic()
|
|
|
|
|
|
|
|
|
2024-04-27 14:08:56 +02:00
|
|
|
# Replace partial functions which are not found by freezegun
|
2023-08-08 19:16:52 +02:00
|
|
|
dt_util.utcnow = _utcnow # type: ignore[assignment]
|
2024-04-27 14:08:56 +02:00
|
|
|
event_helper.time_tracker_utcnow = _utcnow # type: ignore[assignment]
|
2023-08-08 19:16:52 +02:00
|
|
|
util.utcnow = _utcnow # type: ignore[assignment]
|
2024-04-27 14:08:56 +02:00
|
|
|
|
2023-08-13 19:37:45 -05:00
|
|
|
runner.monotonic = _monotonic # type: ignore[assignment]
|