mirror of
https://github.com/home-assistant/core
synced 2024-09-15 17:29:45 +02:00
Patch dt_util.utcnow earlier (#98050)
This commit is contained in:
parent
ba3f0372f3
commit
a77009c3ca
@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
from collections.abc import AsyncGenerator, Callable, Coroutine, Generator
|
||||
from contextlib import asynccontextmanager
|
||||
import datetime
|
||||
import functools
|
||||
import gc
|
||||
import itertools
|
||||
@ -32,6 +31,9 @@ import pytest_socket
|
||||
import requests_mock
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
# Setup patching if dt_util time functions before any other Home Assistant imports
|
||||
from . import patch_time # noqa: F401, isort:skip
|
||||
|
||||
from homeassistant import core as ha, loader, runner
|
||||
from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
|
||||
from homeassistant.auth.models import Credentials
|
||||
@ -53,7 +55,6 @@ from homeassistant.helpers import (
|
||||
config_entry_oauth2_flow,
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
event,
|
||||
issue_registry as ir,
|
||||
recorder as recorder_helper,
|
||||
)
|
||||
@ -109,15 +110,6 @@ asyncio.set_event_loop_policy(runner.HassEventLoopPolicy(False))
|
||||
asyncio.set_event_loop_policy = lambda policy: None
|
||||
|
||||
|
||||
def _utcnow() -> datetime.datetime:
|
||||
"""Make utcnow patchable by freezegun."""
|
||||
return datetime.datetime.now(datetime.UTC)
|
||||
|
||||
|
||||
dt_util.utcnow = _utcnow # type: ignore[assignment]
|
||||
event.time_tracker_utcnow = _utcnow # type: ignore[assignment]
|
||||
|
||||
|
||||
def pytest_addoption(parser: pytest.Parser) -> None:
|
||||
"""Register custom pytest options."""
|
||||
parser.addoption("--dburl", action="store", default="sqlite://")
|
||||
|
16
tests/patch_time.py
Normal file
16
tests/patch_time.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""Patch time related functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
|
||||
from homeassistant import util
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
|
||||
def _utcnow() -> datetime.datetime:
|
||||
"""Make utcnow patchable by freezegun."""
|
||||
return datetime.datetime.now(datetime.UTC)
|
||||
|
||||
|
||||
dt_util.utcnow = _utcnow # type: ignore[assignment]
|
||||
util.utcnow = _utcnow # type: ignore[assignment]
|
Loading…
Reference in New Issue
Block a user