Abort ring config_flow if account is already configured (#125120)

* Abort ring config_flow if account is already configured

* Update tests/components/ring/test_config_flow.py

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/125151/head
Steven B. 2024-09-03 14:22:38 +01:00 committed by GitHub
parent 6cea6be4a7
commit eda1656e75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 2 deletions

View File

@ -65,6 +65,8 @@ class RingConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
await self.async_set_unique_id(user_input[CONF_USERNAME])
self._abort_if_unique_id_configured()
try:
token = await validate_input(self.hass, user_input)
except Require2FA:
@ -77,7 +79,6 @@ class RingConfigFlow(ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(user_input[CONF_USERNAME])
return self.async_create_entry(
title=user_input[CONF_USERNAME],
data={CONF_USERNAME: user_input[CONF_USERNAME], CONF_TOKEN: token},

View File

@ -27,7 +27,7 @@
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
}
},

View File

@ -220,3 +220,25 @@ async def test_reauth_error(
"token": "new-foobar",
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_account_configured(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_added_config_entry: Mock,
) -> None:
"""Test that user cannot configure the same account twice."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"username": "foo@bar.com", "password": "test-password"},
)
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "already_configured"