ha-lighting-engine 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ha_lighting_engine-0.2.1/PKG-INFO +13 -0
- ha_lighting_engine-0.2.1/README.md +5 -0
- ha_lighting_engine-0.2.1/pyproject.toml +52 -0
- ha_lighting_engine-0.2.1/pyproject.toml.orig +46 -0
- ha_lighting_engine-0.2.1/src/ha_lighting_engine/__init__.py +10 -0
- ha_lighting_engine-0.2.1/src/ha_lighting_engine/arbitration.py +67 -0
- ha_lighting_engine-0.2.1/src/ha_lighting_engine/engine.py +188 -0
- ha_lighting_engine-0.2.1/src/ha_lighting_engine/request.py +182 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: ha-lighting-engine
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Light leasing/layering capabilities for Home Assistant
|
|
5
|
+
Author: TColl
|
|
6
|
+
Requires-Python: >=3.14
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
# ha-lighting-engine
|
|
10
|
+
|
|
11
|
+
[](https://github.com/TColl/ha-lighting-engine/tree/python-coverage-comment-action-data)
|
|
12
|
+
|
|
13
|
+
Light leasing/layering capabilities for Home Assistant
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# ha-lighting-engine
|
|
2
|
+
|
|
3
|
+
[](https://github.com/TColl/ha-lighting-engine/tree/python-coverage-comment-action-data)
|
|
4
|
+
|
|
5
|
+
Light leasing/layering capabilities for Home Assistant
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ha-lighting-engine"
|
|
3
|
+
version = "0.2.1"
|
|
4
|
+
description = "Light leasing/layering capabilities for Home Assistant"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
dependencies = []
|
|
8
|
+
|
|
9
|
+
[[project.authors]]
|
|
10
|
+
name = "TColl"
|
|
11
|
+
|
|
12
|
+
[project.scripts]
|
|
13
|
+
ha-lighting-engine = "ha_lighting_engine:main"
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["uv_build>=0.13.10,<0.14.0"]
|
|
17
|
+
build-backend = "uv_build"
|
|
18
|
+
|
|
19
|
+
[dependency-groups]
|
|
20
|
+
dev = [
|
|
21
|
+
"coverage>=7.18.0",
|
|
22
|
+
"pytest>=9.2.0",
|
|
23
|
+
"pytest-cov>=7.1.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[tool.ruff]
|
|
27
|
+
line-length = 88
|
|
28
|
+
indent-width = 4
|
|
29
|
+
|
|
30
|
+
[tool.ruff.lint]
|
|
31
|
+
select = [
|
|
32
|
+
"E",
|
|
33
|
+
"F",
|
|
34
|
+
"I",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[tool.pytest]
|
|
38
|
+
pythonpath = ["src"]
|
|
39
|
+
testpaths = ["tests"]
|
|
40
|
+
addopts = [
|
|
41
|
+
"--cov=src",
|
|
42
|
+
"-vv",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.coverage.run]
|
|
46
|
+
source = ["src/ha_lighting_engine"]
|
|
47
|
+
relative_files = true
|
|
48
|
+
branch = true
|
|
49
|
+
|
|
50
|
+
[tool.coverage.report]
|
|
51
|
+
show_missing = true
|
|
52
|
+
fail_under = 100
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ha-lighting-engine"
|
|
3
|
+
version = "0.2.1" # x-release-please-version
|
|
4
|
+
description = "Light leasing/layering capabilities for Home Assistant"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "TColl" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.14"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[project.scripts]
|
|
13
|
+
ha-lighting-engine = "ha_lighting_engine:main"
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["uv_build>=0.13.10,<0.14.0"]
|
|
17
|
+
build-backend = "uv_build"
|
|
18
|
+
|
|
19
|
+
[dependency-groups]
|
|
20
|
+
dev = [
|
|
21
|
+
"coverage>=7.18.0",
|
|
22
|
+
"pytest>=9.2.0",
|
|
23
|
+
"pytest-cov>=7.1.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[tool.ruff]
|
|
27
|
+
# Same as Black:
|
|
28
|
+
line-length = 88
|
|
29
|
+
indent-width = 4
|
|
30
|
+
|
|
31
|
+
[tool.ruff.lint]
|
|
32
|
+
select = ["E", "F", "I"]
|
|
33
|
+
|
|
34
|
+
[tool.pytest]
|
|
35
|
+
pythonpath=["src"]
|
|
36
|
+
testpaths=["tests"]
|
|
37
|
+
addopts = ["--cov=src", "-vv"]
|
|
38
|
+
|
|
39
|
+
[tool.coverage.run]
|
|
40
|
+
source = ["src/ha_lighting_engine"]
|
|
41
|
+
relative_files=true
|
|
42
|
+
branch = true
|
|
43
|
+
|
|
44
|
+
[tool.coverage.report]
|
|
45
|
+
show_missing = true
|
|
46
|
+
fail_under = 100
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Decides the effective state via priority resolution across all active requests."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict, dataclass
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from collections.abc import Iterable
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class Instruction:
|
|
12
|
+
"""New settings to apply per resolved entity."""
|
|
13
|
+
|
|
14
|
+
state: bool | None = None
|
|
15
|
+
brightness: int | None = None
|
|
16
|
+
color_temp_kelvin: int | None = None
|
|
17
|
+
|
|
18
|
+
def as_dict(self) -> dict:
|
|
19
|
+
"""Serialise the instruction."""
|
|
20
|
+
return asdict(self)
|
|
21
|
+
|
|
22
|
+
def __len__(self) -> int:
|
|
23
|
+
"""Get the length of the instruction."""
|
|
24
|
+
return len(self.as_dict())
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def calculate_effective_state(
|
|
28
|
+
requests: Iterable,
|
|
29
|
+
) -> Instruction:
|
|
30
|
+
"""Calculate the effective final state across all requests.
|
|
31
|
+
|
|
32
|
+
Higher priority wins; newer requests win where priority ties.
|
|
33
|
+
Each attribute is independently inherited from the
|
|
34
|
+
highest-priority request that specifies it.
|
|
35
|
+
"""
|
|
36
|
+
ordered = sorted(
|
|
37
|
+
requests,
|
|
38
|
+
key=lambda request: (
|
|
39
|
+
request.priority,
|
|
40
|
+
request.updated_at,
|
|
41
|
+
),
|
|
42
|
+
reverse=True,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
result = Instruction()
|
|
46
|
+
resolved = set()
|
|
47
|
+
|
|
48
|
+
for request in ordered:
|
|
49
|
+
if "state" not in resolved and request.state is not None:
|
|
50
|
+
result.state = request.state
|
|
51
|
+
resolved.add("state")
|
|
52
|
+
|
|
53
|
+
if "brightness" not in resolved and request.brightness is not None:
|
|
54
|
+
result.brightness = request.brightness
|
|
55
|
+
resolved.add("brightness")
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
"color_temp_kelvin" not in resolved
|
|
59
|
+
and request.color_temp_kelvin is not None
|
|
60
|
+
):
|
|
61
|
+
result.color_temp_kelvin = request.color_temp_kelvin
|
|
62
|
+
resolved.add("color_temp_kelvin")
|
|
63
|
+
|
|
64
|
+
if len(resolved) == len(result):
|
|
65
|
+
break
|
|
66
|
+
|
|
67
|
+
return result
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""Request management and reconciliation."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from datetime import UTC, datetime
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .arbitration import Instruction, calculate_effective_state
|
|
8
|
+
from .request import Request
|
|
9
|
+
|
|
10
|
+
type InstructionSet = dict[str, Instruction]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Engine:
|
|
14
|
+
"""Main logic engine."""
|
|
15
|
+
|
|
16
|
+
requests: dict[str, Request]
|
|
17
|
+
last_effective_states: InstructionSet
|
|
18
|
+
|
|
19
|
+
def __init__(self) -> None:
|
|
20
|
+
"""Create the engine."""
|
|
21
|
+
|
|
22
|
+
self.requests = {}
|
|
23
|
+
self.last_effective_states = {}
|
|
24
|
+
|
|
25
|
+
def request(
|
|
26
|
+
self,
|
|
27
|
+
request_id: str,
|
|
28
|
+
entity_ids: Iterable[str],
|
|
29
|
+
priority: int,
|
|
30
|
+
state: bool | None = None,
|
|
31
|
+
brightness: int | None = None,
|
|
32
|
+
color_temp_kelvin: int | None = None,
|
|
33
|
+
duration: int | None = -1,
|
|
34
|
+
source: str | None = None,
|
|
35
|
+
) -> InstructionSet:
|
|
36
|
+
"""Create or update a lighting request."""
|
|
37
|
+
if request_id not in self.requests:
|
|
38
|
+
self.requests[request_id] = Request.create(
|
|
39
|
+
request_id=request_id,
|
|
40
|
+
entity_ids=entity_ids,
|
|
41
|
+
priority=priority,
|
|
42
|
+
state=state,
|
|
43
|
+
brightness=brightness,
|
|
44
|
+
color_temp_kelvin=color_temp_kelvin,
|
|
45
|
+
duration=duration if duration != -1 else None,
|
|
46
|
+
source=source,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
else:
|
|
50
|
+
request = self.requests[request_id]
|
|
51
|
+
|
|
52
|
+
update_values: dict[str, Any] = {
|
|
53
|
+
"entity_ids": entity_ids,
|
|
54
|
+
"priority": priority,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if state is not None:
|
|
58
|
+
update_values["state"] = state
|
|
59
|
+
|
|
60
|
+
if brightness is not None:
|
|
61
|
+
update_values["brightness"] = brightness
|
|
62
|
+
|
|
63
|
+
if color_temp_kelvin is not None:
|
|
64
|
+
update_values["color_temp_kelvin"] = color_temp_kelvin
|
|
65
|
+
|
|
66
|
+
if duration != -1:
|
|
67
|
+
update_values["duration"] = duration
|
|
68
|
+
|
|
69
|
+
if source is not None:
|
|
70
|
+
update_values["source"] = source
|
|
71
|
+
|
|
72
|
+
request.update(**update_values)
|
|
73
|
+
|
|
74
|
+
return self.reconcile()
|
|
75
|
+
|
|
76
|
+
def cancel(self, request_id: str) -> InstructionSet:
|
|
77
|
+
"""Remove a request by ID."""
|
|
78
|
+
self.requests.pop(request_id, None)
|
|
79
|
+
|
|
80
|
+
return self.reconcile()
|
|
81
|
+
|
|
82
|
+
def clear(
|
|
83
|
+
self,
|
|
84
|
+
source: str | None = None,
|
|
85
|
+
entity_id: str | None = None,
|
|
86
|
+
) -> InstructionSet:
|
|
87
|
+
"""Clear all requests (in totality, or per source/entity)."""
|
|
88
|
+
to_remove = []
|
|
89
|
+
|
|
90
|
+
for request_id, request in self.requests.items():
|
|
91
|
+
if source is not None and request.source != source:
|
|
92
|
+
continue
|
|
93
|
+
|
|
94
|
+
if entity_id is None:
|
|
95
|
+
to_remove.append(request_id)
|
|
96
|
+
continue
|
|
97
|
+
elif entity_id in request.entity_ids:
|
|
98
|
+
request.entity_ids.discard(entity_id)
|
|
99
|
+
|
|
100
|
+
if len(request.entity_ids) == 0:
|
|
101
|
+
to_remove.append(request_id)
|
|
102
|
+
|
|
103
|
+
for request_id in to_remove:
|
|
104
|
+
del self.requests[request_id]
|
|
105
|
+
|
|
106
|
+
return self.reconcile()
|
|
107
|
+
|
|
108
|
+
def expire(self, now: datetime | None = None) -> InstructionSet:
|
|
109
|
+
"""Remove expired requests and recalculate affected lights."""
|
|
110
|
+
now = now or datetime.now(UTC)
|
|
111
|
+
|
|
112
|
+
expired = [
|
|
113
|
+
request_id
|
|
114
|
+
for request_id, request in self.requests.items()
|
|
115
|
+
if request.is_expired(now)
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
for request_id in expired:
|
|
119
|
+
del self.requests[request_id]
|
|
120
|
+
|
|
121
|
+
if not expired:
|
|
122
|
+
return {}
|
|
123
|
+
|
|
124
|
+
return self.reconcile()
|
|
125
|
+
|
|
126
|
+
def reconcile(self) -> InstructionSet:
|
|
127
|
+
"""Calculate effective state for every light."""
|
|
128
|
+
entity_ids = set()
|
|
129
|
+
|
|
130
|
+
for request in self.requests.values():
|
|
131
|
+
entity_ids.update(request.entity_ids)
|
|
132
|
+
|
|
133
|
+
changes: InstructionSet = {}
|
|
134
|
+
|
|
135
|
+
for entity_id in entity_ids:
|
|
136
|
+
applicable = [
|
|
137
|
+
request
|
|
138
|
+
for request in self.requests.values()
|
|
139
|
+
if entity_id in request.entity_ids
|
|
140
|
+
]
|
|
141
|
+
|
|
142
|
+
effective = calculate_effective_state(applicable)
|
|
143
|
+
previous = self.last_effective_states.get(entity_id)
|
|
144
|
+
|
|
145
|
+
if effective != previous:
|
|
146
|
+
changes[entity_id] = effective
|
|
147
|
+
self.last_effective_states[entity_id] = effective
|
|
148
|
+
|
|
149
|
+
for entity_id in list(self.last_effective_states):
|
|
150
|
+
if entity_id not in entity_ids:
|
|
151
|
+
del self.last_effective_states[entity_id]
|
|
152
|
+
|
|
153
|
+
return changes
|
|
154
|
+
|
|
155
|
+
def get_requests(self, entity_id: str | None = None) -> dict[str, dict[str, Any]]:
|
|
156
|
+
"""Return active requests, optionally filtered by light."""
|
|
157
|
+
if entity_id is None:
|
|
158
|
+
return {k: v.to_dict() for k, v in self.requests.items()}
|
|
159
|
+
|
|
160
|
+
requests = {}
|
|
161
|
+
for request_id, request in self.requests.items():
|
|
162
|
+
if entity_id in request.entity_ids:
|
|
163
|
+
requests[request_id] = request.to_dict()
|
|
164
|
+
|
|
165
|
+
return requests
|
|
166
|
+
|
|
167
|
+
def export_state(self) -> dict[str, dict[str, Any]]:
|
|
168
|
+
"""Return a serialisable representation of all requests."""
|
|
169
|
+
return {
|
|
170
|
+
request_id: request.to_dict()
|
|
171
|
+
for request_id, request in self.requests.items()
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
def restore_state(self, data: dict, now: datetime | None = None) -> InstructionSet:
|
|
175
|
+
"""Restore requests from previously exported data."""
|
|
176
|
+
self.requests = {}
|
|
177
|
+
|
|
178
|
+
for request_id, request_data in data.items():
|
|
179
|
+
request = Request.from_dict(request_data)
|
|
180
|
+
|
|
181
|
+
if request.is_expired(now or datetime.now(UTC)):
|
|
182
|
+
continue
|
|
183
|
+
|
|
184
|
+
self.requests[request_id] = request
|
|
185
|
+
|
|
186
|
+
self.last_effective_states = {}
|
|
187
|
+
|
|
188
|
+
return self.reconcile()
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Request model and serialisation."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from datetime import UTC, datetime, timedelta
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class Request:
|
|
11
|
+
"""A request."""
|
|
12
|
+
|
|
13
|
+
request_id: str
|
|
14
|
+
entity_ids: set[str]
|
|
15
|
+
priority: int
|
|
16
|
+
|
|
17
|
+
created_at: datetime = field(init=False)
|
|
18
|
+
updated_at: datetime = field(init=False)
|
|
19
|
+
expires_at: datetime | None = None
|
|
20
|
+
|
|
21
|
+
source: str | None = None
|
|
22
|
+
state: bool | None = None
|
|
23
|
+
brightness: int | None = None
|
|
24
|
+
color_temp_kelvin: int | None = None
|
|
25
|
+
|
|
26
|
+
def __post_init__(self) -> None:
|
|
27
|
+
now = datetime.now(UTC)
|
|
28
|
+
self.created_at = self.updated_at = now
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def create(
|
|
32
|
+
cls,
|
|
33
|
+
request_id: str,
|
|
34
|
+
entity_ids: Iterable[str],
|
|
35
|
+
priority: int,
|
|
36
|
+
state: bool | None = None,
|
|
37
|
+
brightness: int | None = None,
|
|
38
|
+
color_temp_kelvin: int | None = None,
|
|
39
|
+
duration: int | None = None,
|
|
40
|
+
source: str | None = None,
|
|
41
|
+
) -> Request:
|
|
42
|
+
"""Create a new request."""
|
|
43
|
+
now = datetime.now(UTC)
|
|
44
|
+
|
|
45
|
+
expires_at = None
|
|
46
|
+
if duration is not None:
|
|
47
|
+
expires_at = now + timedelta(seconds=float(duration))
|
|
48
|
+
|
|
49
|
+
if request_id == "":
|
|
50
|
+
msg = "request_id must not be empty"
|
|
51
|
+
raise ValueError(msg)
|
|
52
|
+
|
|
53
|
+
if len(list(entity_ids)) == 0:
|
|
54
|
+
msg = "At least one entity_id is required"
|
|
55
|
+
raise ValueError(msg)
|
|
56
|
+
entity_ids = set(entity_ids)
|
|
57
|
+
|
|
58
|
+
for entity_id in entity_ids:
|
|
59
|
+
if not isinstance(entity_id, str):
|
|
60
|
+
msg = "entity_ids must contain strings"
|
|
61
|
+
raise TypeError(msg)
|
|
62
|
+
|
|
63
|
+
if brightness is not None:
|
|
64
|
+
brightness = int(brightness)
|
|
65
|
+
if not 1 <= brightness <= 255:
|
|
66
|
+
msg = "brightness must be between 1 and 255"
|
|
67
|
+
raise ValueError(msg)
|
|
68
|
+
|
|
69
|
+
if color_temp_kelvin is not None:
|
|
70
|
+
color_temp_kelvin = int(color_temp_kelvin)
|
|
71
|
+
if color_temp_kelvin <= 0:
|
|
72
|
+
msg = "color_temp_kelvin must be positive"
|
|
73
|
+
raise ValueError(msg)
|
|
74
|
+
|
|
75
|
+
if state is not None and not isinstance(state, bool):
|
|
76
|
+
msg = "state must be True, False or None"
|
|
77
|
+
raise TypeError(msg)
|
|
78
|
+
|
|
79
|
+
return cls(
|
|
80
|
+
request_id=request_id,
|
|
81
|
+
entity_ids=set(entity_ids),
|
|
82
|
+
priority=int(priority),
|
|
83
|
+
source=source,
|
|
84
|
+
state=state,
|
|
85
|
+
brightness=brightness,
|
|
86
|
+
color_temp_kelvin=color_temp_kelvin,
|
|
87
|
+
expires_at=expires_at,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
def update(self, **kwargs) -> None:
|
|
91
|
+
"""Update supplied fields only."""
|
|
92
|
+
now = datetime.now(UTC)
|
|
93
|
+
|
|
94
|
+
for input_field in (
|
|
95
|
+
"entity_ids",
|
|
96
|
+
"priority",
|
|
97
|
+
"source",
|
|
98
|
+
"state",
|
|
99
|
+
"brightness",
|
|
100
|
+
"color_temp_kelvin",
|
|
101
|
+
):
|
|
102
|
+
if input_field in kwargs:
|
|
103
|
+
value = kwargs[input_field]
|
|
104
|
+
|
|
105
|
+
if input_field == "entity_ids":
|
|
106
|
+
value = set(value)
|
|
107
|
+
|
|
108
|
+
setattr(self, input_field, value)
|
|
109
|
+
|
|
110
|
+
# switch off if brightness has been set to 0:
|
|
111
|
+
if "brightness" in kwargs:
|
|
112
|
+
brightness = kwargs["brightness"]
|
|
113
|
+
if brightness == 0:
|
|
114
|
+
self.state = False
|
|
115
|
+
|
|
116
|
+
if "duration" in kwargs:
|
|
117
|
+
duration = kwargs["duration"]
|
|
118
|
+
|
|
119
|
+
if duration is None:
|
|
120
|
+
self.expires_at = None
|
|
121
|
+
else:
|
|
122
|
+
self.expires_at = now + timedelta(seconds=float(duration))
|
|
123
|
+
|
|
124
|
+
self.updated_at = now
|
|
125
|
+
|
|
126
|
+
def is_expired(self, now: datetime | None = None) -> bool:
|
|
127
|
+
"""Check whether this request has lapsed, if it was a lease.
|
|
128
|
+
|
|
129
|
+
Supports injecting 'now' values for the purposes of unit testing.
|
|
130
|
+
"""
|
|
131
|
+
if self.expires_at is None:
|
|
132
|
+
return False
|
|
133
|
+
|
|
134
|
+
return self.expires_at <= (now or datetime.now(UTC))
|
|
135
|
+
|
|
136
|
+
def to_dict(self) -> dict:
|
|
137
|
+
"""Serialise the request for storage."""
|
|
138
|
+
return {
|
|
139
|
+
"request_id": self.request_id,
|
|
140
|
+
"entity_ids": sorted(self.entity_ids),
|
|
141
|
+
"priority": self.priority,
|
|
142
|
+
"source": self.source,
|
|
143
|
+
"state": self.state,
|
|
144
|
+
"brightness": self.brightness,
|
|
145
|
+
"color_temp_kelvin": self.color_temp_kelvin,
|
|
146
|
+
"created_at": (self.created_at.isoformat() if self.created_at else None),
|
|
147
|
+
"updated_at": (self.updated_at.isoformat() if self.updated_at else None),
|
|
148
|
+
"expires_at": (self.expires_at.isoformat() if self.expires_at else None),
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@classmethod
|
|
152
|
+
def from_dict(cls, data: dict) -> Request:
|
|
153
|
+
"""Restore a request from its serialised state."""
|
|
154
|
+
|
|
155
|
+
def parse_datetime(value: Any | None) -> datetime | None:
|
|
156
|
+
if value is None:
|
|
157
|
+
return None
|
|
158
|
+
|
|
159
|
+
parsed = datetime.fromisoformat(value)
|
|
160
|
+
|
|
161
|
+
if parsed.tzinfo is None:
|
|
162
|
+
parsed = parsed.replace(tzinfo=UTC)
|
|
163
|
+
|
|
164
|
+
return parsed
|
|
165
|
+
|
|
166
|
+
restored = cls(
|
|
167
|
+
request_id=data["request_id"],
|
|
168
|
+
entity_ids=set(data["entity_ids"]),
|
|
169
|
+
priority=int(data["priority"]),
|
|
170
|
+
source=data.get("source"),
|
|
171
|
+
state=data.get("state"),
|
|
172
|
+
brightness=data.get("brightness"),
|
|
173
|
+
color_temp_kelvin=data.get("color_temp_kelvin"),
|
|
174
|
+
expires_at=parse_datetime(data.get("expires_at")),
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
if creation_timestamp := parse_datetime(data.get("created_at")):
|
|
178
|
+
restored.created_at = creation_timestamp
|
|
179
|
+
|
|
180
|
+
if update_timestamp := parse_datetime(data.get("updated_at")):
|
|
181
|
+
restored.updated_at = update_timestamp
|
|
182
|
+
return restored
|