diff --git a/homeassistant/components/upcloud/config_flow.py b/homeassistant/components/upcloud/config_flow.py index e6868be29b9..fda6c1d561b 100644 --- a/homeassistant/components/upcloud/config_flow.py +++ b/homeassistant/components/upcloud/config_flow.py @@ -55,6 +55,9 @@ class UpCloudConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): step_id="user", user_input=user_input, errors=errors ) + self._abort_if_unique_id_configured( + updates={CONF_PASSWORD: user_input[CONF_PASSWORD]} + ) return self.async_create_entry(title=user_input[CONF_USERNAME], data=user_input) @callback diff --git a/homeassistant/components/upcloud/strings.json b/homeassistant/components/upcloud/strings.json index 3daf70be598..fc52916e9b4 100644 --- a/homeassistant/components/upcloud/strings.json +++ b/homeassistant/components/upcloud/strings.json @@ -11,6 +11,9 @@ "password": "[%key:common::config_flow::data::password%]" } } + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_service%]" } }, "options": { diff --git a/tests/components/upcloud/test_config_flow.py b/tests/components/upcloud/test_config_flow.py index 5a52bb62c27..eadbe1c8fe6 100644 --- a/tests/components/upcloud/test_config_flow.py +++ b/tests/components/upcloud/test_config_flow.py @@ -72,7 +72,8 @@ async def test_success( hass: HomeAssistant, requests_mock: requests_mock.Mocker ) -> None: """Test successful flow provides entry creation data.""" - requests_mock.request(ANY, ANY, text='{"account":{"username":"user"}}') + requests_mock.request(ANY, "/1.3/account", text='{"account":{"username":"user"}}') + requests_mock.request(ANY, "/1.3/server", text='{"servers": {"server":[]}}') result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, data=FIXTURE_USER_INPUT ) @@ -105,3 +106,28 @@ async def test_options(hass: HomeAssistant) -> None: assert result["data"][CONF_SCAN_INTERVAL] == int( FIXTURE_USER_INPUT_OPTIONS[CONF_SCAN_INTERVAL] ) + + +async def test_already_configured(hass, requests_mock): + """Test duplicate entry aborts and updates data.""" + + config_entry = MockConfigEntry( + domain=DOMAIN, + unique_id=FIXTURE_USER_INPUT[CONF_USERNAME], + data=FIXTURE_USER_INPUT, + options=FIXTURE_USER_INPUT_OPTIONS, + ) + config_entry.add_to_hass(hass) + + new_user_input = FIXTURE_USER_INPUT.copy() + new_user_input[CONF_PASSWORD] += "_changed" + + requests_mock.request(ANY, "/1.3/account", text='{"account":{"username":"user"}}') + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER}, data=new_user_input + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" + assert config_entry.data[CONF_USERNAME] == new_user_input[CONF_USERNAME] + assert config_entry.data[CONF_PASSWORD] == new_user_input[CONF_PASSWORD]