forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_time.py
More file actions
102 lines (69 loc) · 3.2 KB
/
Copy pathpatch_time.py
File metadata and controls
102 lines (69 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Patch time related functions."""
import datetime
import time
import freezegun
def ha_datetime_to_fakedatetime(datetime) -> freezegun.api.FakeDatetime: # type: ignore[name-defined]
"""Convert datetime to FakeDatetime.
Modified to include https://github.com/spulec/freezegun/pull/424.
"""
return freezegun.api.FakeDatetime( # type: ignore[attr-defined]
datetime.year,
datetime.month,
datetime.day,
datetime.hour,
datetime.minute,
datetime.second,
datetime.microsecond,
datetime.tzinfo,
fold=datetime.fold,
)
class HAFakeDateMeta(freezegun.api.FakeDateMeta):
"""Modified to override the string representation."""
def __str__(cls) -> str: # noqa: N805 (ruff doesn't know this is a metaclass)
"""Return the string representation of the class."""
return "<class 'datetime.date'>"
class HAFakeDate(freezegun.api.FakeDate, metaclass=HAFakeDateMeta): # type: ignore[name-defined]
"""Modified to improve class str."""
class HAFakeDatetimeMeta(freezegun.api.FakeDatetimeMeta):
"""Modified to override the string representation."""
def __str__(cls) -> str: # noqa: N805 (ruff doesn't know this is a metaclass)
"""Return the string representation of the class."""
return "<class 'datetime.datetime'>"
class HAFakeDatetime(freezegun.api.FakeDatetime, metaclass=HAFakeDatetimeMeta): # type: ignore[name-defined]
"""Modified to include basic fold support and improve class str.
Fold support submitted to upstream in https://github.com/spulec/freezegun/pull/424.
"""
@classmethod
def now(cls, tz=None):
"""Return frozen now."""
now = cls._time_to_freeze() or freezegun.api.real_datetime.now()
if tz:
result = tz.fromutc(now.replace(tzinfo=tz))
else:
result = now
# Add the _tz_offset only if it's non-zero to preserve fold
if cls._tz_offset():
result += cls._tz_offset()
return ha_datetime_to_fakedatetime(result)
# Needed by Mashumaro
datetime.HAFakeDatetime = HAFakeDatetime
# Do not add any Home Assistant import here
def _utcnow() -> datetime.datetime:
"""Make utcnow patchable by freezegun."""
return datetime.datetime.now(datetime.UTC) # pylint: disable=home-assistant-enforce-utcnow
def _monotonic() -> float:
"""Make monotonic patchable by freezegun."""
return time.monotonic()
# 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]
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
runner.monotonic = _monotonic # type: ignore[assignment]