From e5b25bfa582efb3360c5174ee179c2808f718f80 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:52:38 +0100 Subject: [PATCH] Use reauth_confirm in ovo_energy (#129306) --- .../components/ovo_energy/config_flow.py | 38 +++++++++---------- .../components/ovo_energy/strings.json | 2 +- .../components/ovo_energy/test_config_flow.py | 38 +++++++++++-------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/ovo_energy/config_flow.py b/homeassistant/components/ovo_energy/config_flow.py index 60a2870ef59..53fc4f8eff6 100644 --- a/homeassistant/components/ovo_energy/config_flow.py +++ b/homeassistant/components/ovo_energy/config_flow.py @@ -79,22 +79,26 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): async def async_step_reauth( self, - user_input: Mapping[str, Any], + entry_data: Mapping[str, Any], ) -> ConfigFlowResult: """Handle configuration by re-auth.""" - errors = {} - - if user_input and user_input.get(CONF_USERNAME): - self.username = user_input[CONF_USERNAME] - - if user_input and user_input.get(CONF_ACCOUNT): - self.account = user_input[CONF_ACCOUNT] + self.username = entry_data.get(CONF_USERNAME) + self.account = entry_data.get(CONF_ACCOUNT) if self.username: # If we have a username, use it as flow title self.context["title_placeholders"] = {CONF_USERNAME: self.username} - if user_input is not None and user_input.get(CONF_PASSWORD) is not None: + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, + user_input: Mapping[str, Any] | None = None, + ) -> ConfigFlowResult: + """Handle configuration by re-auth.""" + errors = {} + + if user_input is not None: client = OVOEnergy( client_session=async_get_clientsession(self.hass), ) @@ -111,19 +115,13 @@ class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN): errors["base"] = "connection_error" else: if authenticated: - entry = await self.async_set_unique_id(self.username) - if entry: - self.hass.config_entries.async_update_entry( - entry, - data={ - CONF_USERNAME: self.username, - CONF_PASSWORD: user_input[CONF_PASSWORD], - }, - ) - return self.async_abort(reason="reauth_successful") + return self.async_update_reload_and_abort( + self._get_reauth_entry(), + data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]}, + ) errors["base"] = "authorization_error" return self.async_show_form( - step_id="reauth", data_schema=REAUTH_SCHEMA, errors=errors + step_id="reauth_confirm", data_schema=REAUTH_SCHEMA, errors=errors ) diff --git a/homeassistant/components/ovo_energy/strings.json b/homeassistant/components/ovo_energy/strings.json index fda0c2996dc..a9f7c9056b7 100644 --- a/homeassistant/components/ovo_energy/strings.json +++ b/homeassistant/components/ovo_energy/strings.json @@ -16,7 +16,7 @@ "description": "Set up an OVO Energy instance to access your energy usage.", "title": "Add OVO Energy Account" }, - "reauth": { + "reauth_confirm": { "data": { "password": "[%key:common::config_flow::data::password%]" }, diff --git a/tests/components/ovo_energy/test_config_flow.py b/tests/components/ovo_energy/test_config_flow.py index f21672679bd..b6250a95492 100644 --- a/tests/components/ovo_energy/test_config_flow.py +++ b/tests/components/ovo_energy/test_config_flow.py @@ -131,15 +131,14 @@ async def test_reauth_authorization_error(hass: HomeAssistant) -> None: domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT ) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", return_value=False, ): - result = await mock_config.start_reauth_flow(hass) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" - result2 = await hass.config_entries.flow.async_configure( result["flow_id"], FIXTURE_REAUTH_INPUT, @@ -147,7 +146,7 @@ async def test_reauth_authorization_error(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "reauth" + assert result2["step_id"] == "reauth_confirm" assert result2["errors"] == {"base": "authorization_error"} @@ -161,15 +160,16 @@ async def test_reauth_connection_error(hass: HomeAssistant) -> None: domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT ) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + assert result["errors"] == {} + with patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", side_effect=aiohttp.ClientError, ): - result = await mock_config.start_reauth_flow(hass) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" - result2 = await hass.config_entries.flow.async_configure( result["flow_id"], FIXTURE_REAUTH_INPUT, @@ -177,7 +177,7 @@ async def test_reauth_connection_error(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "reauth" + assert result2["step_id"] == "reauth_confirm" assert result2["errors"] == {"base": "connection_error"} @@ -196,14 +196,22 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT ) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + assert result["errors"] == {} + with patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", return_value=False, ): - result = await mock_config.start_reauth_flow(hass) - + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + FIXTURE_REAUTH_INPUT, + ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" + assert result["step_id"] == "reauth_confirm" assert result["errors"] == {"base": "authorization_error"} with (