mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 00:27:19 +00:00
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 combination
This commit is contained in:
parent
d2ef3ca100
commit
c022c32d2f
@ -19,7 +19,6 @@ from homeassistant.const import (
|
|||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
@ -44,21 +43,6 @@ CONFIG_SCHEMA = vol.Schema(CONFIG_DATA)
|
|||||||
USER_SCHEMA = vol.Schema(USER_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:
|
async def validate_connection(data: ConfigType) -> str | None:
|
||||||
"""Validate if a connection to LCN can be established."""
|
"""Validate if a connection to LCN can be established."""
|
||||||
error = None
|
error = None
|
||||||
@ -120,19 +104,20 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is None:
|
if user_input is None:
|
||||||
return self.async_show_form(step_id="user", data_schema=USER_SCHEMA)
|
return self.async_show_form(step_id="user", data_schema=USER_SCHEMA)
|
||||||
|
|
||||||
errors = None
|
self._async_abort_entries_match(
|
||||||
if get_config_entry(self.hass, user_input):
|
{
|
||||||
errors = {CONF_BASE: "already_configured"}
|
CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS],
|
||||||
elif (error := await validate_connection(user_input)) is not None:
|
CONF_PORT: user_input[CONF_PORT],
|
||||||
errors = {CONF_BASE: error}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
if errors is not None:
|
if (error := await validate_connection(user_input)) is not None:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=self.add_suggested_values_to_schema(
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
USER_SCHEMA, user_input
|
USER_SCHEMA, user_input
|
||||||
),
|
),
|
||||||
errors=errors,
|
errors={CONF_BASE: error},
|
||||||
)
|
)
|
||||||
|
|
||||||
data: dict = {
|
data: dict = {
|
||||||
@ -152,15 +137,21 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
user_input[CONF_HOST] = reconfigure_entry.data[CONF_HOST]
|
user_input[CONF_HOST] = reconfigure_entry.data[CONF_HOST]
|
||||||
|
|
||||||
await self.hass.config_entries.async_unload(reconfigure_entry.entry_id)
|
self._async_abort_entries_match(
|
||||||
if (error := await validate_connection(user_input)) is not None:
|
{
|
||||||
errors = {CONF_BASE: error}
|
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(
|
return self.async_update_reload_and_abort(
|
||||||
reconfigure_entry, data_updates=user_input
|
reconfigure_entry, data_updates=user_input
|
||||||
)
|
)
|
||||||
|
|
||||||
|
errors = {CONF_BASE: error}
|
||||||
await self.hass.config_entries.async_setup(reconfigure_entry.entry_id)
|
await self.hass.config_entries.async_setup(reconfigure_entry.entry_id)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
@ -66,11 +66,11 @@
|
|||||||
"error": {
|
"error": {
|
||||||
"authentication_error": "Authentication failed. Wrong username or password.",
|
"authentication_error": "Authentication failed. Wrong username or password.",
|
||||||
"license_error": "Maximum number of connections was reached. An additional licence key is required.",
|
"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.",
|
"connection_refused": "Unable to connect to PCHK. Check IP and port."
|
||||||
"already_configured": "PCHK connection using the same ip address/port is already configured."
|
|
||||||
},
|
},
|
||||||
"abort": {
|
"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": {
|
"issues": {
|
||||||
|
@ -94,8 +94,8 @@ async def test_step_user_existing_host(
|
|||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config_data
|
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config_data
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||||
assert result["errors"] == {CONF_BASE: "already_configured"}
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user