1
mirror of https://github.com/home-assistant/core synced 2024-07-21 14:24:50 +02:00
ha-core/tests/patch_time.py
J. Nick Koston 790c1bc251
Decrease event loop latency by binding time.monotonic to loop.time directly (#98288)
* Decrease event loop latency by binding time.monotonic to loop.time directly

This is a small improvment to decrease event loop latency. While the goal is
is to reduce Bluetooth connection time latency, everything using asyncio
is a bit more responsive as a result.

* relo per comments

* fix too fast by adding resolution, ensure monotonic time is patchable by freezegun

* fix test that freezes time too late and has a race loop
2023-08-13 20:37:45 -04:00

24 lines
585 B
Python

"""Patch time related functions."""
from __future__ import annotations
import datetime
import time
from homeassistant import runner, util
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()
dt_util.utcnow = _utcnow # type: ignore[assignment]
util.utcnow = _utcnow # type: ignore[assignment]
runner.monotonic = _monotonic # type: ignore[assignment]