Use reauth_confirm in ovo_energy (#129306)

This commit is contained in:
epenet 2024-10-28 11:52:38 +01:00 committed by GitHub
parent 1d23adcda3
commit e5b25bfa58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 36 deletions

View File

@ -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
)

View File

@ -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%]"
},

View File

@ -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 (