forked from Danielhiversen/flux_led
-
Notifications
You must be signed in to change notification settings - Fork 30
/
tests_aio.py
831 lines (688 loc) · 26.3 KB
/
tests_aio.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
import asyncio
import contextlib
import datetime
import logging
from unittest.mock import MagicMock, call, patch
import pytest
from flux_led import aiodevice
from flux_led.aio import AIOWifiLedBulb
from flux_led.aioprotocol import AIOLEDENETProtocol
from flux_led.aioscanner import AIOBulbScanner, LEDENETDiscovery
from flux_led.const import COLOR_MODE_CCT, COLOR_MODE_RGBWW
from flux_led.protocol import PROTOCOL_LEDENET_9BYTE, PROTOCOL_LEDENET_ORIGINAL
@pytest.fixture
async def mock_discovery_aio_protocol():
"""Fixture to mock an asyncio connection."""
loop = asyncio.get_running_loop()
future = asyncio.Future()
async def _wait_for_connection():
transport, protocol = await future
await asyncio.sleep(0)
await asyncio.sleep(0)
return transport, protocol
async def _mock_create_datagram_endpoint(func, sock=None):
protocol: LEDENETDiscovery = func()
transport = MagicMock()
protocol.connection_made(transport)
with contextlib.suppress(asyncio.InvalidStateError):
future.set_result((transport, protocol))
return transport, protocol
with patch.object(loop, "create_datagram_endpoint", _mock_create_datagram_endpoint):
yield _wait_for_connection
@pytest.fixture
async def mock_aio_protocol():
"""Fixture to mock an asyncio connection."""
loop = asyncio.get_running_loop()
future = asyncio.Future()
async def _wait_for_connection():
transport, protocol = await future
await asyncio.sleep(0)
await asyncio.sleep(0)
return transport, protocol
async def _mock_create_connection(func, ip, port):
protocol: AIOLEDENETProtocol = func()
transport = MagicMock()
protocol.connection_made(transport)
with contextlib.suppress(asyncio.InvalidStateError):
future.set_result((transport, protocol))
return transport, protocol
with patch.object(loop, "create_connection", _mock_create_connection):
yield _wait_for_connection
@pytest.mark.asyncio
async def test_no_initial_response(mock_aio_protocol):
"""Test we try switching protocol if we get no initial response."""
light = AIOWifiLedBulb("192.168.1.166", timeout=0.1)
assert light.protocol is None
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
with pytest.raises(RuntimeError):
await task
assert transport.mock_calls == [
call.get_extra_info("peername"),
call.write(bytearray(b"\x81\x8a\x8b\x96")),
call.write_eof(),
call.close(),
]
assert not light.available
assert light.protocol is PROTOCOL_LEDENET_ORIGINAL
@pytest.mark.asyncio
async def test_invalid_initial_response(mock_aio_protocol):
"""Test we try switching protocol if we an unexpected response."""
light = AIOWifiLedBulb("192.168.1.166", timeout=0.1)
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(b"\x31\x25")
with pytest.raises(RuntimeError):
await task
assert transport.mock_calls == [
call.get_extra_info("peername"),
call.write(bytearray(b"\x81\x8a\x8b\x96")),
call.write_eof(),
call.close(),
]
assert not light.available
@pytest.mark.asyncio
async def test_reassemble(mock_aio_protocol):
"""Test we can reassemble."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
assert light.color_modes == {COLOR_MODE_RGBWW, COLOR_MODE_CCT}
assert light.protocol == PROTOCOL_LEDENET_9BYTE
assert light.model_num == 0x25
assert light.model == "RGB/WW/CW Controller (0x25)"
assert light.is_on is True
assert len(light.effect_list) == 21
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
)
await asyncio.sleep(0)
assert light.is_on is False
light._aio_protocol.data_received(b"\x81")
light._aio_protocol.data_received(
b"\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f"
)
light._aio_protocol.data_received(b"\xde")
await asyncio.sleep(0)
assert light.is_on is True
@pytest.mark.asyncio
async def test_turn_on_off(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
"""Test we can turn on and off."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
task = asyncio.create_task(light.async_turn_off())
# Wait for the future to get added
await asyncio.sleep(0)
light._aio_protocol.data_received(
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
)
await asyncio.sleep(0)
assert light.is_on is False
await task
task = asyncio.create_task(light.async_turn_on())
await asyncio.sleep(0)
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await asyncio.sleep(0)
assert light.is_on is True
await task
await asyncio.sleep(0)
caplog.clear()
caplog.set_level(logging.DEBUG)
# Handle the failure case
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.05):
await asyncio.create_task(light.async_turn_off())
assert light.is_on is True
assert "Failed to turn off (1/3)" in caplog.text
assert "Failed to turn off (2/3)" in caplog.text
assert "Failed to turn off (3/3)" in caplog.text
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.05):
task = asyncio.create_task(light.async_turn_off())
# Do NOT wait for the future to get added, we know the retry logic works
light._aio_protocol.data_received(
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
)
await asyncio.sleep(0)
assert light.is_on is False
await task
await asyncio.sleep(0)
caplog.clear()
caplog.set_level(logging.DEBUG)
# Handle the failure case
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.05):
await asyncio.create_task(light.async_turn_on())
assert light.is_on is False
assert "Failed to turn on (1/3)" in caplog.text
assert "Failed to turn on (2/3)" in caplog.text
assert "Failed to turn on (3/3)" in caplog.text
@pytest.mark.asyncio
async def test_turn_on_off_via_power_state_message(
mock_aio_protocol, caplog: pytest.LogCaptureFixture
):
"""Test we can turn on and off via power state message."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
task = asyncio.create_task(light.async_turn_off())
# Wait for the future to get added
await asyncio.sleep(0)
light._ignore_next_power_state_update = False
light._aio_protocol.data_received(b"\x0F\x71\x24\xA4")
await asyncio.sleep(0)
assert light.is_on is False
await task
task = asyncio.create_task(light.async_turn_on())
await asyncio.sleep(0)
light._ignore_next_power_state_update = False
light._aio_protocol.data_received(b"\x0F\x71\x23\xA3")
await asyncio.sleep(0)
assert light.is_on is True
await task
@pytest.mark.asyncio
async def test_turn_on_off_via_assessable_state_message(
mock_aio_protocol, caplog: pytest.LogCaptureFixture
):
"""Test we can turn on and off via addressable state message."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\xA3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
await task
task = asyncio.create_task(light.async_turn_off())
# Wait for the future to get added
await asyncio.sleep(0)
light._ignore_next_power_state_update = False
light._aio_protocol.data_received(
b"\xB0\xB1\xB2\xB3\x00\x01\x01\x23\x00\x0E\x81\xA3\x24\x25\xFF\x47\x64\xFF\xFF\x00\x01\x00\x1E\x34\x61"
)
await asyncio.sleep(0)
assert light.is_on is False
await task
task = asyncio.create_task(light.async_turn_on())
await asyncio.sleep(0)
light._ignore_next_power_state_update = False
light._aio_protocol.data_received(
b"\xB0\xB1\xB2\xB3\x00\x01\x01\x24\x00\x0E\x81\xA3\x23\x25\x5F\x21\x64\xFF\xFF\x00\x01\x00\x1E\x6D\xD4"
)
await asyncio.sleep(0)
assert light.is_on is True
await task
@pytest.mark.asyncio
async def test_shutdown(mock_aio_protocol):
"""Test we can shutdown."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
await light.async_stop()
await asyncio.sleep(0) # make sure nothing throws
@pytest.mark.asyncio
async def test_handling_connection_lost(mock_aio_protocol):
"""Test we can reconnect."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
light._aio_protocol.connection_lost(None)
await asyncio.sleep(0) # make sure nothing throws
# Test we reconnect and can turn off
task = asyncio.create_task(light.async_turn_off())
# Wait for the future to get added
await asyncio.sleep(0.1) # wait for reconnect
light._aio_protocol.data_received(
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
)
await asyncio.sleep(0)
assert light.is_on is False
await task
@pytest.mark.asyncio
async def test_handling_unavailable_after_no_response(mock_aio_protocol):
"""Test we handle the bulb not responding."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
await light.async_update()
await light.async_update()
await light.async_update()
await light.async_update()
with pytest.raises(RuntimeError):
await light.async_update()
assert light.available is False
@pytest.mark.asyncio
async def test_async_set_levels(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
"""Test we can set levels."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x33#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x65"
)
await task
assert light.model_num == 0x33
assert light.dimmable_effects is False
transport.reset_mock()
with pytest.raises(ValueError):
# ValueError: RGBW command sent to non-RGBW devic
await light.async_set_levels(255, 255, 255, 255, 255)
await light.async_set_levels(255, 0, 0)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\xff\x00\x00\x00\x00\x0f?"
@pytest.mark.asyncio
async def test_async_set_effect(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
"""Test we can set an effect."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\xA3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
await task
assert light.model_num == 0xA3
assert light.dimmable_effects is True
transport.reset_mock()
await light.async_set_effect("random", 50)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0].startswith(b"\xb0\xb1\xb2\xb3")
transport.reset_mock()
await light.async_set_effect("RBM 1", 50)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\x05B\x012d\x00\xa8"
)
assert light.effect == "RBM 1"
transport.reset_mock()
await light.async_set_brightness(255)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x05B\x01\x10d\x00\x87"
)
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00\x05B\x01\x102\x00V"
)
@pytest.mark.asyncio
async def test_async_set_custom_effect(
mock_aio_protocol, caplog: pytest.LogCaptureFixture
):
"""Test we can set a custom effect."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
assert light.model_num == 0x25
transport.reset_mock()
# no values
with pytest.raises(ValueError):
await light.async_set_custom_pattern([], 50, "jump")
await light.async_set_custom_pattern(
[
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 0),
(255, 0, 255),
(255, 0, 0),
(255, 0, 0),
],
50,
"jump",
)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"Q\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\xff\x00\xff\x00\x00\x00\x10;\xff\x0f\x99"
)
@pytest.mark.asyncio
async def test_async_set_brightness_rgbww(mock_aio_protocol):
"""Test we can set brightness rgbww."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
)
await task
await light.async_stop()
await asyncio.sleep(0) # make sure nothing throws
transport.reset_mock()
await light.async_set_brightness(255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd5\xff\xff\x00\x0f\x12"
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x80\x00k\x80\x80\x00\x0f+"
@pytest.mark.asyncio
async def test_async_set_brightness_cct(mock_aio_protocol):
"""Test we can set brightness with a cct device."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x02\x10\xb6\x00\x98\x19\x04\x25\x0f\xdb"
)
await task
await light.async_stop()
await asyncio.sleep(0) # make sure nothing throws
transport.reset_mock()
await light.async_set_brightness(255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00g\x98\x0f\x0fN"
assert light.brightness == 255
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x00\x00\x004L\x0f\x0f\xcf"
assert light.brightness == 128
@pytest.mark.asyncio
async def test_async_set_brightness_dim(mock_aio_protocol):
"""Test we can set brightness with a dim only device."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x01\x10\xb6\x00\x98\x19\x04\x25\x0f\xda"
)
await task
await light.async_stop()
await asyncio.sleep(0) # make sure nothing throws
transport.reset_mock()
await light.async_set_brightness(255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\xff\xff\x0f\x0fM"
assert light.brightness == 255
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\x80\x80\x0f\x0fO"
assert light.brightness == 128
@pytest.mark.asyncio
async def test_async_set_brightness_rgb(mock_aio_protocol):
"""Test we can set brightness with a rgb only device."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x03\x10\xb6\x00\x98\x19\x04\x25\x0f\xdc"
)
await task
await light.async_stop()
await asyncio.sleep(0) # make sure nothing throws
transport.reset_mock()
await light.async_set_brightness(255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd4\x00\x00\xf0\x0f\x03"
assert light.brightness == 255
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x80\x00j\x00\x00\xf0\x0f\x1a"
assert light.brightness == 128
@pytest.mark.asyncio
async def test_async_set_brightness_rgbw(mock_aio_protocol):
"""Test we can set brightness with a rgbw only device."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x25\x23\x61\x04\x10\xb6\x00\x98\x19\x04\x25\x0f\xdd"
)
await task
await light.async_stop()
await asyncio.sleep(0) # make sure nothing throws
transport.reset_mock()
await light.async_set_brightness(255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd5\xff\xff\x00\x0f\x12"
assert light.brightness == 255
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x80\x00k\x80\x80\x00\x0f+"
assert light.brightness == 128
@pytest.mark.asyncio
async def test_cct_protocol_device(mock_aio_protocol):
"""Test a cct protocol device."""
light = AIOWifiLedBulb("192.168.1.166")
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
transport, protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\x1C\x23\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0F\xC8"
)
await task
assert light.getCCT() == (0, 255)
assert light.color_temp == 6500
assert light.brightness == 255
light._aio_protocol.data_received(
b"\x81\x1C\x23\x61\x00\x05\x00\x00\x00\x00\x03\x64\x00\x8D"
)
assert light.getCCT() == (255, 0)
assert light.color_temp == 2700
assert light.brightness == 255
transport.reset_mock()
await light.async_set_brightness(32)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\t5\xb1\x00\r\x00\x00\x00\x03\xf6\xbe"
)
assert light.brightness == 33
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\t5\xb1\x002\x00\x00\x00\x03\x1b\t"
)
assert light.brightness == 128
transport.reset_mock()
await light.async_set_brightness(1)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\t5\xb1\x00\x02\x00\x00\x00\x03\xeb\xaa"
)
assert light.brightness == 0
transport.reset_mock()
await light.async_set_levels(w=0, w2=255)
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00\t5\xb1dd\x00\x00\x00\x03\xb17"
)
assert light.getCCT() == (0, 255)
assert light.color_temp == 6500
assert light.brightness == 255
@pytest.mark.asyncio
async def test_async_scanner(mock_discovery_aio_protocol):
"""Test scanner."""
scanner = AIOBulbScanner()
task = asyncio.ensure_future(
scanner.async_scan(timeout=0.1, address="192.168.213.252")
)
transport, protocol = await mock_discovery_aio_protocol()
protocol.datagram_received(b"HF-A11ASSISTHREAD", ("127.0.0.1", 48899))
protocol.datagram_received(
b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
)
protocol.datagram_received(b"AT+LVER\r", ("127.0.0.1", 48899))
protocol.datagram_received(
b"+ok=08_15_20210204_ZG-BL\r", ("192.168.213.252", 48899)
)
protocol.datagram_received(
b"192.168.213.65,F4CFA23E1AAF,AK001-ZJ2104", ("192.168.213.65", 48899)
)
protocol.datagram_received(b"+ok=A2_33_20200428_ZG-LX\r", ("192.168.213.65", 48899))
data = await task
assert data == [
{
"firmware_date": datetime.date(2021, 2, 4),
"id": "B4E842E10588",
"ipaddr": "192.168.213.252",
"model": "AK001-ZJ2145",
"model_description": "RGB Controller with MIC",
"model_info": "ZG-BL",
"model_num": 8,
"version_num": 21,
},
{
"firmware_date": datetime.date(2020, 4, 28),
"id": "F4CFA23E1AAF",
"ipaddr": "192.168.213.65",
"model": "AK001-ZJ2104",
"model_description": "RGB Symphony v2",
"model_info": "ZG-LX",
"model_num": 162,
"version_num": 51,
},
]
@pytest.mark.asyncio
async def test_async_scanner_specific_address(mock_discovery_aio_protocol):
"""Test scanner with a specific address."""
scanner = AIOBulbScanner()
task = asyncio.ensure_future(
scanner.async_scan(timeout=10, address="192.168.213.252")
)
transport, protocol = await mock_discovery_aio_protocol()
protocol.datagram_received(
b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
)
protocol.datagram_received(
b"+ok=08_15_20210204_ZG-BL\r", ("192.168.213.252", 48899)
)
data = await task
assert data == [
{
"firmware_date": datetime.date(2021, 2, 4),
"id": "B4E842E10588",
"ipaddr": "192.168.213.252",
"model": "AK001-ZJ2145",
"model_description": "RGB Controller with MIC",
"model_info": "ZG-BL",
"model_num": 8,
"version_num": 21,
}
]
assert scanner.getBulbInfoByID("B4E842E10588") == {
"firmware_date": datetime.date(2021, 2, 4),
"id": "B4E842E10588",
"ipaddr": "192.168.213.252",
"model": "AK001-ZJ2145",
"model_description": "RGB Controller with MIC",
"model_info": "ZG-BL",
"model_num": 8,
"version_num": 21,
}
assert scanner.getBulbInfo() == [
{
"firmware_date": datetime.date(2021, 2, 4),
"id": "B4E842E10588",
"ipaddr": "192.168.213.252",
"model": "AK001-ZJ2145",
"model_description": "RGB Controller with MIC",
"model_info": "ZG-BL",
"model_num": 8,
"version_num": 21,
}
]
@pytest.mark.asyncio
async def test_async_scanner_times_out_with_nothing(mock_discovery_aio_protocol):
"""Test scanner."""
scanner = AIOBulbScanner()
task = asyncio.ensure_future(scanner.async_scan(timeout=0.05))
transport, protocol = await mock_discovery_aio_protocol()
data = await task
assert data == []
@pytest.mark.asyncio
async def test_async_scanner_times_out_with_nothing_specific_address(
mock_discovery_aio_protocol,
):
"""Test scanner."""
scanner = AIOBulbScanner()
task = asyncio.ensure_future(
scanner.async_scan(timeout=0.05, address="192.168.213.252")
)
transport, protocol = await mock_discovery_aio_protocol()
data = await task
assert data == []