133
133
CONF_SCAN_INTERVAL : 15 ,
134
134
}
135
135
136
+ DUMMY_ENTRY_CHANGE_IP : dict [str , Any ] = {
137
+ CONF_SCAN_INTERVAL : 15 ,
138
+ CONF_HOST : "http://localhost2"
139
+ }
140
+
136
141
DUMMY_CONFIG_ENTRY_UPDATED : dict [str , Any ] = {
137
142
CONF_HOST : "http://localhost" ,
138
143
CONF_USERNAME : "test" ,
149
154
],
150
155
}
151
156
157
+ DUMMY_CONFIG_ENTRY_UPDATED_IP : dict [str , Any ] = {
158
+ CONF_HOST : "http://localhost2" ,
159
+ CONF_USERNAME : "test" ,
160
+ CONF_PASSWORD : "test" ,
161
+ NEW_UID : True ,
162
+ CONF_SCAN_INTERVAL : 15 ,
163
+ CONF_DEVICES : [
164
+ {
165
+ CONF_DEVICE_ID : "2" ,
166
+ CONF_DEVICE_FETCH_MODE : "all" ,
167
+ CONF_DEVICE_TYPE : "UVR16x2" ,
168
+ CONF_CHANNELS : [],
169
+ }
170
+ ],
171
+ }
172
+
152
173
DUMMY_API = CMIAPI ("" , "" , "" )
153
174
154
175
@@ -526,8 +547,8 @@ async def test_step_channels_edit_more(hass: HomeAssistant) -> None:
526
547
527
548
528
549
@pytest .mark .asyncio
529
- async def test_options_flow_init (hass : HomeAssistant ) -> None :
530
- """Test config flow options."""
550
+ async def test_options_flow_init_no_ip (hass : HomeAssistant ) -> None :
551
+ """Test config flow options with no ip change ."""
531
552
532
553
config_entry = MockConfigEntry (
533
554
domain = DOMAIN ,
@@ -552,3 +573,133 @@ async def test_options_flow_init(hass: HomeAssistant) -> None:
552
573
553
574
assert result ["type" ] == data_entry_flow .RESULT_TYPE_CREATE_ENTRY
554
575
assert dict (config_entry .options ) == DUMMY_CONFIG_ENTRY_UPDATED
576
+
577
+ @pytest .mark .asyncio
578
+ async def test_options_flow_init (hass : HomeAssistant ) -> None :
579
+ """Test config flow options with ip change."""
580
+ config_entry = MockConfigEntry (
581
+ domain = DOMAIN ,
582
+ title = "C.M.I" ,
583
+ data = DUMMY_CONFIG_ENTRY ,
584
+ )
585
+ config_entry .add_to_hass (hass )
586
+
587
+ with patch ("custom_components.ta_cmi.async_setup_entry" , return_value = True ), patch (
588
+ "ta_cmi.cmi_api.CMIAPI._make_request_no_json" ,
589
+ return_value = "2;" ,
590
+ ), patch (
591
+ "ta_cmi.cmi_api.CMIAPI._make_request_get" , return_value = DUMMY_DEVICE_API_DATA
592
+ ), patch (
593
+ "asyncio.sleep" , wraps = sleep_mock
594
+ ):
595
+ result = await hass .config_entries .options .async_init (config_entry .entry_id )
596
+
597
+ await hass .config_entries .async_setup (config_entry .entry_id )
598
+ await hass .async_block_till_done ()
599
+
600
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
601
+ assert result ["step_id" ] == "init"
602
+
603
+ result = await hass .config_entries .options .async_configure (
604
+ result ["flow_id" ],
605
+ user_input = DUMMY_ENTRY_CHANGE_IP ,
606
+ )
607
+
608
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_CREATE_ENTRY
609
+ assert dict (config_entry .options ) == DUMMY_CONFIG_ENTRY_UPDATED_IP
610
+
611
+ @pytest .mark .asyncio
612
+ async def test_options_flow_ip_change_invalid_auth (hass : HomeAssistant ) -> None :
613
+ """Test config flow options with ip change and invalid auth."""
614
+ config_entry = MockConfigEntry (
615
+ domain = DOMAIN ,
616
+ title = "C.M.I" ,
617
+ data = DUMMY_CONFIG_ENTRY ,
618
+ )
619
+ config_entry .add_to_hass (hass )
620
+
621
+ with patch ("custom_components.ta_cmi.async_setup_entry" , return_value = True ), patch (
622
+ "ta_cmi.cmi_api.CMIAPI._make_request_no_json" ,
623
+ side_effect = InvalidCredentialsError ("Invalid API key" ),
624
+ ):
625
+ result = await hass .config_entries .options .async_init (config_entry .entry_id )
626
+
627
+ await hass .config_entries .async_setup (config_entry .entry_id )
628
+ await hass .async_block_till_done ()
629
+
630
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
631
+ assert result ["step_id" ] == "init"
632
+
633
+ result = await hass .config_entries .options .async_configure (
634
+ result ["flow_id" ],
635
+ user_input = DUMMY_ENTRY_CHANGE_IP ,
636
+ )
637
+
638
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
639
+ assert result ["step_id" ] == "init"
640
+ assert result ["errors" ] == {"base" : "invalid_auth" }
641
+ assert dict (config_entry .options ) == {}
642
+
643
+ @pytest .mark .asyncio
644
+ async def test_options_flow_ip_change_connection_error (hass : HomeAssistant ) -> None :
645
+ """Test config flow options with ip change and connection error."""
646
+ config_entry = MockConfigEntry (
647
+ domain = DOMAIN ,
648
+ title = "C.M.I" ,
649
+ data = DUMMY_CONFIG_ENTRY ,
650
+ )
651
+ config_entry .add_to_hass (hass )
652
+
653
+ with patch ("custom_components.ta_cmi.async_setup_entry" , return_value = True ), patch (
654
+ "ta_cmi.cmi_api.CMIAPI._make_request_no_json" ,
655
+ side_effect = ApiError ("Could not connect to C.M.I." ),
656
+ ):
657
+ result = await hass .config_entries .options .async_init (config_entry .entry_id )
658
+
659
+ await hass .config_entries .async_setup (config_entry .entry_id )
660
+ await hass .async_block_till_done ()
661
+
662
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
663
+ assert result ["step_id" ] == "init"
664
+
665
+ result = await hass .config_entries .options .async_configure (
666
+ result ["flow_id" ],
667
+ user_input = DUMMY_ENTRY_CHANGE_IP ,
668
+ )
669
+
670
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
671
+ assert result ["step_id" ] == "init"
672
+ assert result ["errors" ] == {"base" : "cannot_connect" }
673
+ assert dict (config_entry .options ) == {}
674
+
675
+ @pytest .mark .asyncio
676
+ async def test_options_flow_ip_change_unexpected_error (hass : HomeAssistant ) -> None :
677
+ """Test config flow options with ip change and unexpected error."""
678
+ config_entry = MockConfigEntry (
679
+ domain = DOMAIN ,
680
+ title = "C.M.I" ,
681
+ data = DUMMY_CONFIG_ENTRY ,
682
+ )
683
+ config_entry .add_to_hass (hass )
684
+
685
+ with patch ("custom_components.ta_cmi.async_setup_entry" , return_value = True ), patch (
686
+ "ta_cmi.cmi_api.CMIAPI._make_request_no_json" ,
687
+ side_effect = Exception ("DUMMY" ),
688
+ ):
689
+ result = await hass .config_entries .options .async_init (config_entry .entry_id )
690
+
691
+ await hass .config_entries .async_setup (config_entry .entry_id )
692
+ await hass .async_block_till_done ()
693
+
694
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
695
+ assert result ["step_id" ] == "init"
696
+
697
+ result = await hass .config_entries .options .async_configure (
698
+ result ["flow_id" ],
699
+ user_input = DUMMY_ENTRY_CHANGE_IP ,
700
+ )
701
+
702
+ assert result ["type" ] == data_entry_flow .RESULT_TYPE_FORM
703
+ assert result ["step_id" ] == "init"
704
+ assert result ["errors" ] == {"base" : "unknown" }
705
+ assert dict (config_entry .options ) == {}
0 commit comments