4747
4848async def test_form (hass : HomeAssistant ) -> None :
4949 """Test we get the form."""
50+ bulb = _mocked_wizlight (None , None , FAKE_DIMMABLE_BULB )
51+
5052 result = await hass .config_entries .flow .async_init (
5153 DOMAIN , context = {"source" : config_entries .SOURCE_USER }
5254 )
5355 assert result ["type" ] is FlowResultType .FORM
5456 assert result ["errors" ] == {}
5557 # Patch functions
5658 with (
57- _patch_wizlight (),
59+ _patch_wizlight (device = bulb ),
5860 patch (
5961 "homeassistant.components.wiz.async_setup_entry" ,
6062 return_value = True ,
@@ -76,6 +78,7 @@ async def test_form(hass: HomeAssistant) -> None:
7678 }
7779 assert len (mock_setup .mock_calls ) == 1
7880 assert len (mock_setup_entry .mock_calls ) == 1
81+ bulb .async_close .assert_awaited_once ()
7982
8083
8184async def test_user_flow_enters_dns_name (hass : HomeAssistant ) -> None :
@@ -137,17 +140,18 @@ async def test_user_form_exceptions(
137140 DOMAIN , context = {"source" : config_entries .SOURCE_USER }
138141 )
139142
140- with patch (
141- "homeassistant.components.wiz.wizlight.getBulbConfig" ,
142- side_effect = side_effect ,
143- ):
143+ bulb = _mocked_wizlight ( None , None , FAKE_DIMMABLE_BULB )
144+ bulb . get_bulbtype = AsyncMock ( side_effect = side_effect )
145+
146+ with _patch_wizlight ( device = bulb ):
144147 result2 = await hass .config_entries .flow .async_configure (
145148 result ["flow_id" ],
146149 TEST_CONNECTION ,
147150 )
148151
149152 assert result2 ["type" ] is FlowResultType .FORM
150153 assert result2 ["errors" ] == {"base" : error_base }
154+ bulb .async_close .assert_awaited_once ()
151155
152156
153157async def test_form_updates_unique_id (hass : HomeAssistant ) -> None :
@@ -185,17 +189,18 @@ async def test_discovered_by_dhcp_connection_fails(
185189 hass : HomeAssistant , source , data
186190) -> None :
187191 """Test we abort on connection failure."""
188- with patch (
189- "homeassistant.components.wiz.wizlight.getBulbConfig" ,
190- side_effect = WizLightTimeOutError ,
191- ):
192+ bulb = _mocked_wizlight ( None , None , FAKE_DIMMABLE_BULB )
193+ bulb . get_bulbtype = AsyncMock ( side_effect = WizLightTimeOutError )
194+
195+ with _patch_wizlight ( device = bulb ):
192196 result = await hass .config_entries .flow .async_init (
193197 DOMAIN , context = {"source" : source }, data = data
194198 )
195199 await hass .async_block_till_done ()
196200
197201 assert result ["type" ] is FlowResultType .ABORT
198202 assert result ["reason" ] == "cannot_connect"
203+ bulb .async_close .assert_awaited_once ()
199204
200205
201206@pytest .mark .parametrize (
@@ -263,21 +268,22 @@ async def test_discovered_by_dhcp_or_integration_discovery(
263268 hass : HomeAssistant , source , data , bulb_type , extended_white_range , name
264269) -> None :
265270 """Test we can configure when discovered from dhcp or discovery."""
266- with _patch_wizlight (
267- device = None , extended_white_range = extended_white_range , bulb_type = bulb_type
268- ):
271+ bulb = _mocked_wizlight ( None , extended_white_range , bulb_type )
272+
273+ with _patch_wizlight ( device = bulb ):
269274 result = await hass .config_entries .flow .async_init (
270275 DOMAIN , context = {"source" : source }, data = data
271276 )
272277 await hass .async_block_till_done ()
273278
274279 assert result ["type" ] is FlowResultType .FORM
275280 assert result ["step_id" ] == "discovery_confirm"
281+ bulb .async_close .assert_awaited_once ()
282+
283+ bulb .async_close .reset_mock ()
276284
277285 with (
278- _patch_wizlight (
279- device = None , extended_white_range = extended_white_range , bulb_type = bulb_type
280- ),
286+ _patch_wizlight (device = bulb ),
281287 patch (
282288 "homeassistant.components.wiz.async_setup_entry" ,
283289 return_value = True ,
@@ -299,6 +305,7 @@ async def test_discovered_by_dhcp_or_integration_discovery(
299305 }
300306 assert len (mock_setup .mock_calls ) == 1
301307 assert len (mock_setup_entry .mock_calls ) == 1
308+ bulb .async_close .assert_awaited_once ()
302309
303310
304311@pytest .mark .parametrize (
@@ -393,8 +400,10 @@ async def test_setup_via_discovery(hass: HomeAssistant) -> None:
393400 assert result2 ["step_id" ] == "pick_device"
394401 assert not result2 ["errors" ]
395402
403+ bulb = _mocked_wizlight (None , None , FAKE_DIMMABLE_BULB )
404+
396405 with (
397- _patch_wizlight (),
406+ _patch_wizlight (device = bulb ),
398407 patch (
399408 "homeassistant.components.wiz.async_setup" , return_value = True
400409 ) as mock_setup ,
@@ -415,6 +424,7 @@ async def test_setup_via_discovery(hass: HomeAssistant) -> None:
415424 }
416425 assert len (mock_setup .mock_calls ) == 1
417426 assert len (mock_setup_entry .mock_calls ) == 1
427+ bulb .async_close .assert_awaited_once ()
418428
419429 # ignore configured devices
420430 result = await hass .config_entries .flow .async_init (
0 commit comments