Improve tests/patch_time.py (#122017)
This commit is contained in:
parent
dbe68ca057
commit
ea29c2c4d4
1 changed files with 14 additions and 5 deletions
|
@ -5,9 +5,7 @@ from __future__ import annotations
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from homeassistant import runner, util
|
# Do not add any Home Assistant import here
|
||||||
from homeassistant.helpers import event as event_helper
|
|
||||||
from homeassistant.util import dt as dt_util
|
|
||||||
|
|
||||||
|
|
||||||
def _utcnow() -> datetime.datetime:
|
def _utcnow() -> datetime.datetime:
|
||||||
|
@ -20,10 +18,21 @@ def _monotonic() -> float:
|
||||||
return time.monotonic()
|
return time.monotonic()
|
||||||
|
|
||||||
|
|
||||||
# Replace partial functions which are not found by freezegun
|
# Before importing any other Home Assistant functionality, import and replace
|
||||||
|
# partial dt_util.utcnow with a regular function which can be found by freezegun
|
||||||
|
from homeassistant import util # noqa: E402
|
||||||
|
from homeassistant.util import dt as dt_util # noqa: E402
|
||||||
|
|
||||||
dt_util.utcnow = _utcnow # type: ignore[assignment]
|
dt_util.utcnow = _utcnow # type: ignore[assignment]
|
||||||
event_helper.time_tracker_utcnow = _utcnow # type: ignore[assignment]
|
|
||||||
util.utcnow = _utcnow # type: ignore[assignment]
|
util.utcnow = _utcnow # type: ignore[assignment]
|
||||||
|
|
||||||
|
|
||||||
|
# Import other Home Assistant functionality which we need to patch
|
||||||
|
from homeassistant import runner # noqa: E402
|
||||||
|
from homeassistant.helpers import event as event_helper # noqa: E402
|
||||||
|
|
||||||
|
# Replace partial functions which are not found by freezegun
|
||||||
|
event_helper.time_tracker_utcnow = _utcnow # type: ignore[assignment]
|
||||||
|
|
||||||
# Replace bound methods which are not found by freezegun
|
# Replace bound methods which are not found by freezegun
|
||||||
runner.monotonic = _monotonic # type: ignore[assignment]
|
runner.monotonic = _monotonic # type: ignore[assignment]
|
||||||
|
|
Loading…
Add table
Reference in a new issue