Simplify unique config_entry check for LCN (#135756)
* Simplify check for unique config_entry * Fix tests * Fix reconfigure flow * Add check for unchanging IP/port combination * Remove explicit check for unchanged IP/port combinationpull/144755/head
parent
d2ef3ca100
commit
c022c32d2f
|
@ -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(
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue