/
test_config_flow.py
823 lines (656 loc) · 27.7 KB
/
test_config_flow.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
from typing import Any, Mapping
from unittest.mock import patch
import pytest
import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import sensor
from homeassistant.const import (
CONF_ENTITY_ID,
CONF_NAME,
CONF_PLATFORM,
CONF_UNIQUE_ID,
CONF_UNIT_OF_MEASUREMENT,
POWER_WATT,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.selector import SelectSelector
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component
import custom_components.test.sensor as test_sensor_platform
from custom_components.powercalc.config_flow import (
CONF_CONFIRM_AUTODISCOVERED_MODEL,
DOMAIN,
)
from custom_components.powercalc.const import (
CONF_CALCULATION_ENABLED_CONDITION,
CONF_CREATE_ENERGY_SENSOR,
CONF_CREATE_UTILITY_METERS,
CONF_DAILY_FIXED_ENERGY,
CONF_FIXED,
CONF_GROUP_MEMBER_SENSORS,
CONF_GROUP_POWER_ENTITIES,
CONF_HIDE_MEMBERS,
CONF_LINEAR,
CONF_MANUFACTURER,
CONF_MAX_POWER,
CONF_MIN_POWER,
CONF_MODE,
CONF_MODEL,
CONF_POWER,
CONF_POWER_FACTOR,
CONF_POWER_TEMPLATE,
CONF_SENSOR_TYPE,
CONF_STATES_POWER,
CONF_SUB_PROFILE,
CONF_UPDATE_FREQUENCY,
CONF_VALUE,
CONF_VOLTAGE,
CONF_WLED,
CalculationStrategy,
SensorType,
)
from custom_components.powercalc.errors import StrategyConfigurationError
from custom_components.test.light import MockLight
from .common import (
MockConfigEntry,
create_mock_light_entity,
create_mocked_virtual_power_sensor_entry,
)
DEFAULT_ENTITY_ID = "light.test"
DEFAULT_UNIQUE_ID = "7c009ef6829f"
async def test_discovery_flow(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON, DEFAULT_UNIQUE_ID)
light_entity.manufacturer = "signify"
light_entity.model = "LCT010"
await create_mock_light_entity(hass, light_entity)
result: FlowResult = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
data={
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_NAME: "test",
CONF_ENTITY_ID: DEFAULT_ENTITY_ID,
CONF_MANUFACTURER: "signify",
CONF_MODEL: "LCT010",
},
)
# Confirm selected manufacturer/model
assert result["type"] == data_entry_flow.FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_CONFIRM_AUTODISCOVERED_MODEL: True}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_ENTITY_ID: DEFAULT_ENTITY_ID,
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MANUFACTURER: "signify",
CONF_MODEL: "LCT010",
CONF_NAME: "test",
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
}
async def test_sensor_type_menu_displayed(hass: HomeAssistant):
"""Test a menu is diplayed with sensor type selection"""
result: FlowResult = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.MENU
assert result["step_id"] == "user"
@pytest.mark.parametrize(
"sensor_type",
[SensorType.VIRTUAL_POWER, SensorType.DAILY_ENERGY, SensorType.GROUP],
)
async def test_sensor_type_form_displayed(hass: HomeAssistant, sensor_type: SensorType):
await _select_sensor_type(hass, sensor_type)
async def test_create_fixed_sensor_entry(hass: HomeAssistant):
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.FIXED)
result = await _set_virtual_power_configuration(hass, result, {CONF_POWER: 20})
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.FIXED, result["data"], {CONF_FIXED: {CONF_POWER: 20}}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_create_fixed_sensor_entry_with_template(hass: HomeAssistant):
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.FIXED)
template = "{states(input_number.my_number} | float"
result = await _set_virtual_power_configuration(
hass, result, {CONF_POWER_TEMPLATE: template}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.FIXED,
result["data"],
{CONF_FIXED: {CONF_POWER: template, CONF_POWER_TEMPLATE: template}},
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_create_fixed_sensor_entry_with_states_power(hass: HomeAssistant):
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.FIXED)
result = await _set_virtual_power_configuration(
hass, result, {CONF_STATES_POWER: ""}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.FIXED, result["data"], {CONF_FIXED: {CONF_STATES_POWER: ""}}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_create_linear_sensor_entry(hass: HomeAssistant):
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LINEAR)
result = await _set_virtual_power_configuration(
hass, result, {CONF_MIN_POWER: 1, CONF_MAX_POWER: 40}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.LINEAR,
result["data"],
{CONF_LINEAR: {CONF_MIN_POWER: 1, CONF_MAX_POWER: 40}},
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_create_linear_sensor_error_mandatory_fields(hass: HomeAssistant):
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LINEAR)
result = await _set_virtual_power_configuration(hass, result, {CONF_MIN_POWER: 20})
assert result["errors"]
assert result["errors"]["base"] == "linear_mandatory"
assert result["type"] == data_entry_flow.FlowResultType.FORM
async def test_create_wled_sensor_entry(hass: HomeAssistant):
await _create_wled_entities(hass)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.WLED)
result = await _set_virtual_power_configuration(
hass, result, {CONF_VOLTAGE: 12, CONF_POWER_FACTOR: 0.8}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.WLED,
result["data"],
{CONF_WLED: {CONF_VOLTAGE: 12, CONF_POWER_FACTOR: 0.8}},
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_lut_manual_flow(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON, DEFAULT_UNIQUE_ID)
await create_mock_light_entity(hass, light_entity)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_manufacturer"
data_schema: vol.Schema = result["data_schema"]
manufacturer_select: SelectSelector = data_schema.schema["manufacturer"]
manufacturer_options = manufacturer_select.config["options"]
assert {"value": "belkin", "label": "belkin"} in manufacturer_options
assert {"value": "signify", "label": "signify"} in manufacturer_options
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_MANUFACTURER: "signify"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_model"
data_schema: vol.Schema = result["data_schema"]
model_select: SelectSelector = data_schema.schema["model"]
model_options = model_select.config["options"]
assert {"value": "LCT010", "label": "LCT010"} in model_options
assert {"value": "LWB010", "label": "LWB010"} in model_options
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_MODEL: "LCT010"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
# Advanced options step
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.LUT,
result["data"],
{CONF_MANUFACTURER: "signify", CONF_MODEL: "LCT010"},
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_lut_autodiscover_flow(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON, DEFAULT_UNIQUE_ID)
light_entity.manufacturer = "ikea"
light_entity.model = "LED1545G12"
await create_mock_light_entity(hass, light_entity)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut"
result = await _set_virtual_power_configuration(
hass, result, {CONF_CONFIRM_AUTODISCOVERED_MODEL: True}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.LUT,
result["data"],
{CONF_MANUFACTURER: light_entity.manufacturer, CONF_MODEL: light_entity.model},
)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_lut_not_autodiscovered_model_unsupported(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON)
light_entity.manufacturer = "ikea"
# Set to model which is not in library
light_entity.model = "unknown_model"
await create_mock_light_entity(hass, light_entity)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_manufacturer"
async def test_lut_not_autodiscovered(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON)
light_entity._attr_unique_id = None
await create_mock_light_entity(hass, light_entity)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_manufacturer"
async def test_lut_autodiscover_flow_not_confirmed(hass: HomeAssistant):
"""
When manufacturer and model are auto detected and user chooses to not accept it,
make sure he/she is forwarded to the manufacturer listing
"""
light_entity = MockLight("test", STATE_ON, "234438")
light_entity.manufacturer = "ikea"
light_entity.model = "LED1545G12"
await create_mock_light_entity(hass, light_entity)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_CONFIRM_AUTODISCOVERED_MODEL: False}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_manufacturer"
async def test_lut_flow_with_sub_profiles(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON, DEFAULT_UNIQUE_ID)
await create_mock_light_entity(hass, light_entity)
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_MANUFACTURER: "yeelight"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_MODEL: "YLDL01YL"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_subprofile"
data_schema: vol.Schema = result["data_schema"]
model_select: SelectSelector = data_schema.schema["sub_profile"]
select_options = model_select.config["options"]
assert {"value": "ambilight", "label": "ambilight"} in select_options
assert {"value": "downlight", "label": "downlight"} in select_options
result = await _set_virtual_power_configuration(
hass, result, {CONF_SUB_PROFILE: "ambilight"}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.LUT,
result["data"],
{CONF_MANUFACTURER: "yeelight", CONF_MODEL: "YLDL01YL/ambilight"},
)
async def test_advanced_power_configuration_can_be_set(hass: HomeAssistant):
result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.FIXED)
advanced_options = {
CONF_CALCULATION_ENABLED_CONDITION: "{{ is_state('vacuum.my_robot_cleaner', 'docked') }}"
}
result = await _set_virtual_power_configuration(
hass, result, {CONF_POWER: 20}, advanced_options
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.FIXED,
result["data"],
{
CONF_FIXED: {CONF_POWER: 20},
CONF_CALCULATION_ENABLED_CONDITION: "{{ is_state('vacuum.my_robot_cleaner', 'docked') }}",
},
)
async def test_daily_energy_mandatory_fields_not_supplied(hass: HomeAssistant):
result = await _select_sensor_type(hass, SensorType.DAILY_ENERGY)
user_input = {CONF_NAME: "My daily energy sensor"}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"]
assert result["errors"] == {"base": "daily_energy_mandatory"}
async def test_create_daily_energy_entry(hass: HomeAssistant):
result = await _select_sensor_type(hass, SensorType.DAILY_ENERGY)
user_input = {
CONF_NAME: "My daily energy sensor",
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_VALUE: 0.5,
CONF_UNIT_OF_MEASUREMENT: POWER_WATT,
}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_SENSOR_TYPE: SensorType.DAILY_ENERGY,
CONF_NAME: "My daily energy sensor",
CONF_DAILY_FIXED_ENERGY: {
CONF_UPDATE_FREQUENCY: 1800,
CONF_VALUE: 0.5,
CONF_UNIT_OF_MEASUREMENT: POWER_WATT,
},
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
}
await hass.async_block_till_done()
assert hass.states.get("sensor.my_daily_energy_sensor_energy")
async def test_create_group_entry(hass: HomeAssistant):
result = await _select_sensor_type(hass, SensorType.GROUP)
user_input = {
CONF_NAME: "My group sensor",
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_GROUP_POWER_ENTITIES: ["sensor.balcony_power", "sensor.bedroom1_power"],
}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "My group sensor",
CONF_HIDE_MEMBERS: False,
CONF_GROUP_POWER_ENTITIES: ["sensor.balcony_power", "sensor.bedroom1_power"],
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_CREATE_UTILITY_METERS: False,
}
await hass.async_block_till_done()
assert hass.states.get("sensor.my_group_sensor_power")
async def test_can_select_existing_powercalc_entry_as_group_member(hass: HomeAssistant):
"""
Test if we can select previously created virtual power config entries as the group member.
Only entries with a unique ID must be selectable
"""
config_entry_1 = await create_mocked_virtual_power_sensor_entry(
hass, "VirtualPower1", "abcdef"
)
config_entry_2 = await create_mocked_virtual_power_sensor_entry(
hass, "VirtualPower2", None
)
result = await _select_sensor_type(hass, SensorType.GROUP)
assert result["type"] == data_entry_flow.FlowResultType.FORM
data_schema: vol.Schema = result["data_schema"]
select: SelectSelector = data_schema.schema[CONF_GROUP_MEMBER_SENSORS]
options = select.config["options"]
assert {"value": config_entry_1.entry_id, "label": "VirtualPower1"} in options
assert {"value": config_entry_2.entry_id, "label": "VirtualPower2"} not in options
user_input = {
CONF_NAME: "My group sensor",
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_GROUP_MEMBER_SENSORS: [config_entry_1.entry_id],
}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "My group sensor",
CONF_HIDE_MEMBERS: False,
CONF_GROUP_MEMBER_SENSORS: [config_entry_1.entry_id],
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
CONF_CREATE_UTILITY_METERS: False,
}
async def test_group_error_mandatory(hass: HomeAssistant):
result = await _select_sensor_type(hass, SensorType.GROUP)
user_input = {CONF_NAME: "My group sensor", CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"]
assert result["errors"]["base"] == "group_mandatory"
async def test_fixed_options_flow(hass: HomeAssistant):
entry = _create_mock_entry(
hass,
{
CONF_ENTITY_ID: "light.test",
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: CalculationStrategy.FIXED,
CONF_FIXED: {CONF_POWER: 40},
},
)
result = await _initialize_options_flow(hass, entry)
user_input = {CONF_POWER: 50}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_FIXED][CONF_POWER] == 50
async def test_wled_options_flow(hass: HomeAssistant):
await _create_wled_entities(hass)
entry = _create_mock_entry(
hass,
{
CONF_ENTITY_ID: "light.test",
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: CalculationStrategy.WLED,
CONF_WLED: {CONF_VOLTAGE: 5},
},
)
result = await _initialize_options_flow(hass, entry)
user_input = {CONF_VOLTAGE: 12}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_WLED][CONF_VOLTAGE] == 12
async def test_linear_options_flow(hass: HomeAssistant):
entry = _create_mock_entry(
hass,
{
CONF_ENTITY_ID: "light.test",
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: CalculationStrategy.LINEAR,
CONF_LINEAR: {CONF_MIN_POWER: 10, CONF_MAX_POWER: 40},
},
)
result = await _initialize_options_flow(hass, entry)
user_input = {CONF_MAX_POWER: 50}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_LINEAR][CONF_MAX_POWER] == 50
async def test_daily_energy_options_flow(hass: HomeAssistant):
entry = _create_mock_entry(
hass,
{
CONF_NAME: "My daily energy sensor",
CONF_SENSOR_TYPE: SensorType.DAILY_ENERGY,
CONF_DAILY_FIXED_ENERGY: {CONF_VALUE: 50},
},
)
result = await _initialize_options_flow(hass, entry)
user_input = {CONF_VALUE: 75, CONF_UNIT_OF_MEASUREMENT: POWER_WATT}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_DAILY_FIXED_ENERGY][CONF_UNIT_OF_MEASUREMENT] == POWER_WATT
assert entry.data[CONF_DAILY_FIXED_ENERGY][CONF_VALUE] == 75
async def test_lut_options_flow(hass: HomeAssistant):
entry = _create_mock_entry(
hass,
{
CONF_ENTITY_ID: "light.spots_kitchen",
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: CalculationStrategy.LUT,
CONF_CREATE_ENERGY_SENSOR: True,
CONF_MANUFACTURER: "signify",
CONF_MODEL: "LCT010",
},
)
result = await _initialize_options_flow(hass, entry)
user_input = {CONF_CREATE_ENERGY_SENSOR: False}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_CREATE_ENERGY_SENSOR] == False
async def test_group_options_flow(hass: HomeAssistant):
entry = _create_mock_entry(
hass,
{
CONF_NAME: "Kitchen",
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_GROUP_POWER_ENTITIES: ["sensor.fridge_power"],
},
)
result = await _initialize_options_flow(hass, entry)
new_entities = ["sensor.fridge_power", "sensor.kitchen_lights_power"]
user_input = {CONF_GROUP_POWER_ENTITIES: new_entities}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_GROUP_POWER_ENTITIES] == new_entities
# assert hass.states.get("sensor.kitchen_power").attributes.get(ATTR_ENTITIES) == new_entities
async def test_linear_options_flow_error(hass: HomeAssistant):
entry = _create_mock_entry(
hass,
{
CONF_ENTITY_ID: "light.test",
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: CalculationStrategy.LINEAR,
CONF_LINEAR: {CONF_MIN_POWER: 40, CONF_MAX_POWER: 50},
},
)
result = await _initialize_options_flow(hass, entry)
user_input = {CONF_MIN_POWER: 55, CONF_MAX_POWER: 50}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"]
assert result["errors"]["base"] == "linear_min_higher_as_max"
async def test_strategy_raises_unknown_error(hass: HomeAssistant):
with patch(
"custom_components.powercalc.strategy.fixed.FixedStrategy.validate_config",
side_effect=StrategyConfigurationError("test"),
):
result = await _goto_virtual_power_strategy_step(
hass, CalculationStrategy.FIXED
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_POWER: 20}
)
assert result["errors"]
assert result["errors"]["base"] == "unknown"
assert result["type"] == data_entry_flow.FlowResultType.FORM
def _create_mock_entry(hass: HomeAssistant, entry_data: ConfigType) -> MockConfigEntry:
entry = MockConfigEntry(domain=DOMAIN, data=entry_data)
entry.add_to_hass(hass)
assert not entry.options
return entry
async def _initialize_options_flow(
hass: HomeAssistant, entry: config_entries.ConfigEntry
) -> FlowResult:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(
entry.entry_id,
data=None,
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
return result
def _assert_default_virtual_power_entry_data(
strategy: CalculationStrategy,
config_entry_data: Mapping[str, Any],
expected_strategy_options: dict,
):
assert (
config_entry_data
== {
CONF_ENTITY_ID: DEFAULT_ENTITY_ID,
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: strategy,
CONF_CREATE_ENERGY_SENSOR: True,
CONF_CREATE_UTILITY_METERS: False,
CONF_NAME: "test",
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
}
| expected_strategy_options
)
async def _goto_virtual_power_strategy_step(
hass: HomeAssistant,
strategy: CalculationStrategy,
user_input: dict[str, Any] | None = None,
) -> FlowResult:
"""
- Select the virtual power sensor type
- Select the given calculation strategy and put in default configuration options
"""
if user_input is None:
user_input = {
CONF_ENTITY_ID: DEFAULT_ENTITY_ID,
CONF_MODE: strategy,
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
}
result = await _select_sensor_type(hass, SensorType.VIRTUAL_POWER)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
# Lut has alternate flows depending on auto discovery, don't need to assert here
if strategy != CalculationStrategy.LUT:
assert result["step_id"] == strategy
assert result["type"] == data_entry_flow.FlowResultType.FORM
return result
async def _set_virtual_power_configuration(
hass: HomeAssistant,
previous_result: FlowResult,
basic_options: dict[str, Any] | None = None,
advanced_options: dict[str, Any] | None = None,
) -> FlowResult:
if basic_options is None:
basic_options = {}
result = await hass.config_entries.flow.async_configure(
previous_result["flow_id"], basic_options
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
if advanced_options is None:
advanced_options = {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], advanced_options
)
return result
async def _select_sensor_type(
hass: HomeAssistant, sensor_type: SensorType
) -> FlowResult:
"""Select a sensor type from the menu"""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {"next_step_id": sensor_type}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == sensor_type
return result
async def _create_wled_entities(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON, DEFAULT_UNIQUE_ID)
await create_mock_light_entity(hass, light_entity)
platform: test_sensor_platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
estimated_current_entity = platform.MockSensor(
name="test_estimated_current", native_value="5.0", unique_id=DEFAULT_UNIQUE_ID
)
platform.ENTITIES[0] = estimated_current_entity
assert await async_setup_component(
hass, sensor.DOMAIN, {sensor.DOMAIN: {CONF_PLATFORM: "test"}}
)
await hass.async_block_till_done()