forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_mqtt.py
823 lines (687 loc) · 33.4 KB
/
test_mqtt.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
"""The tests for the mqtt climate component."""
import copy
import json
import unittest
from unittest.mock import ANY
import pytest
import voluptuous as vol
from homeassistant.util.unit_system import (
METRIC_SYSTEM
)
from homeassistant.setup import setup_component
from homeassistant.components import climate, mqtt
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE
from homeassistant.components.climate import (
SUPPORT_OPERATION_MODE, SUPPORT_TARGET_TEMPERATURE,
SUPPORT_FAN_MODE, SUPPORT_SWING_MODE, SUPPORT_HOLD_MODE,
SUPPORT_AWAY_MODE, SUPPORT_AUX_HEAT, DEFAULT_MIN_TEMP, DEFAULT_MAX_TEMP)
from homeassistant.components.mqtt.discovery import async_start
from tests.common import (
async_fire_mqtt_message, async_mock_mqtt_component, async_setup_component,
fire_mqtt_message, get_test_home_assistant, mock_component,
mock_mqtt_component, MockConfigEntry, mock_registry)
from tests.components.climate import common
ENTITY_CLIMATE = 'climate.test'
DEFAULT_CONFIG = {
'climate': {
'platform': 'mqtt',
'name': 'test',
'mode_command_topic': 'mode-topic',
'temperature_command_topic': 'temperature-topic',
'fan_mode_command_topic': 'fan-mode-topic',
'swing_mode_command_topic': 'swing-mode-topic',
'away_mode_command_topic': 'away-mode-topic',
'hold_command_topic': 'hold-topic',
'aux_command_topic': 'aux-topic'
}}
class TestMQTTClimate(unittest.TestCase):
"""Test the mqtt climate hvac."""
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)
self.hass.config.units = METRIC_SYSTEM
def tearDown(self): # pylint: disable=invalid-name
"""Stop down everything that was started."""
self.hass.stop()
def test_setup_params(self):
"""Test the initial parameters."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 21 == state.attributes.get('temperature')
assert "low" == state.attributes.get('fan_mode')
assert "off" == state.attributes.get('swing_mode')
assert "off" == state.attributes.get('operation_mode')
assert DEFAULT_MIN_TEMP == state.attributes.get('min_temp')
assert DEFAULT_MAX_TEMP == state.attributes.get('max_temp')
def test_supported_features(self):
"""Test the supported_features."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
support = (SUPPORT_TARGET_TEMPERATURE | SUPPORT_OPERATION_MODE |
SUPPORT_SWING_MODE | SUPPORT_FAN_MODE | SUPPORT_AWAY_MODE |
SUPPORT_HOLD_MODE | SUPPORT_AUX_HEAT)
assert state.attributes.get("supported_features") == support
def test_get_operation_modes(self):
"""Test that the operation list returns the correct modes."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
modes = state.attributes.get('operation_list')
assert [
climate.STATE_AUTO, STATE_OFF, climate.STATE_COOL,
climate.STATE_HEAT, climate.STATE_DRY, climate.STATE_FAN_ONLY
] == modes
def test_set_operation_bad_attr_and_state(self):
"""Test setting operation mode without required attribute.
Also check the state.
"""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('operation_mode')
assert "off" == state.state
with pytest.raises(vol.Invalid):
common.set_operation_mode(self.hass, None, ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('operation_mode')
assert "off" == state.state
def test_set_operation(self):
"""Test setting of new operation mode."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('operation_mode')
assert "off" == state.state
common.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "cool" == state.attributes.get('operation_mode')
assert "cool" == state.state
self.mock_publish.async_publish.assert_called_once_with(
'mode-topic', 'cool', 0, False)
def test_set_operation_pessimistic(self):
"""Test setting operation mode in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['mode_state_topic'] = 'mode-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('operation_mode') is None
assert "unknown" == state.state
common.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('operation_mode') is None
assert "unknown" == state.state
fire_mqtt_message(self.hass, 'mode-state', 'cool')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "cool" == state.attributes.get('operation_mode')
assert "cool" == state.state
fire_mqtt_message(self.hass, 'mode-state', 'bogus mode')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "cool" == state.attributes.get('operation_mode')
assert "cool" == state.state
def test_set_operation_with_power_command(self):
"""Test setting of new operation mode with power command enabled."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['power_command_topic'] = 'power-command'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('operation_mode')
assert "off" == state.state
common.set_operation_mode(self.hass, "on", ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "on" == state.attributes.get('operation_mode')
assert "on" == state.state
self.mock_publish.async_publish.assert_has_calls([
unittest.mock.call('power-command', 'ON', 0, False),
unittest.mock.call('mode-topic', 'on', 0, False)
])
self.mock_publish.async_publish.reset_mock()
common.set_operation_mode(self.hass, "off", ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('operation_mode')
assert "off" == state.state
self.mock_publish.async_publish.assert_has_calls([
unittest.mock.call('power-command', 'OFF', 0, False),
unittest.mock.call('mode-topic', 'off', 0, False)
])
self.mock_publish.async_publish.reset_mock()
def test_set_fan_mode_bad_attr(self):
"""Test setting fan mode without required attribute."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "low" == state.attributes.get('fan_mode')
with pytest.raises(vol.Invalid):
common.set_fan_mode(self.hass, None, ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "low" == state.attributes.get('fan_mode')
def test_set_fan_mode_pessimistic(self):
"""Test setting of new fan mode in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['fan_mode_state_topic'] = 'fan-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('fan_mode') is None
common.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('fan_mode') is None
fire_mqtt_message(self.hass, 'fan-state', 'high')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'high' == state.attributes.get('fan_mode')
fire_mqtt_message(self.hass, 'fan-state', 'bogus mode')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'high' == state.attributes.get('fan_mode')
def test_set_fan_mode(self):
"""Test setting of new fan mode."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "low" == state.attributes.get('fan_mode')
common.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'fan-mode-topic', 'high', 0, False)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'high' == state.attributes.get('fan_mode')
def test_set_swing_mode_bad_attr(self):
"""Test setting swing mode without required attribute."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('swing_mode')
with pytest.raises(vol.Invalid):
common.set_swing_mode(self.hass, None, ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('swing_mode')
def test_set_swing_pessimistic(self):
"""Test setting swing mode in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['swing_mode_state_topic'] = 'swing-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('swing_mode') is None
common.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('swing_mode') is None
fire_mqtt_message(self.hass, 'swing-state', 'on')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "on" == state.attributes.get('swing_mode')
fire_mqtt_message(self.hass, 'swing-state', 'bogus state')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "on" == state.attributes.get('swing_mode')
def test_set_swing(self):
"""Test setting of new swing mode."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "off" == state.attributes.get('swing_mode')
common.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'swing-mode-topic', 'on', 0, False)
state = self.hass.states.get(ENTITY_CLIMATE)
assert "on" == state.attributes.get('swing_mode')
def test_set_target_temperature(self):
"""Test setting the target temperature."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 21 == state.attributes.get('temperature')
common.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'heat' == state.attributes.get('operation_mode')
self.mock_publish.async_publish.assert_called_once_with(
'mode-topic', 'heat', 0, False)
self.mock_publish.async_publish.reset_mock()
common.set_temperature(self.hass, temperature=47,
entity_id=ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 47 == state.attributes.get('temperature')
self.mock_publish.async_publish.assert_called_once_with(
'temperature-topic', 47, 0, False)
# also test directly supplying the operation mode to set_temperature
self.mock_publish.async_publish.reset_mock()
common.set_temperature(self.hass, temperature=21,
operation_mode="cool",
entity_id=ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'cool' == state.attributes.get('operation_mode')
assert 21 == state.attributes.get('temperature')
self.mock_publish.async_publish.assert_has_calls([
unittest.mock.call('mode-topic', 'cool', 0, False),
unittest.mock.call('temperature-topic', 21, 0, False)
])
self.mock_publish.async_publish.reset_mock()
def test_set_target_temperature_pessimistic(self):
"""Test setting the target temperature."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['temperature_state_topic'] = 'temperature-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('temperature') is None
common.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE)
self.hass.block_till_done()
common.set_temperature(self.hass, temperature=47,
entity_id=ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('temperature') is None
fire_mqtt_message(self.hass, 'temperature-state', '1701')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 1701 == state.attributes.get('temperature')
fire_mqtt_message(self.hass, 'temperature-state', 'not a number')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 1701 == state.attributes.get('temperature')
def test_receive_mqtt_temperature(self):
"""Test getting the current temperature via MQTT."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['current_temperature_topic'] = 'current_temperature'
mock_component(self.hass, 'mqtt')
assert setup_component(self.hass, climate.DOMAIN, config)
fire_mqtt_message(self.hass, 'current_temperature', '47')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 47 == state.attributes.get('current_temperature')
def test_set_away_mode_pessimistic(self):
"""Test setting of the away mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['away_mode_state_topic'] = 'away-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
common.set_away_mode(self.hass, True, ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
fire_mqtt_message(self.hass, 'away-state', 'ON')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('away_mode')
fire_mqtt_message(self.hass, 'away-state', 'OFF')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
fire_mqtt_message(self.hass, 'away-state', 'nonsense')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
def test_set_away_mode(self):
"""Test setting of the away mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['payload_on'] = 'AN'
config['climate']['payload_off'] = 'AUS'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
common.set_away_mode(self.hass, True, ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'away-mode-topic', 'AN', 0, False)
self.mock_publish.async_publish.reset_mock()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('away_mode')
common.set_away_mode(self.hass, False, ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'away-mode-topic', 'AUS', 0, False)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
def test_set_hold_pessimistic(self):
"""Test setting the hold mode in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['hold_state_topic'] = 'hold-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('hold_mode') is None
common.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('hold_mode') is None
fire_mqtt_message(self.hass, 'hold-state', 'on')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('hold_mode')
fire_mqtt_message(self.hass, 'hold-state', 'off')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('hold_mode')
def test_set_hold(self):
"""Test setting the hold mode."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('hold_mode') is None
common.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'hold-topic', 'on', 0, False)
self.mock_publish.async_publish.reset_mock()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('hold_mode')
common.set_hold_mode(self.hass, 'off', ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'hold-topic', 'off', 0, False)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('hold_mode')
def test_set_aux_pessimistic(self):
"""Test setting of the aux heating in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['aux_state_topic'] = 'aux-state'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
common.set_aux_heat(self.hass, True, ENTITY_CLIMATE)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
fire_mqtt_message(self.hass, 'aux-state', 'ON')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('aux_heat')
fire_mqtt_message(self.hass, 'aux-state', 'OFF')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
fire_mqtt_message(self.hass, 'aux-state', 'nonsense')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
def test_set_aux(self):
"""Test setting of the aux heating."""
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
common.set_aux_heat(self.hass, True, ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'aux-topic', 'ON', 0, False)
self.mock_publish.async_publish.reset_mock()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('aux_heat')
common.set_aux_heat(self.hass, False, ENTITY_CLIMATE)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'aux-topic', 'OFF', 0, False)
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
def test_custom_availability_payload(self):
"""Test availability by custom payload with defined topic."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['availability_topic'] = 'availability-topic'
config['climate']['payload_available'] = 'good'
config['climate']['payload_not_available'] = 'nogood'
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get('climate.test')
assert STATE_UNAVAILABLE == state.state
fire_mqtt_message(self.hass, 'availability-topic', 'good')
self.hass.block_till_done()
state = self.hass.states.get('climate.test')
assert STATE_UNAVAILABLE != state.state
fire_mqtt_message(self.hass, 'availability-topic', 'nogood')
self.hass.block_till_done()
state = self.hass.states.get('climate.test')
assert STATE_UNAVAILABLE == state.state
def test_set_with_templates(self):
"""Test setting of new fan mode in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
# By default, just unquote the JSON-strings
config['climate']['value_template'] = '{{ value_json }}'
# Something more complicated for hold mode
config['climate']['hold_state_template'] = \
'{{ value_json.attribute }}'
# Rendering to a bool for aux heat
config['climate']['aux_state_template'] = \
"{{ value == 'switchmeon' }}"
config['climate']['mode_state_topic'] = 'mode-state'
config['climate']['fan_mode_state_topic'] = 'fan-state'
config['climate']['swing_mode_state_topic'] = 'swing-state'
config['climate']['temperature_state_topic'] = 'temperature-state'
config['climate']['away_mode_state_topic'] = 'away-state'
config['climate']['hold_state_topic'] = 'hold-state'
config['climate']['aux_state_topic'] = 'aux-state'
config['climate']['current_temperature_topic'] = 'current-temperature'
assert setup_component(self.hass, climate.DOMAIN, config)
# Operation Mode
state = self.hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('operation_mode') is None
fire_mqtt_message(self.hass, 'mode-state', '"cool"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "cool" == state.attributes.get('operation_mode')
# Fan Mode
assert state.attributes.get('fan_mode') is None
fire_mqtt_message(self.hass, 'fan-state', '"high"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'high' == state.attributes.get('fan_mode')
# Swing Mode
assert state.attributes.get('swing_mode') is None
fire_mqtt_message(self.hass, 'swing-state', '"on"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert "on" == state.attributes.get('swing_mode')
# Temperature - with valid value
assert state.attributes.get('temperature') is None
fire_mqtt_message(self.hass, 'temperature-state', '"1031"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 1031 == state.attributes.get('temperature')
# Temperature - with invalid value
with self.assertLogs(level='ERROR') as log:
fire_mqtt_message(self.hass, 'temperature-state', '"-INVALID-"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
# make sure, the invalid value gets logged...
assert len(log.output) == 1
assert len(log.records) == 1
assert "Could not parse temperature from -INVALID-" in \
log.output[0]
# ... but the actual value stays unchanged.
assert 1031 == state.attributes.get('temperature')
# Away Mode
assert 'off' == state.attributes.get('away_mode')
fire_mqtt_message(self.hass, 'away-state', '"ON"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('away_mode')
# Away Mode with JSON values
fire_mqtt_message(self.hass, 'away-state', 'false')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('away_mode')
fire_mqtt_message(self.hass, 'away-state', 'true')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('away_mode')
# Hold Mode
assert state.attributes.get('hold_mode') is None
fire_mqtt_message(self.hass, 'hold-state', """
{ "attribute": "somemode" }
""")
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'somemode' == state.attributes.get('hold_mode')
# Aux mode
assert 'off' == state.attributes.get('aux_heat')
fire_mqtt_message(self.hass, 'aux-state', 'switchmeon')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'on' == state.attributes.get('aux_heat')
# anything other than 'switchmeon' should turn Aux mode off
fire_mqtt_message(self.hass, 'aux-state', 'somerandomstring')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 'off' == state.attributes.get('aux_heat')
# Current temperature
fire_mqtt_message(self.hass, 'current-temperature', '"74656"')
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
assert 74656 == state.attributes.get('current_temperature')
def test_min_temp_custom(self):
"""Test a custom min temp."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['min_temp'] = 26
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
min_temp = state.attributes.get('min_temp')
assert isinstance(min_temp, float)
assert 26 == state.attributes.get('min_temp')
def test_max_temp_custom(self):
"""Test a custom max temp."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['max_temp'] = 60
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
max_temp = state.attributes.get('max_temp')
assert isinstance(max_temp, float)
assert 60 == max_temp
def test_temp_step_custom(self):
"""Test a custom temp step."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['temp_step'] = 0.01
assert setup_component(self.hass, climate.DOMAIN, config)
state = self.hass.states.get(ENTITY_CLIMATE)
temp_step = state.attributes.get('target_temp_step')
assert isinstance(temp_step, float)
assert 0.01 == temp_step
async def test_unique_id(hass):
"""Test unique id option only creates one climate per unique_id."""
await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, climate.DOMAIN, {
climate.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'status_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'status_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
async_fire_mqtt_message(hass, 'test-topic', 'payload')
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(climate.DOMAIN)) == 1
async def test_discovery_removal_climate(hass, mqtt_mock, caplog):
"""Test removal of discovered climate."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data = (
'{ "name": "Beer" }'
)
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
data)
await hass.async_block_till_done()
state = hass.states.get('climate.beer')
assert state is not None
assert state.name == 'Beer'
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
'')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('climate.beer')
assert state is None
async def test_discovery_update_climate(hass, mqtt_mock, caplog):
"""Test removal of discovered climate."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data1 = (
'{ "name": "Beer" }'
)
data2 = (
'{ "name": "Milk" }'
)
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
data1)
await hass.async_block_till_done()
state = hass.states.get('climate.beer')
assert state is not None
assert state.name == 'Beer'
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
data2)
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('climate.beer')
assert state is not None
assert state.name == 'Milk'
state = hass.states.get('climate.milk')
assert state is None
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT climate device registry integration."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
entry.add_to_hass(hass)
await async_start(hass, 'homeassistant', {}, entry)
registry = await hass.helpers.device_registry.async_get_registry()
data = json.dumps({
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'command_topic': 'test-topic',
'device': {
'identifiers': ['helloworld'],
'connections': [
["mac", "02:5b:26:a8:dc:12"],
],
'manufacturer': 'Whatever',
'name': 'Beer',
'model': 'Glass',
'sw_version': '0.1-beta',
},
'unique_id': 'veryunique'
})
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
data)
await hass.async_block_till_done()
await hass.async_block_till_done()
device = registry.async_get_device({('mqtt', 'helloworld')}, set())
assert device is not None
assert device.identifiers == {('mqtt', 'helloworld')}
assert device.connections == {('mac', "02:5b:26:a8:dc:12")}
assert device.manufacturer == 'Whatever'
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'
async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, climate.DOMAIN, {
climate.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'mode_state_topic': 'test-topic',
'availability_topic': 'avty-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
state = hass.states.get('climate.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('avty-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()
registry.async_update_entity('climate.beer', new_entity_id='climate.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('climate.beer')
assert state is None
state = hass.states.get('climate.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('avty-topic', ANY, 0, 'utf-8')