/
test_light_json.py
2648 lines (2360 loc) · 82.8 KB
/
test_light_json.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
"""The tests for the MQTT JSON light platform.
Configuration with RGB, brightness, color temp, effect, and XY:
mqtt:
light:
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
color_temp: true
effect: true
rgb: true
xy: true
Configuration with RGB, brightness, color temp and effect:
mqtt:
light:
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
color_temp: true
effect: true
rgb: true
Configuration with RGB, brightness and color temp:
mqtt:
light:
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
rgb: true
color_temp: true
Configuration with RGB, brightness:
mqtt:
light:
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
rgb: true
Config without RGB:
mqtt:
light:
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
brightness: true
Config without RGB and brightness:
light:
platform: mqtt_json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
Config with brightness and scale:
light:
platform: mqtt_json
name: test
state_topic: "mqtt_json_light_1"
command_topic: "mqtt_json_light_1/set"
brightness: true
brightness_scale: 99
"""
import copy
from typing import Any
from unittest.mock import call, patch
import pytest
from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.light.schema_basic import (
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
from homeassistant.components.mqtt.models import PublishPayloadType
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_SUPPORTED_FEATURES,
STATE_OFF,
STATE_ON,
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.util.json import JsonValueType, json_loads
from .test_common import (
help_custom_config,
help_test_availability_when_connection_lost,
help_test_availability_without_topic,
help_test_custom_availability_payload,
help_test_default_availability_payload,
help_test_discovery_broken,
help_test_discovery_removal,
help_test_discovery_update,
help_test_discovery_update_attr,
help_test_discovery_update_unchanged,
help_test_encoding_subscribable_topics,
help_test_entity_debug_info_message,
help_test_entity_device_info_remove,
help_test_entity_device_info_update,
help_test_entity_device_info_with_connection,
help_test_entity_device_info_with_identifier,
help_test_entity_id_update_discovery_update,
help_test_entity_id_update_subscriptions,
help_test_publishing_with_custom_encoding,
help_test_reloadable,
help_test_setting_attribute_via_mqtt_json_message,
help_test_setting_attribute_with_template,
help_test_setting_blocked_attribute_via_mqtt_json_message,
help_test_skipped_async_ha_write_state,
help_test_unique_id,
help_test_update_with_json_attrs_bad_json,
help_test_update_with_json_attrs_not_dict,
)
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"command_topic": "test-topic",
}
}
}
COLOR_MODES_CONFIG = {
mqtt.DOMAIN: {
light.DOMAIN: {
"brightness": True,
"color_mode": True,
"effect": True,
"command_topic": "test_light_rgb/set",
"name": "test",
"schema": "json",
"supported_color_modes": [
"color_temp",
"hs",
"rgb",
"rgbw",
"rgbww",
"white",
"xy",
],
"qos": 0,
}
}
}
@pytest.fixture(autouse=True)
def light_platform_only():
"""Only setup the light platform to speed up tests."""
with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]):
yield
class JsonValidator:
"""Helper to compare JSON."""
def __init__(self, jsondata: JsonValueType) -> None:
"""Initialize JSON validator."""
self.jsondata = jsondata
def __eq__(self, other: JsonValueType) -> bool:
"""Compare JSON data."""
return json_loads(self.jsondata) == json_loads(other)
@pytest.mark.parametrize(
"hass_config", [{mqtt.DOMAIN: {light.DOMAIN: {"schema": "json", "name": "test"}}}]
)
async def test_fail_setup_if_no_command_topic(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if setup fails with no command topic."""
assert await mqtt_mock_entry()
assert "required key not provided" in caplog.text
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"color_temp": True},)),
help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"hs": True},)),
help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"rgb": True},)),
help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"xy": True},)),
],
)
async def test_fail_setup_if_color_mode_deprecated(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if setup fails if color mode is combined with deprecated config keys."""
assert await mqtt_mock_entry()
assert "color_mode must not be combined with any of" in caplog.text
@pytest.mark.parametrize(
("hass_config", "error"),
[
(
help_custom_config(
light.DOMAIN,
COLOR_MODES_CONFIG,
({"supported_color_modes": ["onoff", "rgb"]},),
),
"Invalid supported_color_modes ['onoff', 'rgb']",
),
(
help_custom_config(
light.DOMAIN,
COLOR_MODES_CONFIG,
({"supported_color_modes": ["brightness", "rgb"]},),
),
"Invalid supported_color_modes ['brightness', 'rgb']",
),
(
help_custom_config(
light.DOMAIN,
COLOR_MODES_CONFIG,
({"supported_color_modes": ["unknown"]},),
),
"value must be one of [<ColorMode.",
),
],
)
async def test_fail_setup_if_color_modes_invalid(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
error: str,
) -> None:
"""Test if setup fails if supported color modes is invalid."""
assert await mqtt_mock_entry()
assert error in caplog.text
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"command_topic": "test_light/set",
"state_topic": "test_light",
"color_mode": True,
"supported_color_modes": "color_temp",
}
}
}
],
)
async def test_single_color_mode(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
) -> None:
"""Test setup with single color_mode."""
await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192)
async_fire_mqtt_message(
hass,
"test_light",
'{"state": "ON", "brightness": 50, "color_mode": "color_temp", "color_temp": 192}',
)
color_modes = [light.ColorMode.COLOR_TEMP]
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes
assert state.attributes.get(light.ATTR_COLOR_TEMP) == 192
assert state.attributes.get(light.ATTR_BRIGHTNESS) == 50
assert state.attributes.get(light.ATTR_COLOR_MODE) == color_modes[0]
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"rgb": True,
}
}
}
],
)
async def test_legacy_rgb_light(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test legacy RGB light flags expected features and color modes."""
await mqtt_mock_entry()
state = hass.states.get("light.test")
color_modes = [light.ColorMode.HS]
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"state_topic": "test_light_rgb",
"command_topic": "test_light_rgb/set",
}
}
}
],
)
async def test_no_color_brightness_color_temp_if_no_topics(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test for no RGB, brightness, color temp, effector XY."""
await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("xy_color") is None
assert state.attributes.get("hs_color") is None
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON"}')
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("xy_color") is None
assert state.attributes.get("hs_color") is None
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"OFF"}')
state = hass.states.get("light.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "test_light_rgb", '{"state": null}')
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"state_topic": "test_light_rgb",
"command_topic": "test_light_rgb/set",
"brightness": True,
"color_temp": True,
"effect": True,
"rgb": True,
"xy": True,
"hs": True,
"qos": "0",
}
}
}
],
)
async def test_controlling_state_via_topic(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test the controlling of the state via topic."""
await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
color_modes = [light.ColorMode.COLOR_TEMP, light.ColorMode.HS]
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes
expected_features = (
light.SUPPORT_EFFECT | light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
)
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("xy_color") is None
assert state.attributes.get("hs_color") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)
# Turn on the light
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON",'
'"color":{"r":255,"g":255,"b":255},'
'"brightness":255,'
'"color_temp":155,'
'"effect":"colorloop"}',
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("rgb_color") == (255, 255, 255)
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_temp") is None # rgb color has priority
assert state.attributes.get("effect") == "colorloop"
assert state.attributes.get("xy_color") == (0.323, 0.329)
assert state.attributes.get("hs_color") == (0.0, 0.0)
# Turn on the light
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON",'
'"brightness":255,'
'"color":null,'
'"color_temp":155,'
'"effect":"colorloop"}',
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("rgb_color") == (
255,
253,
248,
) # temp converted to color
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_temp") == 155
assert state.attributes.get("effect") == "colorloop"
assert state.attributes.get("xy_color") == (0.328, 0.334) # temp converted to color
assert state.attributes.get("hs_color") == (44.098, 2.43) # temp converted to color
# Turn the light off
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"OFF"}')
state = hass.states.get("light.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON", "brightness":100}')
light_state = hass.states.get("light.test")
assert light_state.attributes["brightness"] == 100
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "color":{"r":125,"g":125,"b":125}}'
)
light_state = hass.states.get("light.test")
assert light_state.attributes.get("rgb_color") == (255, 255, 255)
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "color":{"x":0.135,"y":0.135}}'
)
light_state = hass.states.get("light.test")
assert light_state.attributes.get("xy_color") == (0.141, 0.14)
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "color":{"h":180,"s":50}}'
)
light_state = hass.states.get("light.test")
assert light_state.attributes.get("hs_color") == (180.0, 50.0)
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON", "color":null}')
light_state = hass.states.get("light.test")
assert "hs_color" in light_state.attributes # Color temp approximation
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON", "color_temp":155}')
light_state = hass.states.get("light.test")
assert light_state.attributes.get("color_temp") == 155
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON", "color_temp":null}')
light_state = hass.states.get("light.test")
assert light_state.attributes.get("color_temp") is None
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "effect":"colorloop"}'
)
light_state = hass.states.get("light.test")
assert light_state.attributes.get("effect") == "colorloop"
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON",'
'"color":{"r":255,"g":255,"b":255},'
'"brightness":128,'
'"color_temp":155,'
'"effect":"colorloop"}',
)
light_state = hass.states.get("light.test")
assert light_state.state == STATE_ON
assert light_state.attributes.get("brightness") == 128
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"OFF","brightness":0}',
)
light_state = hass.states.get("light.test")
assert light_state.state == STATE_OFF
assert light_state.attributes.get("brightness") is None
# test previous zero brightness received was ignored and brightness is restored
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON"}')
light_state = hass.states.get("light.test")
assert light_state.attributes.get("brightness") == 128
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON","brightness":0}')
light_state = hass.states.get("light.test")
assert light_state.attributes.get("brightness") == 128
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
light.DOMAIN, COLOR_MODES_CONFIG, ({"state_topic": "test_light_rgb"},)
)
],
)
async def test_controlling_state_via_topic2(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling of the state via topic for a light supporting color mode."""
supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "white", "xy"]
await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
expected_features = (
light.SUPPORT_EFFECT | light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
)
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_mode") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") is None
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("supported_color_modes") == supported_color_modes
assert state.attributes.get("xy_color") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)
# Turn on the light, rgbww mode, additional values in the update
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON",'
'"color_mode":"rgbww",'
'"color":{"r":255,"g":128,"b":64, "c": 32, "w": 16, "x": 1, "y": 1},'
'"brightness":255,'
'"color_temp":155,'
'"effect":"colorloop"}',
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_mode") == "rgbww"
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") == "colorloop"
assert state.attributes.get("hs_color") == (20.552, 70.98)
assert state.attributes.get("rgb_color") == (255, 136, 74)
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") == (255, 128, 64, 32, 16)
assert state.attributes.get("xy_color") == (0.571, 0.361)
# Light turned off
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"OFF"}')
state = hass.states.get("light.test")
assert state.state == STATE_OFF
# Light turned on, brightness 100
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON", "brightness":100}')
state = hass.states.get("light.test")
assert state.attributes["brightness"] == 100
# RGB color
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"rgb", "color":{"r":64,"g":128,"b":255}}',
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == "rgb"
assert state.attributes.get("rgb_color") == (64, 128, 255)
# RGBW color
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"rgbw", "color":{"r":64,"g":128,"b":255,"w":32}}',
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == "rgbw"
assert state.attributes.get("rgbw_color") == (64, 128, 255, 32)
# XY color
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"xy", "color":{"x":0.135,"y":0.235}}',
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == "xy"
assert state.attributes.get("xy_color") == (0.135, 0.235)
# HS color
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"hs", "color":{"h":180,"s":50}}',
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == "hs"
assert state.attributes.get("hs_color") == (180.0, 50.0)
# Color temp
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"color_temp", "color_temp":155}',
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == "color_temp"
assert state.attributes.get("color_temp") == 155
# White
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"white", "brightness":123}',
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == "white"
assert state.attributes.get("brightness") == 123
# Effect
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "effect":"other_effect"}'
)
state = hass.states.get("light.test")
assert state.attributes.get("effect") == "other_effect"
# Invalid color mode
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "color_mode":"col_temp"}'
)
assert "Invalid color mode 'col_temp' received" in caplog.text
caplog.clear()
# Incomplete color
async_fire_mqtt_message(
hass, "test_light_rgb", '{"state":"ON", "color_mode":"rgb"}'
)
assert (
"Invalid or incomplete color value '{'state': 'ON', 'color_mode': 'rgb'}' received"
in caplog.text
)
caplog.clear()
# Invalid color
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON", "color_mode":"rgb", "color":{"r":64,"g":128,"b":"cow"}}',
)
assert (
"Invalid or incomplete color value '{'state': 'ON', 'color_mode': 'rgb', 'color': {'r': 64, 'g': 128, 'b': 'cow'}}' received"
in caplog.text
)
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"state_topic": "test_light_rgb/set",
"rgb": True,
"color_temp": True,
"brightness": True,
}
}
}
],
)
async def test_controlling_the_state_with_legacy_color_handling(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test state updates for lights with a legacy color handling."""
supported_color_modes = ["color_temp", "hs"]
await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_mode") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") is None
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("supported_color_modes") == supported_color_modes
assert state.attributes.get("xy_color") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)
for _ in range(0, 2):
# Returned state after the light was turned on
# Receiving legacy color mode: rgb.
async_fire_mqtt_message(
hass,
"test_light_rgb/set",
'{ "state": "ON", "brightness": 255, "level": 100, "hue": 16,'
'"saturation": 100, "color": { "r": 255, "g": 67, "b": 0 }, '
'"bulb_mode": "color", "color_mode": "rgb" }',
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_mode") == "hs"
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") == (15.765, 100.0)
assert state.attributes.get("rgb_color") == (255, 67, 0)
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("xy_color") == (0.674, 0.322)
# Returned state after the lights color mode was changed
# Receiving legacy color mode: color_temp
async_fire_mqtt_message(
hass,
"test_light_rgb/set",
'{ "state": "ON", "brightness": 255, "level": 100, '
'"kelvin": 92, "color_temp": 353, "bulb_mode": "white", '
'"color_mode": "color_temp" }',
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_mode") == "color_temp"
assert state.attributes.get("color_temp") == 353
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") == (28.125, 61.661)
assert state.attributes.get("rgb_color") == (255, 171, 97)
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("xy_color") == (0.513, 0.386)
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"brightness": True,
"color_temp": True,
"effect": True,
"hs": True,
"rgb": True,
"xy": True,
"qos": 2,
}
}
}
],
)
async def test_sending_mqtt_commands_and_optimistic(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test the sending of command in optimistic mode."""
fake_state = State(
"light.test",
"on",
{
"brightness": 95,
"hs_color": [100, 100],
"effect": "random",
"color_temp": 100,
},
)
mock_restore_cache(hass, (fake_state,))
mqtt_mock = await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 95
assert state.attributes.get("hs_color") == (100, 100)
assert state.attributes.get("effect") == "random"
assert state.attributes.get("color_temp") is None # hs_color has priority
color_modes = [light.ColorMode.COLOR_TEMP, light.ColorMode.HS]
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes
expected_features = (
light.SUPPORT_EFFECT | light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
)
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_turn_on(hass, "light.test")
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set", '{"state":"ON"}', 2, False
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("light.test")
assert state.state == STATE_ON
await common.async_turn_on(hass, "light.test", color_temp=90)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator('{"state": "ON", "color_temp": 90}'),
2,
False,
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("color_mode") == light.ColorMode.COLOR_TEMP
assert state.attributes.get("color_temp") == 90
await common.async_turn_off(hass, "light.test")
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set", '{"state":"OFF"}', 2, False
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
mqtt_mock.reset_mock()
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 0, "g": 123, "b": 255,'
' "x": 0.14, "y": 0.131, "h": 210.824, "s": 100.0},'
' "brightness": 50}'
),
2,
False,
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == light.ColorMode.HS
assert state.attributes["brightness"] == 50
assert state.attributes["hs_color"] == (210.824, 100.0)
assert state.attributes["rgb_color"] == (0, 123, 255)
assert state.attributes["xy_color"] == (0.14, 0.131)
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59,'
' "x": 0.654, "y": 0.301, "h": 359.0, "s": 78.0},'
' "brightness": 50}'
),
2,
False,
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("color_mode") == light.ColorMode.HS
assert state.attributes["brightness"] == 50
assert state.attributes["hs_color"] == (359.0, 78.0)
assert state.attributes["rgb_color"] == (255, 56, 59)
assert state.attributes["xy_color"] == (0.654, 0.301)
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0,'
' "x": 0.611, "y": 0.375, "h": 30.118, "s": 100.0}}'
),
2,
False,
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("color_mode") == light.ColorMode.HS
assert state.attributes["brightness"] == 50
assert state.attributes["hs_color"] == (30.118, 100)
assert state.attributes["rgb_color"] == (255, 128, 0)
assert state.attributes["xy_color"] == (0.611, 0.375)
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"brightness": True,
"color_mode": True,
"command_topic": "test_light_rgb/set",
"effect": True,
"name": "test",
"qos": 2,
"schema": "json",
"supported_color_modes": [
"color_temp",
"hs",
"rgb",
"rgbw",
"rgbww",
"white",
"xy",
],
}
}
}
],
)
async def test_sending_mqtt_commands_and_optimistic2(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test the sending of command in optimistic mode for a light supporting color mode."""
supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "white", "xy"]
fake_state = State(
"light.test",
"on",
{
"brightness": 95,
"color_temp": 100,
"color_mode": "rgb",