11"""Test niko_home_control config flow."""
22
3- from unittest .mock import AsyncMock , patch
3+ from unittest .mock import AsyncMock
4+
5+ import pytest
46
57from homeassistant .components .niko_home_control .const import DOMAIN
68from homeassistant .config_entries import SOURCE_USER
@@ -36,34 +38,45 @@ async def test_full_flow(
3638 assert len (mock_setup_entry .mock_calls ) == 1
3739
3840
39- async def test_cannot_connect (hass : HomeAssistant ) -> None :
40- """Test the cannot connect error."""
41+ @pytest .mark .parametrize (
42+ ("exception" , "error" ),
43+ [
44+ (TimeoutError , "timeout_connect" ),
45+ (OSError , "cannot_connect" ),
46+ (Exception , "unknown" ),
47+ ],
48+ )
49+ async def test_flow_errors (
50+ hass : HomeAssistant ,
51+ mock_niko_home_control_connection : AsyncMock ,
52+ mock_setup_entry : AsyncMock ,
53+ exception : Exception ,
54+ error : str ,
55+ ) -> None :
56+ """Test the timeout error."""
4157
4258 result = await hass .config_entries .flow .async_init (
4359 DOMAIN , context = {"source" : SOURCE_USER }
4460 )
4561 assert result ["type" ] is FlowResultType .FORM
4662 assert result ["errors" ] == {}
4763
48- with patch (
49- "homeassistant.components.niko_home_control.config_flow.NHCController.connect" ,
50- side_effect = Exception ,
51- ):
52- result = await hass .config_entries .flow .async_configure (
53- result ["flow_id" ],
54- {CONF_HOST : "192.168.0.123" },
55- )
64+ mock_niko_home_control_connection .connect .side_effect = exception
65+
66+ result = await hass .config_entries .flow .async_configure (
67+ result ["flow_id" ],
68+ {CONF_HOST : "192.168.0.123" },
69+ )
5670
5771 assert result ["type" ] is FlowResultType .FORM
58- assert result ["errors" ] == {"base" : "cannot_connect" }
72+ assert result ["errors" ] == {"base" : error }
5973
60- with patch (
61- "homeassistant.components.niko_home_control.config_flow.NHCController.connect" ,
62- ):
63- result = await hass .config_entries .flow .async_configure (
64- result ["flow_id" ],
65- {CONF_HOST : "192.168.0.123" },
66- )
74+ mock_niko_home_control_connection .connect .side_effect = None
75+
76+ result = await hass .config_entries .flow .async_configure (
77+ result ["flow_id" ],
78+ {CONF_HOST : "192.168.0.123" },
79+ )
6780
6881 assert result ["type" ] is FlowResultType .CREATE_ENTRY
6982
@@ -119,6 +132,7 @@ async def test_reconfigure(
119132 hass : HomeAssistant ,
120133 mock_niko_home_control_connection : AsyncMock ,
121134 mock_config_entry : MockConfigEntry ,
135+ mock_setup_entry : AsyncMock ,
122136) -> None :
123137 """Test the reconfigure flow."""
124138 mock_config_entry .add_to_hass (hass )
@@ -136,28 +150,39 @@ async def test_reconfigure(
136150 assert result ["reason" ] == "reconfigure_successful"
137151
138152
139- async def test_reconfigure_cannot_connect (
153+ @pytest .mark .parametrize (
154+ ("exception" , "error" ),
155+ [
156+ (TimeoutError , "timeout_connect" ),
157+ (OSError , "cannot_connect" ),
158+ (Exception , "unknown" ),
159+ ],
160+ )
161+ async def test_reconfigure_errors (
140162 hass : HomeAssistant ,
141163 mock_niko_home_control_connection : AsyncMock ,
142164 mock_config_entry : MockConfigEntry ,
165+ mock_setup_entry : AsyncMock ,
166+ exception : Exception ,
167+ error : str ,
143168) -> None :
144169 """Test reconfiguration with connection error."""
145170 mock_config_entry .add_to_hass (hass )
146171
147- mock_niko_home_control_connection .connect .side_effect = Exception ("cannot_connect" )
148-
149172 result = await mock_config_entry .start_reconfigure_flow (hass )
150173
151174 assert result ["type" ] is FlowResultType .FORM
152175 assert result ["errors" ] == {}
153176
177+ mock_niko_home_control_connection .connect .side_effect = exception
178+
154179 result = await hass .config_entries .flow .async_configure (
155180 result ["flow_id" ],
156181 {CONF_HOST : "192.168.0.122" },
157182 )
158183
159184 assert result ["type" ] is FlowResultType .FORM
160- assert result ["errors" ] == {"base" : "cannot_connect" }
185+ assert result ["errors" ] == {"base" : error }
161186
162187 mock_niko_home_control_connection .connect .side_effect = None
163188
0 commit comments