-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Expand file tree
/
Copy pathtest_fan.py
More file actions
475 lines (449 loc) · 15 KB
/
test_fan.py
File metadata and controls
475 lines (449 loc) · 15 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
"""Test Matter Fan platform."""
from unittest.mock import MagicMock, call
from matter_server.client.models.node import MatterNode
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.fan import (
ATTR_DIRECTION,
ATTR_OSCILLATING,
ATTR_PERCENTAGE,
ATTR_PRESET_MODE,
DIRECTION_FORWARD,
DIRECTION_REVERSE,
DOMAIN as FAN_DOMAIN,
SERVICE_OSCILLATE,
SERVICE_SET_DIRECTION,
SERVICE_SET_PERCENTAGE,
FanEntityFeature,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .common import (
set_node_attribute,
snapshot_matter_entities,
trigger_subscription_callback,
)
@pytest.mark.usefixtures("matter_devices")
async def test_fans(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test fans."""
snapshot_matter_entities(hass, entity_registry, snapshot, Platform.FAN)
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_fan_base(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test Fan platform."""
entity_id = "fan.mock_air_purifier"
state = hass.states.get(entity_id)
assert state
assert state.attributes["preset_modes"] == [
"low",
"medium",
"high",
"auto",
"natural_wind",
"sleep_wind",
]
assert state.attributes["direction"] == "forward"
assert state.attributes["oscillating"] is False
assert state.attributes["percentage"] is None
assert state.attributes["percentage_step"] == 10
assert state.attributes["preset_mode"] == "auto"
mask = (
FanEntityFeature.DIRECTION
| FanEntityFeature.OSCILLATE
| FanEntityFeature.PRESET_MODE
| FanEntityFeature.SET_SPEED
)
assert state.attributes["supported_features"] & mask == mask
# handle fan mode update
set_node_attribute(matter_node, 1, 514, 0, 1)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["preset_mode"] == "low"
# handle direction update
set_node_attribute(matter_node, 1, 514, 11, 1)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["direction"] == "reverse"
# handle rock/oscillation update
set_node_attribute(matter_node, 1, 514, 8, 1)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["oscillating"] is True
# handle wind mode active translates to correct preset
set_node_attribute(matter_node, 1, 514, 10, 2)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["preset_mode"] == "natural_wind"
set_node_attribute(matter_node, 1, 514, 10, 1)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["preset_mode"] == "sleep_wind"
# set mains power to OFF (OnOff cluster)
set_node_attribute(matter_node, 1, 6, 0, False)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["preset_mode"] is None
assert state.attributes["percentage"] == 0
# test featuremap update
set_node_attribute(matter_node, 1, 514, 65532, 1)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.attributes["supported_features"] & FanEntityFeature.SET_SPEED
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_fan_turn_on_with_percentage(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test turning on the fan with a specific percentage."""
entity_id = "fan.mock_air_purifier"
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id, ATTR_PERCENTAGE: 50},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/2",
value=50,
)
# test again where preset_mode is omitted in the service call
# which should select the last active percentage
matter_client.write_attribute.reset_mock()
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/2",
value=255,
)
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("node_fixture", ["mock_fan"])
async def test_fan_turn_on_with_preset_mode(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test turning on the fan with a specific preset mode."""
entity_id = "fan.mocked_fan_switch"
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id, ATTR_PRESET_MODE: "medium"},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/0",
value=2,
)
# test again with wind feature as preset mode
for preset_mode, value in (("natural_wind", 2), ("sleep_wind", 1)):
matter_client.write_attribute.reset_mock()
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id, ATTR_PRESET_MODE: preset_mode},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/10",
value=value,
)
# test again if wind mode is explicitly turned off when we set a new preset mode
matter_client.write_attribute.reset_mock()
set_node_attribute(matter_node, 1, 514, 10, 2)
await trigger_subscription_callback(hass, matter_client)
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id, ATTR_PRESET_MODE: "medium"},
blocking=True,
)
assert matter_client.write_attribute.call_count == 2
assert matter_client.write_attribute.call_args_list[0] == call(
node_id=matter_node.node_id,
attribute_path="1/514/10",
value=0,
)
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/0",
value=2,
)
# test again where preset_mode is omitted in the service call
# which should select the last active preset
matter_client.write_attribute.reset_mock()
set_node_attribute(matter_node, 1, 514, 0, 1)
set_node_attribute(matter_node, 1, 514, 10, 0)
await trigger_subscription_callback(hass, matter_client)
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/0",
value=1,
)
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_fan_turn_off(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test turning off the fan."""
entity_id = "fan.mock_air_purifier"
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/0",
value=0,
)
matter_client.write_attribute.reset_mock()
# test again if wind mode is turned off
set_node_attribute(matter_node, 1, 514, 10, 2)
await trigger_subscription_callback(hass, matter_client)
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
assert matter_client.write_attribute.call_count == 2
assert matter_client.write_attribute.call_args_list[0] == call(
node_id=matter_node.node_id,
attribute_path="1/514/10",
value=0,
)
assert matter_client.write_attribute.call_args_list[1] == call(
node_id=matter_node.node_id,
attribute_path="1/514/0",
value=0,
)
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_fan_oscillate(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test oscillating the fan."""
entity_id = "fan.mock_air_purifier"
for oscillating, value in ((True, 1), (False, 0)):
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_OSCILLATE,
{ATTR_ENTITY_ID: entity_id, ATTR_OSCILLATING: oscillating},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/8",
value=value,
)
matter_client.write_attribute.reset_mock()
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_fan_set_direction(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test oscillating the fan."""
entity_id = "fan.mock_air_purifier"
for direction, value in ((DIRECTION_FORWARD, 0), (DIRECTION_REVERSE, 1)):
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_SET_DIRECTION,
{ATTR_ENTITY_ID: entity_id, ATTR_DIRECTION: direction},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/11",
value=value,
)
matter_client.write_attribute.reset_mock()
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize(
("node_fixture", "entity_id", "attributes", "features"),
[
(
"mock_fan",
"fan.mocked_fan_switch",
{
"1/514/65532": 0,
},
(FanEntityFeature.TURN_ON | FanEntityFeature.TURN_OFF),
),
(
"mock_fan",
"fan.mocked_fan_switch",
{
"1/514/65532": 1,
},
(
FanEntityFeature.TURN_ON
| FanEntityFeature.TURN_OFF
| FanEntityFeature.SET_SPEED
),
),
(
"mock_fan",
"fan.mocked_fan_switch",
{
"1/514/65532": 4,
},
(
FanEntityFeature.TURN_ON
| FanEntityFeature.TURN_OFF
| FanEntityFeature.OSCILLATE
),
),
(
"mock_fan",
"fan.mocked_fan_switch",
{
"1/514/65532": 36,
},
(
FanEntityFeature.TURN_ON
| FanEntityFeature.TURN_OFF
| FanEntityFeature.OSCILLATE
| FanEntityFeature.DIRECTION
),
),
],
)
async def test_fan_supported_features(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
entity_id: str,
features: int,
) -> None:
"""Test if the correct features get discovered from featuremap."""
state = hass.states.get(entity_id)
assert state
assert state.attributes["supported_features"] & features == features
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize(
("node_fixture", "entity_id", "attributes", "preset_modes"),
[
(
"mock_fan",
"fan.mocked_fan_switch",
{"1/514/1": 0, "1/514/65532": 0},
[
"low",
"medium",
"high",
],
),
(
"mock_fan",
"fan.mocked_fan_switch",
{"1/514/1": 1, "1/514/65532": 0},
[
"low",
"high",
],
),
(
"mock_fan",
"fan.mocked_fan_switch",
{"1/514/1": 2, "1/514/65532": 0},
["low", "medium", "high", "auto"],
),
(
"mock_fan",
"fan.mocked_fan_switch",
{"1/514/1": 4, "1/514/65532": 0},
["high", "auto"],
),
(
"mock_fan",
"fan.mocked_fan_switch",
{"1/514/1": 5, "1/514/65532": 0},
["high"],
),
(
"mock_fan",
"fan.mocked_fan_switch",
{"1/514/1": 5, "1/514/65532": 8, "1/514/9": 3},
["high", "natural_wind", "sleep_wind"],
),
],
)
async def test_fan_features(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
entity_id: str,
preset_modes: list[str],
) -> None:
"""Test if the correct presets get discovered from fanmodesequence."""
state = hass.states.get(entity_id)
assert state
assert state.attributes["preset_modes"] == preset_modes
@pytest.mark.parametrize("node_fixture", ["silabs_range_hood"])
async def test_fan_set_percentage_without_multispeed(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test percentage control on a fan without the MultiSpeed feature.
PercentSetting is mandatory in the FanControl cluster regardless of features,
so SET_SPEED must be available and write to PercentSetting (attribute 0x0002).
"""
entity_id = "fan.sl_rangehood"
state = hass.states.get(entity_id)
assert state
assert state.attributes["supported_features"] & FanEntityFeature.SET_SPEED
assert state.attributes["percentage_step"] == 1.0
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_SET_PERCENTAGE,
{ATTR_ENTITY_ID: entity_id, ATTR_PERCENTAGE: 75},
blocking=True,
)
assert matter_client.write_attribute.call_count == 1
assert matter_client.write_attribute.call_args == call(
node_id=matter_node.node_id,
attribute_path="1/514/2",
value=75,
)