@@ -117,6 +117,73 @@ async def test_form(
117117 assert len (mock_setup_entry .mock_calls ) == 1
118118
119119
120+ async def test_user_flow_overrides_existing_discovery (
121+ hass : HomeAssistant ,
122+ mock_rpc_device : Mock ,
123+ ) -> None :
124+ """Test setting up from the user flow when the devices is already discovered."""
125+ with (
126+ patch (
127+ "homeassistant.components.shelly.config_flow.get_info" ,
128+ return_value = {
129+ "mac" : "AABBCCDDEEFF" ,
130+ "model" : MODEL_PLUS_2PM ,
131+ "auth" : False ,
132+ "gen" : 2 ,
133+ "port" : 80 ,
134+ },
135+ ),
136+ patch (
137+ "homeassistant.components.shelly.async_setup" , return_value = True
138+ ) as mock_setup ,
139+ patch (
140+ "homeassistant.components.shelly.async_setup_entry" ,
141+ return_value = True ,
142+ ) as mock_setup_entry ,
143+ ):
144+ discovery_result = await hass .config_entries .flow .async_init (
145+ DOMAIN ,
146+ data = ZeroconfServiceInfo (
147+ ip_address = ip_address ("1.1.1.1" ),
148+ ip_addresses = [ip_address ("1.1.1.1" )],
149+ hostname = "mock_hostname" ,
150+ name = "shelly2pm-aabbccddeeff" ,
151+ port = None ,
152+ properties = {ATTR_PROPERTIES_ID : "shelly2pm-aabbccddeeff" },
153+ type = "mock_type" ,
154+ ),
155+ context = {"source" : config_entries .SOURCE_ZEROCONF },
156+ )
157+ assert discovery_result ["type" ] is FlowResultType .FORM
158+
159+ result = await hass .config_entries .flow .async_init (
160+ DOMAIN , context = {"source" : config_entries .SOURCE_USER }
161+ )
162+ assert result ["type" ] is FlowResultType .FORM
163+ assert result ["errors" ] == {}
164+ result2 = await hass .config_entries .flow .async_configure (
165+ result ["flow_id" ],
166+ {"host" : "1.1.1.1" , "port" : 80 },
167+ )
168+ await hass .async_block_till_done ()
169+
170+ assert result2 ["type" ] is FlowResultType .CREATE_ENTRY
171+ assert result2 ["title" ] == "Test name"
172+ assert result2 ["data" ] == {
173+ "host" : "1.1.1.1" ,
174+ "port" : 80 ,
175+ "model" : MODEL_PLUS_2PM ,
176+ "sleep_period" : 0 ,
177+ "gen" : 2 ,
178+ }
179+ assert result2 ["context" ]["unique_id" ] == "AABBCCDDEEFF"
180+ assert len (mock_setup .mock_calls ) == 1
181+ assert len (mock_setup_entry .mock_calls ) == 1
182+ assert len (hass .config_entries .async_entries (DOMAIN )) == 1
183+ # discovery flow should have been aborted
184+ assert not hass .config_entries .flow .async_progress (DOMAIN )
185+
186+
120187async def test_form_gen1_custom_port (
121188 hass : HomeAssistant ,
122189 mock_block_device : Mock ,
0 commit comments