diff --git a/homeassistant/components/vitesy/config_flow.py b/homeassistant/components/vitesy/config_flow.py index afe218051927..48674f2680a3 100644 --- a/homeassistant/components/vitesy/config_flow.py +++ b/homeassistant/components/vitesy/config_flow.py @@ -88,6 +88,33 @@ class VitesyConfigFlow(ConfigFlow, domain=DOMAIN): step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors ) + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Change the credentials of the configured account.""" + errors: dict[str, str] = {} + reconfigure_entry = self._get_reconfigure_entry() + if user_input is not None: + email = user_input[CONF_EMAIL] + user_id = await self._async_validate_input( + email, user_input[CONF_PASSWORD], errors + ) + if user_id is not None: + await self.async_set_unique_id(user_id) + self._abort_if_unique_id_mismatch(reason="reconfigure_account_mismatch") + return self.async_update_reload_and_abort( + reconfigure_entry, title=email, data_updates=user_input + ) + + return self.async_show_form( + step_id="reconfigure", + data_schema=self.add_suggested_values_to_schema( + STEP_USER_DATA_SCHEMA, + {CONF_EMAIL: reconfigure_entry.data[CONF_EMAIL]}, + ), + errors=errors, + ) + async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: diff --git a/homeassistant/components/vitesy/quality_scale.yaml b/homeassistant/components/vitesy/quality_scale.yaml index 8efe1b1e2e67..28b147c62640 100644 --- a/homeassistant/components/vitesy/quality_scale.yaml +++ b/homeassistant/components/vitesy/quality_scale.yaml @@ -64,7 +64,7 @@ rules: entity-translations: done exception-translations: done icon-translations: done - reconfiguration-flow: todo + reconfiguration-flow: done repair-issues: status: exempt comment: The integration has no repairable issues. diff --git a/homeassistant/components/vitesy/strings.json b/homeassistant/components/vitesy/strings.json index 13008caa25bd..2975e769fa31 100644 --- a/homeassistant/components/vitesy/strings.json +++ b/homeassistant/components/vitesy/strings.json @@ -2,6 +2,7 @@ "config": { "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", + "reconfigure_account_mismatch": "The credentials belong to a different Vitesy Hub account than the one originally configured.", "unique_id_mismatch": "The credentials belong to a different Vitesy Hub account. Please reauthenticate with the account originally configured." }, "error": { @@ -19,6 +20,17 @@ "description": "The password for {email} is no longer valid. Enter the current password of your Vitesy Hub account.", "title": "[%key:common::config_flow::title::reauth%]" }, + "reconfigure": { + "data": { + "email": "[%key:common::config_flow::data::email%]", + "password": "[%key:common::config_flow::data::password%]" + }, + "data_description": { + "email": "[%key:component::vitesy::config::step::user::data_description::email%]", + "password": "[%key:component::vitesy::config::step::user::data_description::password%]" + }, + "description": "Update the credentials of your Vitesy Hub account." + }, "user": { "data": { "email": "[%key:common::config_flow::data::email%]", diff --git a/tests/components/vitesy/test_config_flow.py b/tests/components/vitesy/test_config_flow.py index bbe1b111de1b..b8727756066d 100644 --- a/tests/components/vitesy/test_config_flow.py +++ b/tests/components/vitesy/test_config_flow.py @@ -184,3 +184,80 @@ async def test_reauth_unique_id_mismatch( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "unique_id_mismatch" assert mock_config_entry.data[CONF_PASSWORD] == "hunter2" + + +async def test_reconfigure_flow( + hass: HomeAssistant, + mock_vitesy_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reconfiguration stores the new credentials.""" + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reconfigure_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_EMAIL: "new@example.com", CONF_PASSWORD: "new-password"}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert mock_config_entry.data == { + CONF_EMAIL: "new@example.com", + CONF_PASSWORD: "new-password", + } + assert mock_config_entry.title == "new@example.com" + + +@pytest.mark.parametrize( + ("side_effect", "reason"), + [ + pytest.param(CannotAuthenticate, "invalid_auth", id="invalid_auth"), + pytest.param(CannotConnect, "cannot_connect", id="cannot_connect"), + pytest.param(GenericResponseError, "cannot_connect", id="generic_error"), + ], +) +async def test_reconfigure_errors_then_recovers( + hass: HomeAssistant, + mock_vitesy_client: AsyncMock, + mock_config_entry: MockConfigEntry, + side_effect: type[Exception], + reason: str, +) -> None: + """Test each reconfigure failure surfaces an error and the flow can recover.""" + mock_config_entry.add_to_hass(hass) + mock_vitesy_client.login.side_effect = side_effect + result = await mock_config_entry.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_EMAIL: EMAIL, CONF_PASSWORD: "new-password"} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": reason} + + mock_vitesy_client.login.side_effect = None + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_EMAIL: EMAIL, CONF_PASSWORD: "new-password"} + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert mock_config_entry.data[CONF_PASSWORD] == "new-password" + + +async def test_reconfigure_account_mismatch( + hass: HomeAssistant, + mock_vitesy_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reconfigure aborts when the credentials belong to another account.""" + mock_config_entry.add_to_hass(hass) + mock_vitesy_client.get_user.return_value = {"id": "other-user"} + result = await mock_config_entry.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_EMAIL: "other@example.com", CONF_PASSWORD: "pw"} + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_account_mismatch" + assert mock_config_entry.data == {CONF_EMAIL: EMAIL, CONF_PASSWORD: "hunter2"}