diff --git a/homeassistant/components/lcn/config_flow.py b/homeassistant/components/lcn/config_flow.py index 946c7ac3724..62a9920fb73 100644 --- a/homeassistant/components/lcn/config_flow.py +++ b/homeassistant/components/lcn/config_flow.py @@ -19,7 +19,6 @@ from homeassistant.const import ( CONF_PORT, CONF_USERNAME, ) -from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType @@ -44,21 +43,6 @@ CONFIG_SCHEMA = vol.Schema(CONFIG_DATA) USER_SCHEMA = vol.Schema(USER_DATA) -def get_config_entry( - hass: HomeAssistant, data: ConfigType -) -> config_entries.ConfigEntry | None: - """Check config entries for already configured entries based on the ip address/port.""" - return next( - ( - entry - for entry in hass.config_entries.async_entries(DOMAIN) - if entry.data[CONF_IP_ADDRESS] == data[CONF_IP_ADDRESS] - and entry.data[CONF_PORT] == data[CONF_PORT] - ), - None, - ) - - async def validate_connection(data: ConfigType) -> str | None: """Validate if a connection to LCN can be established.""" error = None @@ -120,19 +104,20 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if user_input is None: return self.async_show_form(step_id="user", data_schema=USER_SCHEMA) - errors = None - if get_config_entry(self.hass, user_input): - errors = {CONF_BASE: "already_configured"} - elif (error := await validate_connection(user_input)) is not None: - errors = {CONF_BASE: error} + self._async_abort_entries_match( + { + CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS], + CONF_PORT: user_input[CONF_PORT], + } + ) - if errors is not None: + if (error := await validate_connection(user_input)) is not None: return self.async_show_form( step_id="user", data_schema=self.add_suggested_values_to_schema( USER_SCHEMA, user_input ), - errors=errors, + errors={CONF_BASE: error}, ) data: dict = { @@ -152,15 +137,21 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: user_input[CONF_HOST] = reconfigure_entry.data[CONF_HOST] - await self.hass.config_entries.async_unload(reconfigure_entry.entry_id) - if (error := await validate_connection(user_input)) is not None: - errors = {CONF_BASE: error} + self._async_abort_entries_match( + { + CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS], + CONF_PORT: user_input[CONF_PORT], + } + ) - if errors is None: + await self.hass.config_entries.async_unload(reconfigure_entry.entry_id) + + if (error := await validate_connection(user_input)) is None: return self.async_update_reload_and_abort( reconfigure_entry, data_updates=user_input ) + errors = {CONF_BASE: error} await self.hass.config_entries.async_setup(reconfigure_entry.entry_id) return self.async_show_form( diff --git a/homeassistant/components/lcn/strings.json b/homeassistant/components/lcn/strings.json index 4295ceb384d..9d806bce104 100644 --- a/homeassistant/components/lcn/strings.json +++ b/homeassistant/components/lcn/strings.json @@ -66,11 +66,11 @@ "error": { "authentication_error": "Authentication failed. Wrong username or password.", "license_error": "Maximum number of connections was reached. An additional licence key is required.", - "connection_refused": "Unable to connect to PCHK. Check IP and port.", - "already_configured": "PCHK connection using the same ip address/port is already configured." + "connection_refused": "Unable to connect to PCHK. Check IP and port." }, "abort": { - "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "already_configured": "PCHK connection using the same ip address/port is already configured." } }, "issues": { diff --git a/tests/components/lcn/test_config_flow.py b/tests/components/lcn/test_config_flow.py index 478f2c0949e..ef99a19dee4 100644 --- a/tests/components/lcn/test_config_flow.py +++ b/tests/components/lcn/test_config_flow.py @@ -94,8 +94,8 @@ async def test_step_user_existing_host( DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config_data ) - assert result["type"] == data_entry_flow.FlowResultType.FORM - assert result["errors"] == {CONF_BASE: "already_configured"} + assert result["type"] == data_entry_flow.FlowResultType.ABORT + assert result["reason"] == "already_configured" @pytest.mark.parametrize(