Skip to content

Commit 6661218

Browse files
authored
Add device reconfigure to Vodafone Station config flow (#141221)
* Add device reconfigure to Vodafone Station config flow * remove unreachable code * apply review comment
1 parent 8904f17 commit 6661218

File tree

4 files changed

+127
-3
lines changed

4 files changed

+127
-3
lines changed

homeassistant/components/vodafone_station/config_flow.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,47 @@ async def async_step_reauth_confirm(
139139
errors=errors,
140140
)
141141

142+
async def async_step_reconfigure(
143+
self, user_input: dict[str, Any] | None = None
144+
) -> ConfigFlowResult:
145+
"""Handle reconfiguration of the device."""
146+
reconfigure_entry = self._get_reconfigure_entry()
147+
if not user_input:
148+
return self.async_show_form(
149+
step_id="reconfigure", data_schema=user_form_schema(user_input)
150+
)
151+
152+
updated_host = user_input[CONF_HOST]
153+
154+
if reconfigure_entry.data[CONF_HOST] != updated_host:
155+
self._async_abort_entries_match({CONF_HOST: updated_host})
156+
157+
errors: dict[str, str] = {}
158+
159+
errors = {}
160+
161+
try:
162+
await validate_input(self.hass, user_input)
163+
except aiovodafone_exceptions.AlreadyLogged:
164+
errors["base"] = "already_logged"
165+
except aiovodafone_exceptions.CannotConnect:
166+
errors["base"] = "cannot_connect"
167+
except aiovodafone_exceptions.CannotAuthenticate:
168+
errors["base"] = "invalid_auth"
169+
except Exception: # noqa: BLE001
170+
_LOGGER.exception("Unexpected exception")
171+
errors["base"] = "unknown"
172+
else:
173+
return self.async_update_reload_and_abort(
174+
reconfigure_entry, data_updates={CONF_HOST: updated_host}
175+
)
176+
177+
return self.async_show_form(
178+
step_id="reconfigure",
179+
data_schema=user_form_schema(user_input),
180+
errors=errors,
181+
)
182+
142183

143184
class VodafoneStationOptionsFlowHandler(OptionsFlow):
144185
"""Handle a option flow."""

homeassistant/components/vodafone_station/quality_scale.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ rules:
6464
entity-translations: done
6565
exception-translations: done
6666
icon-translations: done
67-
reconfiguration-flow:
68-
status: todo
69-
comment: handle host change
67+
reconfiguration-flow: done
7068
repair-issues:
7169
status: exempt
7270
comment: no known use cases for repair issues or flows, yet

homeassistant/components/vodafone_station/strings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,25 @@
2121
"username": "The username for your Vodafone Station.",
2222
"password": "The password for your Vodafone Station."
2323
}
24+
},
25+
"reconfigure": {
26+
"data": {
27+
"host": "[%key:common::config_flow::data::host%]",
28+
"username": "[%key:common::config_flow::data::username%]",
29+
"password": "[%key:common::config_flow::data::password%]"
30+
},
31+
"data_description": {
32+
"host": "[%key:component::vodafone_station::config::step::user::data_description::host%]",
33+
"username": "[%key:component::vodafone_station::config::step::user::data_description::username%]",
34+
"password": "[%key:component::vodafone_station::config::step::user::data_description::password%]"
35+
}
2436
}
2537
},
2638
"abort": {
2739
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
2840
"already_logged": "User already logged-in, please try again later.",
2941
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
42+
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
3043
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
3144
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
3245
"model_not_supported": "The device model is currently unsupported.",

tests/components/vodafone_station/test_config_flow.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,75 @@ async def test_options_flow(
228228
assert result["data"] == {
229229
CONF_CONSIDER_HOME: 37,
230230
}
231+
232+
233+
async def test_reconfigure_successful(
234+
hass: HomeAssistant,
235+
mock_vodafone_station_router: AsyncMock,
236+
mock_setup_entry: AsyncMock,
237+
mock_config_entry: MockConfigEntry,
238+
) -> None:
239+
"""Test that the host can be reconfigured."""
240+
mock_config_entry.add_to_hass(hass)
241+
result = await mock_config_entry.start_reconfigure_flow(hass)
242+
243+
assert result["type"] is FlowResultType.FORM
244+
assert result["step_id"] == "reconfigure"
245+
246+
# original entry
247+
assert mock_config_entry.data["host"] == "fake_host"
248+
249+
reconfigure_result = await hass.config_entries.flow.async_configure(
250+
result["flow_id"],
251+
{
252+
"host": "192.168.100.60",
253+
"password": "fake_password",
254+
"username": "fake_username",
255+
},
256+
)
257+
258+
assert reconfigure_result["type"] is FlowResultType.ABORT
259+
assert reconfigure_result["reason"] == "reconfigure_successful"
260+
261+
# changed entry
262+
assert mock_config_entry.data["host"] == "192.168.100.60"
263+
264+
265+
@pytest.mark.parametrize(
266+
("side_effect", "error"),
267+
[
268+
(CannotConnect, "cannot_connect"),
269+
(CannotAuthenticate, "invalid_auth"),
270+
(AlreadyLogged, "already_logged"),
271+
(ConnectionResetError, "unknown"),
272+
],
273+
)
274+
async def test_reconfigure_fails(
275+
hass: HomeAssistant,
276+
mock_vodafone_station_router: AsyncMock,
277+
mock_setup_entry: AsyncMock,
278+
mock_config_entry: MockConfigEntry,
279+
side_effect: Exception,
280+
error: str,
281+
) -> None:
282+
"""Test that the host can be reconfigured."""
283+
mock_config_entry.add_to_hass(hass)
284+
result = await mock_config_entry.start_reconfigure_flow(hass)
285+
286+
assert result["type"] is FlowResultType.FORM
287+
assert result["step_id"] == "reconfigure"
288+
289+
mock_vodafone_station_router.login.side_effect = side_effect
290+
291+
reconfigure_result = await hass.config_entries.flow.async_configure(
292+
result["flow_id"],
293+
{
294+
"host": "192.168.100.60",
295+
"password": "fake_password",
296+
"username": "fake_username",
297+
},
298+
)
299+
300+
assert reconfigure_result["type"] is FlowResultType.FORM
301+
assert reconfigure_result["step_id"] == "reconfigure"
302+
assert reconfigure_result["errors"] == {"base": error}

0 commit comments

Comments
 (0)