Skip to content

Commit 8ae52cd

Browse files
authored
Fix shelly not being able to be setup from user flow when already discovered (#138807)
raise_on_progress=False was missing in the user flow which made it impossible to configure a shelly by IP when there was an active discovery because the flow would abort
1 parent 13fe2a9 commit 8ae52cd

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

homeassistant/components/shelly/config_flow.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ async def async_step_user(
164164
LOGGER.exception("Unexpected exception")
165165
errors["base"] = "unknown"
166166
else:
167-
await self.async_set_unique_id(self.info[CONF_MAC])
167+
await self.async_set_unique_id(
168+
self.info[CONF_MAC], raise_on_progress=False
169+
)
168170
self._abort_if_unique_id_configured({CONF_HOST: host})
169171
self.host = host
170172
self.port = port

tests/components/shelly/test_config_flow.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
120187
async def test_form_gen1_custom_port(
121188
hass: HomeAssistant,
122189
mock_block_device: Mock,

0 commit comments

Comments
 (0)