11"""Define tests for the Brother Printer config flow."""
22
33from ipaddress import ip_address
4- from unittest .mock import AsyncMock , patch
4+ from unittest .mock import AsyncMock
55
66from brother import SnmpError , UnsupportedModelError
77import pytest
2727 SECTION_ADVANCED_SETTINGS : {CONF_PORT : 161 , CONF_COMMUNITY : "public" },
2828}
2929
30- pytestmark = pytest .mark .usefixtures ("mock_setup_entry" , "mock_unload_entry" )
31-
32-
33- async def test_show_form (hass : HomeAssistant ) -> None :
34- """Test that the form is served with no input."""
35- result = await hass .config_entries .flow .async_init (
36- DOMAIN , context = {"source" : SOURCE_USER }
37- )
38-
39- assert result ["type" ] is FlowResultType .FORM
40- assert result ["step_id" ] == "user"
30+ pytestmark = pytest .mark .usefixtures ("mock_setup_entry" )
4131
4232
4333@pytest .mark .parametrize ("host" , ["example.local" , "127.0.0.1" , "2001:db8::1428:57ab" ])
@@ -49,9 +39,15 @@ async def test_create_entry(
4939 config [CONF_HOST ] = host
5040
5141 result = await hass .config_entries .flow .async_init (
52- DOMAIN ,
53- context = {"source" : SOURCE_USER },
54- data = config ,
42+ DOMAIN , context = {"source" : SOURCE_USER }
43+ )
44+
45+ assert result ["type" ] is FlowResultType .FORM
46+ assert result ["step_id" ] == "user"
47+
48+ result = await hass .config_entries .flow .async_configure (
49+ flow_id = result ["flow_id" ],
50+ user_input = config ,
5551 )
5652
5753 assert result ["type" ] is FlowResultType .CREATE_ENTRY
@@ -60,6 +56,7 @@ async def test_create_entry(
6056 assert result ["data" ][CONF_TYPE ] == "laser"
6157 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_PORT ] == 161
6258 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_COMMUNITY ] == "public"
59+ assert result ["result" ].unique_id == "0123456789"
6360
6461
6562async def test_invalid_hostname (
@@ -97,6 +94,7 @@ async def test_invalid_hostname(
9794 assert result ["data" ][CONF_TYPE ] == "laser"
9895 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_PORT ] == 161
9996 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_COMMUNITY ] == "public"
97+ assert result ["result" ].unique_id == "0123456789"
10098
10199
102100@pytest .mark .parametrize (
@@ -142,6 +140,7 @@ async def test_errors(
142140 assert result ["data" ][CONF_TYPE ] == "laser"
143141 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_PORT ] == 161
144142 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_COMMUNITY ] == "public"
143+ assert result ["result" ].unique_id == "0123456789"
145144
146145
147146async def test_unsupported_model_error (
@@ -250,32 +249,31 @@ async def test_zeroconf_device_exists_abort(
250249 assert mock_config_entry .data [CONF_HOST ] == "127.0.0.1"
251250
252251
253- async def test_zeroconf_no_probe_existing_device (hass : HomeAssistant ) -> None :
252+ async def test_zeroconf_no_probe_existing_device (
253+ hass : HomeAssistant , mock_brother_client : AsyncMock
254+ ) -> None :
254255 """Test we do not probe the device is the host is already configured."""
255256 entry = MockConfigEntry (domain = DOMAIN , unique_id = "0123456789" , data = CONFIG )
256257 entry .add_to_hass (hass )
257- with (
258- patch ("homeassistant.components.brother.Brother.initialize" ),
259- patch ("homeassistant.components.brother.Brother._get_data" ) as mock_get_data ,
260- ):
261- result = await hass .config_entries .flow .async_init (
262- DOMAIN ,
263- context = {"source" : SOURCE_ZEROCONF },
264- data = ZeroconfServiceInfo (
265- ip_address = ip_address ("127.0.0.1" ),
266- ip_addresses = [ip_address ("127.0.0.1" )],
267- hostname = "example.local." ,
268- name = "Brother Printer" ,
269- port = None ,
270- properties = {},
271- type = "mock_type" ,
272- ),
273- )
274- await hass .async_block_till_done ()
258+
259+ result = await hass .config_entries .flow .async_init (
260+ DOMAIN ,
261+ context = {"source" : SOURCE_ZEROCONF },
262+ data = ZeroconfServiceInfo (
263+ ip_address = ip_address ("127.0.0.1" ),
264+ ip_addresses = [ip_address ("127.0.0.1" )],
265+ hostname = "example.local." ,
266+ name = "Brother Printer" ,
267+ port = None ,
268+ properties = {},
269+ type = "mock_type" ,
270+ ),
271+ )
272+ await hass .async_block_till_done ()
275273
276274 assert result ["type" ] is FlowResultType .ABORT
277275 assert result ["reason" ] == "already_configured"
278- assert len ( mock_get_data . mock_calls ) == 0
276+ mock_brother_client . async_update . assert_not_called ()
279277
280278
281279async def test_zeroconf_confirm_create_entry (
@@ -315,6 +313,7 @@ async def test_zeroconf_confirm_create_entry(
315313 assert result ["data" ][CONF_TYPE ] == "laser"
316314 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_PORT ] == 161
317315 assert result ["data" ][SECTION_ADVANCED_SETTINGS ][CONF_COMMUNITY ] == "public"
316+ assert result ["result" ].unique_id == "0123456789"
318317
319318
320319async def test_reconfigure_successful (
0 commit comments