Add username to Reauth flow in Honeywell (#96850)

* pre-populate username/password on reauth

* Update homeassistant/components/honeywell/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Use add_suggested_value_to_schema

* Optimize code

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
mkmer 2023-07-19 09:25:10 -04:00 committed by GitHub
parent 06aeacc324
commit 3b501fd2d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 16 deletions

View file

@ -22,7 +22,12 @@ from .const import (
DOMAIN,
)
REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
REAUTH_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
)
class HoneywellConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
@ -42,18 +47,12 @@ class HoneywellConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) -> FlowResult:
"""Confirm re-authentication with Honeywell."""
errors: dict[str, str] = {}
assert self.entry is not None
if user_input:
assert self.entry is not None
password = user_input[CONF_PASSWORD]
data = {
CONF_USERNAME: self.entry.data[CONF_USERNAME],
CONF_PASSWORD: password,
}
try:
await self.is_valid(
username=data[CONF_USERNAME], password=data[CONF_PASSWORD]
username=user_input[CONF_USERNAME],
password=user_input[CONF_PASSWORD],
)
except aiosomecomfort.AuthError:
@ -71,7 +70,7 @@ class HoneywellConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self.entry,
data={
**self.entry.data,
CONF_PASSWORD: password,
**user_input,
},
)
await self.hass.config_entries.async_reload(self.entry.entry_id)
@ -79,7 +78,9 @@ class HoneywellConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return self.async_show_form(
step_id="reauth_confirm",
data_schema=REAUTH_SCHEMA,
data_schema=self.add_suggested_values_to_schema(
REAUTH_SCHEMA, self.entry.data
),
errors=errors,
)

View file

@ -156,14 +156,14 @@ async def test_reauth_flow(hass: HomeAssistant) -> None:
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password"},
{CONF_USERNAME: "new-username", CONF_PASSWORD: "new-password"},
)
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.ABORT
assert result2["reason"] == "reauth_successful"
assert mock_entry.data == {
CONF_USERNAME: "test-username",
CONF_USERNAME: "new-username",
CONF_PASSWORD: "new-password",
}
@ -200,7 +200,7 @@ async def test_reauth_flow_auth_error(hass: HomeAssistant, client: MagicMock) ->
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password"},
{CONF_USERNAME: "new-username", CONF_PASSWORD: "new-password"},
)
await hass.async_block_till_done()
@ -246,7 +246,7 @@ async def test_reauth_flow_connnection_error(
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password"},
{CONF_USERNAME: "new-username", CONF_PASSWORD: "new-password"},
)
await hass.async_block_till_done()