* 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
23 lines
585 B
Python
23 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]
|