-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathtests_aio.py
3803 lines (3269 loc) · 129 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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
import contextlib
import datetime
import json
import logging
import time
import sys
from unittest.mock import MagicMock, call, patch
try:
from unittest.mock import AsyncMock
except ImportError:
from unittest.mock import MagicMock as AsyncMock
import pytest
from flux_led import aiodevice, aioscanner, DeviceUnavailableException
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_RGB,
COLOR_MODE_RGBW,
COLOR_MODE_RGBWW,
EFFECT_MUSIC,
MAX_TEMP,
MIN_TEMP,
PUSH_UPDATE_INTERVAL,
MultiColorEffects,
WhiteChannelType,
)
from flux_led.protocol import (
ProtocolLEDENETCCT,
ProtocolLEDENETCCTWrapped,
PROTOCOL_LEDENET_8BYTE_AUTO_ON,
PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS,
PROTOCOL_LEDENET_9BYTE,
PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS,
PROTOCOL_LEDENET_ORIGINAL,
PowerRestoreState,
PowerRestoreStates,
RemoteConfig,
)
from flux_led.scanner import (
FluxLEDDiscovery,
create_udp_socket,
is_legacy_device,
merge_discoveries,
)
from flux_led.timer import LedTimer
IP_ADDRESS = "127.0.0.1"
MODEL_NUM_HEX = "0x35"
MODEL = "AZ120444"
MODEL_DESCRIPTION = "Bulb RGBCW"
FLUX_MAC_ADDRESS = "aabbccddeeff"
FLUX_DISCOVERY_PARTIAL = FluxLEDDiscovery(
ipaddr=IP_ADDRESS,
model=MODEL,
id=FLUX_MAC_ADDRESS,
model_num=None,
version_num=None,
firmware_date=None,
model_info=None,
model_description=None,
)
FLUX_DISCOVERY = FluxLEDDiscovery(
ipaddr=IP_ADDRESS,
model=MODEL,
id=FLUX_MAC_ADDRESS,
model_num=0x25,
version_num=0x04,
firmware_date=datetime.date(2021, 5, 5),
model_info=MODEL,
model_description=MODEL_DESCRIPTION,
)
FLUX_DISCOVERY_24G_REMOTE = FluxLEDDiscovery(
ipaddr=IP_ADDRESS,
model="AK001-ZJ2148",
id=FLUX_MAC_ADDRESS,
model_num=0x25,
version_num=0x04,
firmware_date=datetime.date(2021, 5, 5),
model_info=MODEL,
model_description=MODEL_DESCRIPTION,
)
FLUX_DISCOVERY_LEGACY = FluxLEDDiscovery(
ipaddr=IP_ADDRESS,
model=MODEL,
id="ACCF23123456",
model_num=0x23,
version_num=0x04,
firmware_date=datetime.date(2021, 5, 5),
model_info=MODEL,
model_description=MODEL_DESCRIPTION,
)
FLUX_DISCOVERY_MISSING_HARDWARE = FluxLEDDiscovery(
ipaddr=IP_ADDRESS,
model=None,
id=FLUX_MAC_ADDRESS,
model_num=0x25,
version_num=0x04,
firmware_date=datetime.date(2021, 5, 5),
model_info=MODEL,
model_description=MODEL_DESCRIPTION,
)
class MinJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, set):
return list(o)
return json.JSONEncoder.default(self, o)
def mock_coro(return_value=None, exception=None):
"""Return a coro that returns a value or raise an exception."""
fut = asyncio.Future()
if exception is not None:
fut.set_exception(exception)
else:
fut.set_result(return_value)
return fut
@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),
patch.object(aioscanner, "MESSAGE_SEND_INTERLEAVE_DELAY", 0),
):
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)
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.01)
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.01)
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_cannot_determine_strip_type(mock_aio_protocol):
"""Test we raise RuntimeError when we cannot determine the strip type."""
light = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
# protocol state
light._aio_protocol.data_received(
b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
with pytest.raises(RuntimeError):
await task
assert not light.available
@pytest.mark.asyncio
async def test_setting_discovery(mock_aio_protocol):
"""Test we can pass discovery to AIOWifiLedBulb."""
light = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
def _updated_callback(*args, **kwargs):
pass
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
# protocol state
light._aio_protocol.data_received(
b"\x81\x35\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xee"
)
discovery = FluxLEDDiscovery(
{
"firmware_date": datetime.date(2021, 1, 9),
"id": "B4E842E10586",
"ipaddr": "192.168.213.259",
"model": "AK001-ZJ2145",
"model_description": "Bulb RGBCW",
"model_info": "ZG-BL-PWM",
"model_num": 53,
"remote_access_enabled": False,
"remote_access_host": None,
"remote_access_port": None,
"version_num": 98,
}
)
await task
assert light.available
assert light.model == "Bulb RGBCW (0x35)"
light.discovery = discovery
assert light.model == "Bulb RGBCW (0x35)"
assert light.discovery == discovery
@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))
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.color_modes == {COLOR_MODE_RGBWW, COLOR_MODE_CCT}
assert light.protocol == PROTOCOL_LEDENET_9BYTE
assert light.model_num == 0x25
assert light.model == "Controller RGB/WW/CW (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
transport.reset_mock()
await light.async_set_device_config()
assert len(transport.mock_calls) == 1
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"b\x05\x0fv"
transport.reset_mock()
await light.async_set_device_config(operating_mode="CCT")
assert len(transport.mock_calls) == 1
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"b\x02\x0fs"
@pytest.mark.asyncio
async def test_extract_from_outer_message(mock_aio_protocol):
"""Test we can can extract a message wrapped with an outer 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"\xb0\xb1\xb2\xb3\x00\x01\x01\x81\x00\x0e\x81\x1a\x23\x61\x07\x00\xff\x00\x00\x00\x01\x00\x06\x2c\xaf"
b"\xb0\xb1\xb2\xb3\x00\x01\x01\x81\x00\x0e\x81\x1a\x23\x61\x07\x00\xff\x00\x00\x00\x01\x00\x06\x2c\xaf"
)
await task
assert light.color_modes == {COLOR_MODE_RGB}
assert light.protocol == PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
assert light.model_num == 0x1A
assert light.model == "String Lights (0x1A)"
assert light.is_on is True
assert len(light.effect_list) == 101
assert light.rgb == (255, 0, 0)
@pytest.mark.asyncio
async def test_extract_from_outer_message_and_reassemble(mock_aio_protocol):
"""Test we can can extract a message wrapped with an outer 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()
for byte in b"\xb0\xb1\xb2\xb3\x00\x01\x01\x81\x00\x0e\x81\x1a\x23\x61\x07\x00\xff\x00\x00\x00\x01\x00\x06\x2c\xaf":
light._aio_protocol.data_received(bytearray([byte]))
await task
assert light.color_modes == {COLOR_MODE_RGB}
assert light.protocol == PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
assert light.model_num == 0x1A
assert light.model == "String Lights (0x1A)"
assert light.is_on is True
assert len(light.effect_list) == 101
assert light.rgb == (255, 0, 0)
@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
data = []
def _send_data(*args, **kwargs):
light._aio_protocol.data_received(data.pop(0))
with (
patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010),
patch.object(light._aio_protocol, "write", _send_data),
):
data = [
b"\xf0\x71\x24\x85",
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
]
await light.async_turn_off()
await asyncio.sleep(0)
assert light.is_on is False
assert len(data) == 1
data = [
b"\xf0\x71\x24\x85",
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
]
await light.async_turn_on()
await asyncio.sleep(0)
assert light.is_on is True
assert len(data) == 0
data = [b"\xf0\x71\x24\x85"]
await light.async_turn_off()
await asyncio.sleep(0)
assert light.is_on is False
assert len(data) == 0
data = [
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
]
await light.async_turn_on()
await asyncio.sleep(0)
assert light.is_on is True
assert len(data) == 0
data = [
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 light.async_turn_off()
await asyncio.sleep(0)
assert light.is_on is False
assert len(data) == 0
data = [
*(
b"\xf0\x71\x24\x85",
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
)
* 5
]
await light.async_turn_on()
await asyncio.sleep(0)
assert light.is_on is True
assert len(data) == 3
light._aio_protocol.data_received(
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
)
assert (
light.is_on is True
) # transition time should now be in effect since we forced state
data = [*(b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",) * 14]
await light.async_turn_off()
await asyncio.sleep(0)
# If all we get is on 0x81 responses, the bulb failed to turn off
assert light.is_on is True
assert len(data) == 2
await asyncio.sleep(0)
caplog.clear()
caplog.set_level(logging.DEBUG)
# Handle the failure case
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010):
await asyncio.create_task(light.async_turn_off())
assert light.is_on is True
assert "Failed to set power state to False (1/6)" in caplog.text
assert "Failed to set power state to False (2/6)" in caplog.text
assert "Failed to set power state to False (3/6)" in caplog.text
assert "Failed to set power state to False (4/6)" in caplog.text
assert "Failed to set power state to False (5/6)" in caplog.text
assert "Failed to set power state to False (6/6)" in caplog.text
with (
patch.object(light._aio_protocol, "write", _send_data),
patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010),
):
data = [
*(
b"\x0f\x71\x24\xa4",
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
)
* 5
]
await light.async_turn_off()
assert light.is_on is False
assert len(data) == 9
data = [
*(
b"\xf0\x71\x23\xa3",
b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
)
* 5
]
await light.async_turn_on()
assert light.is_on is True
assert len(data) == 9
data = [
*(
b"\x0f\x71\x24\xa4",
b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
)
* 5
]
await light.async_turn_off()
assert light.is_on is False
assert len(data) == 9
await asyncio.sleep(0)
caplog.clear()
caplog.set_level(logging.DEBUG)
# Handle the failure case
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010):
await asyncio.create_task(light.async_turn_on())
assert light.is_on is False
assert "Failed to set power state to True (1/6)" in caplog.text
assert "Failed to set power state to True (2/6)" in caplog.text
assert "Failed to set power state to True (3/6)" in caplog.text
assert "Failed to set power state to True (4/6)" in caplog.text
assert "Failed to set power state to True (5/6)" in caplog.text
assert "Failed to set power state to True (6/6)" 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
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010):
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
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.025):
task = asyncio.create_task(light.async_setup(_updated_callback))
await mock_aio_protocol()
# protocol state
light._aio_protocol.data_received(
b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
# ic sorting
light._aio_protocol.data_received(
b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0"
)
await task
data = None
def _send_data(*args, **kwargs):
light._aio_protocol.data_received(data)
with patch.object(light._aio_protocol, "write", _send_data):
data = 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 light.async_turn_off()
assert light.is_on is False
data = 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 light.async_turn_on()
assert light.is_on is True
@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
with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.025):
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_handling_unavailable_after_no_response_force(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))
transport, original_aio_protocol = await mock_aio_protocol()
light._aio_protocol.data_received(
b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
# ic state
light._aio_protocol.data_received(
b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x0b\x00\x63\x00\x90\x00\x01\x07\x08\x90\x01\x94\xfb"
)
await task
assert light._protocol.power_push_updates is True
transport.reset_mock()
await light.async_update()
assert len(transport.mock_calls) == 1
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x04\x81\x8a\x8b\x96\xf9"
)
light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
await light.async_update()
light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
await light.async_update()
light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
await light.async_update()
light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
with pytest.raises(RuntimeError):
await light.async_update()
assert light.available is False
# simulate reconnect
await light.async_update()
assert light._aio_protocol != original_aio_protocol
light._aio_protocol = original_aio_protocol
transport.reset_mock()
light._aio_protocol.data_received(
b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
await light.async_update(force=True)
assert len(transport.mock_calls) == 1
assert transport.mock_calls[0][0] == "write"
assert (
transport.mock_calls[0][1][0]
== b"\xb0\xb1\xb2\xb3\x00\x01\x01\x06\x00\x04\x81\x8a\x8b\x96\xfe"
)
assert light.available is True
light._aio_protocol.data_received(
b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
)
assert light.available is True
@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\x24\x61\x23\x01\x00\xff\x00\x00\x04\x00\x0f\x6f"
)
await task
assert light.model_num == 0x33
assert light.version_num == 4
assert light.wiring == "GRB"
assert light.wiring_num == 2
assert light.wirings == ["RGB", "GRB", "BRG"]
assert light.operating_mode is None
assert light.dimmable_effects is False
assert light.requires_turn_on is True
assert light._protocol.power_push_updates is False
assert light._protocol.state_push_updates is False
transport.reset_mock()
await light.async_set_device_config()
assert len(transport.mock_calls) == 1
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"b\x00\x02\x0fs"
transport.reset_mock()
await light.async_set_device_config(wiring="BRG")
assert len(transport.mock_calls) == 1
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"b\x00\x03\x0ft"
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?"
# light is on
light._aio_protocol.data_received(
b"\x81\x33\x23\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x65"
)
transport.reset_mock()
await light.async_update()
await light.async_update()
await light.async_update()
await light.async_update()
await asyncio.sleep(0)
assert len(transport.mock_calls) == 4
# light is off
light._aio_protocol.data_received(
b"\x81\x33\x24\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x66"
)
transport.reset_mock()
await light.async_update()
await light.async_update()
await light.async_update()
await light.async_update()
await asyncio.sleep(0)
assert len(transport.mock_calls) == 4
with pytest.raises(ValueError):
await light.async_set_preset_pattern(101, 50, 100)
@pytest.mark.asyncio
async def test_async_set_levels_0x52(
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\x52\x23\x61\x00\x00\xff\x00\x00\x00\x01\x00\x00\x57"
)
await task
assert light.model_num == 0x52
assert light.version_num == 1
assert light.wiring is None
assert light.wiring_num is None
assert light.wirings is None
assert light.operating_mode is None
assert light.dimmable_effects is False
assert light.requires_turn_on is True
assert light._protocol.power_push_updates is False
assert light._protocol.state_push_updates 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)
transport.reset_mock()
await light.async_set_levels(0, 0, 0, 255, 255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\xff\xff\x00\x00\x00\x0f>"
transport.reset_mock()
await light.async_set_levels(0, 0, 0, 128, 255)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x80\xff\x00\x00\x00\x0f\xbf"
transport.reset_mock()
await light.async_set_levels(0, 0, 0, 0, 128)
assert transport.mock_calls[0][0] == "write"
assert transport.mock_calls[0][1][0] == b"1\x00\x80\x00\x00\x00\x0f\xc0"
@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"
)
# ic state
light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
await task
assert light.model_num == 0xA3
assert light.dimmable_effects is True
assert light.requires_turn_on is False
assert light._protocol.power_push_updates is True
assert light._protocol.state_push_updates 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\xd9\x81"
)
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\xb7>"
)
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\x85\xdb"
)
for i in range(5, 255):
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
counter_byte = transport.mock_calls[0][1][0][7]
assert counter_byte == i
transport.reset_mock()
await light.async_set_brightness(128)
assert transport.mock_calls[0][0] == "write"
counter_byte = transport.mock_calls[0][1][0][7]
assert counter_byte == 0
@pytest.mark.asyncio
async def test_SK6812RGBW(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
"""Test we can set set zone colors."""
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"
)
# ic state
light._aio_protocol.data_received(
b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x0b\x00\x63\x00\x90\x00\x01\x07\x08\x90\x01\x94\xfb"
)
await task
assert light.pixels_per_segment == 144
assert light.segments == 1
assert light.music_pixels_per_segment == 144
assert light.music_segments == 1
assert light.ic_types == [
"WS2812B",
"SM16703",
"SM16704",
"WS2811",
"UCS1903",
"SK6812",
"SK6812RGBW",
"INK1003",
"UCS2904B",
]
assert light.ic_type == "SK6812RGBW"
assert light.ic_type_num == 7
assert light.operating_mode is None
assert light.operating_modes is None
assert light.wiring == "WGRB"
assert light.wiring_num == 8
assert light.wirings == [
"RGBW",
"RBGW",
"GRBW",
"GBRW",
"BRGW",
"BGRW",
"WRGB",
"WRBG",
"WGRB",
"WGBR",
"WBRG",
"WBGR",
]
assert light.model_num == 0xA3
assert light.dimmable_effects is True
assert light.requires_turn_on is False
assert light.color_mode == COLOR_MODE_RGBW
assert light.color_modes == {COLOR_MODE_RGBW, COLOR_MODE_CCT}
diag = light.diagnostics
assert isinstance(json.dumps(diag, cls=MinJSONEncoder), str)
assert diag["device_state"]["wiring_num"] == 8
assert (