forked from basnijholt/adaptive-lighting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_adaptation_utils.py
340 lines (323 loc) · 9.67 KB
/
test_adaptation_utils.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Tests for Adaptive Lighting utils."""
from unittest.mock import Mock
import pytest
from homeassistant.components.adaptive_lighting.adaptation_utils import (
ServiceData,
_create_service_call_data_iterator,
_has_relevant_service_data_attributes,
_remove_redundant_attributes,
_split_service_call_data,
prepare_adaptation_data,
)
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP_KELVIN,
ATTR_TRANSITION,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_ON
from homeassistant.core import Context, State
@pytest.mark.parametrize(
("input_data", "expected_data_list"),
[
(
{"foo": 1},
[],
),
(
{ATTR_BRIGHTNESS: 10},
[{ATTR_BRIGHTNESS: 10}],
),
(
{ATTR_COLOR_TEMP_KELVIN: 3500},
[{ATTR_COLOR_TEMP_KELVIN: 3500}],
),
(
{ATTR_ENTITY_ID: "foo", ATTR_BRIGHTNESS: 10},
[{ATTR_ENTITY_ID: "foo", ATTR_BRIGHTNESS: 10}],
),
(
{ATTR_BRIGHTNESS: 10, ATTR_COLOR_TEMP_KELVIN: 3500},
[{ATTR_BRIGHTNESS: 10}, {ATTR_COLOR_TEMP_KELVIN: 3500}],
),
(
{ATTR_BRIGHTNESS: 10, ATTR_COLOR_TEMP_KELVIN: 3500, ATTR_TRANSITION: 2},
[
{ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 1},
{ATTR_COLOR_TEMP_KELVIN: 3500, ATTR_TRANSITION: 1},
],
),
(
{ATTR_TRANSITION: 1},
[],
),
],
ids=[
"remove irrelevant attributes",
"brightness only yields one service call",
"color only yields one service call",
"include entity ID",
"brightness and color are split into two with brightness first",
"transition time is distributed among service calls",
"ignore transition time without service calls",
],
)
async def test_split_service_call_data(input_data, expected_data_list):
"""Test splitting of service call data."""
assert _split_service_call_data(input_data) == expected_data_list
@pytest.mark.parametrize(
("service_data", "state", "service_data_expected"),
[
(
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
State("light.test", STATE_ON),
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
),
(
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
State("light.test", STATE_ON, {ATTR_BRIGHTNESS: 10}),
{ATTR_ENTITY_ID: "light.test", ATTR_TRANSITION: 2},
),
(
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
State("light.test", STATE_ON, {ATTR_BRIGHTNESS: 11}),
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
),
],
ids=[
"pass all attributes on empty state",
"remove attributes whose values equal the state",
"keep attributes whose values differ from the state",
],
)
async def test_remove_redundant_attributes(
service_data: ServiceData,
state: State | None,
service_data_expected: ServiceData,
):
"""Test filtering of service data."""
assert _remove_redundant_attributes(service_data, state) == service_data_expected
@pytest.mark.parametrize(
("service_data", "expected_relevant"),
[
(
{ATTR_ENTITY_ID: "light.test"},
False,
),
(
{ATTR_TRANSITION: 2},
False,
),
(
{ATTR_BRIGHTNESS: 10},
True,
),
(
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10, ATTR_TRANSITION: 2},
True,
),
],
)
async def test_has_relevant_service_data_attributes(
service_data: ServiceData,
expected_relevant: bool,
):
"""Test the determination of relevancy of service data."""
assert _has_relevant_service_data_attributes(service_data) == expected_relevant
@pytest.mark.parametrize(
("service_datas", "filter_by_state", "service_datas_expected"),
[
(
[{ATTR_ENTITY_ID: "light.test"}],
False,
[{ATTR_ENTITY_ID: "light.test"}],
),
(
[{ATTR_ENTITY_ID: "light.test"}, {ATTR_ENTITY_ID: "light.test2"}],
False,
[{ATTR_ENTITY_ID: "light.test"}, {ATTR_ENTITY_ID: "light.test2"}],
),
(
[{ATTR_ENTITY_ID: "light.test"}],
True,
[],
),
(
[{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10}],
True,
[],
),
(
[{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 11}],
True,
[{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 11}],
),
(
[
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 11},
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 22},
],
True,
[
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 11},
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 22},
],
),
(
[
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 10},
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 22},
],
True,
[
{ATTR_ENTITY_ID: "light.test", ATTR_BRIGHTNESS: 22},
],
),
],
ids=[
"single item passed through without filtering",
"two items passed through without filtering",
"filter removes item without relevant attributes",
"filter removes item with relevant attribute that equals the state",
"filter keeps item with relevant attribute that is different from state",
"filter keeps two items with relevant attributes that are different from state",
"filter removes item that equals state and keeps items that differs from state",
],
)
async def test_create_service_call_data_iterator(
service_datas: list[ServiceData],
filter_by_state: bool,
service_datas_expected: list[ServiceData],
hass_states_mock,
):
"""Test the generator function for correct enumeration and filtering."""
generated_service_datas = [
data
async for data in _create_service_call_data_iterator(
hass_states_mock,
service_datas,
filter_by_state,
)
]
assert generated_service_datas == service_datas_expected
assert (
hass_states_mock.states.get.call_count == 0
if not filter_by_state
else len(service_datas)
)
@pytest.mark.parametrize(
(
"service_data",
"split",
"filter_by_state",
"service_datas_expected",
"sleep_time_expected",
),
[
(
{
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
ATTR_COLOR_TEMP_KELVIN: 4000,
},
False,
False,
[
{
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
ATTR_COLOR_TEMP_KELVIN: 4000,
},
],
1.2,
),
(
{
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
ATTR_COLOR_TEMP_KELVIN: 4000,
},
True,
False,
[
{
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
},
{
ATTR_ENTITY_ID: "light.test",
ATTR_COLOR_TEMP_KELVIN: 4000,
},
],
0.7,
),
(
{
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
ATTR_COLOR_TEMP_KELVIN: 4000,
},
False,
True,
[
{
ATTR_ENTITY_ID: "light.test",
ATTR_COLOR_TEMP_KELVIN: 4000,
},
],
1.2,
),
(
{
ATTR_ENTITY_ID: "light.test",
ATTR_BRIGHTNESS: 10,
ATTR_COLOR_TEMP_KELVIN: 4000,
},
True,
True,
[
{
ATTR_ENTITY_ID: "light.test",
ATTR_COLOR_TEMP_KELVIN: 4000,
},
],
0.7,
),
],
ids=[
"service data passed through",
"service data split",
"service data filtered",
"service data split and filtered",
],
)
async def test_prepare_adaptation_data(
hass_states_mock,
service_data,
split,
filter_by_state,
service_datas_expected,
sleep_time_expected,
):
"""Test creation of correct service data objects."""
data = prepare_adaptation_data(
hass_states_mock,
"test.entity",
Context(id="test-id"),
1,
0.2,
service_data,
split,
filter_by_state,
force=False,
)
generated_service_datas = [item async for item in data.service_call_datas]
assert data.entity_id == "test.entity"
assert data.context.id == "test-id"
assert data.sleep_time == sleep_time_expected
assert generated_service_datas == service_datas_expected
@pytest.fixture(name="hass_states_mock")
def fixture_hass_states_mock():
"""Mocks a HA state machine which returns a mock state."""
hass = Mock()
hass.states.get.return_value = Mock(attributes={ATTR_BRIGHTNESS: 10})
return hass