1 """Config flow to configure the LCN integration."""
3 from __future__
import annotations
9 import voluptuous
as vol
11 from homeassistant
import config_entries
26 from .
import PchkConnectionManager
27 from .const
import CONF_ACKNOWLEDGE, CONF_DIM_MODE, CONF_SK_NUM_TRIES, DIM_MODES, DOMAIN
29 _LOGGER = logging.getLogger(__name__)
32 vol.Required(CONF_IP_ADDRESS, default=
""): str,
33 vol.Required(CONF_PORT, default=4114): cv.positive_int,
34 vol.Required(CONF_USERNAME, default=
""): str,
35 vol.Required(CONF_PASSWORD, default=
""): str,
36 vol.Required(CONF_SK_NUM_TRIES, default=0): cv.positive_int,
37 vol.Required(CONF_DIM_MODE, default=
"STEPS200"): vol.In(DIM_MODES),
38 vol.Required(CONF_ACKNOWLEDGE, default=
False): cv.boolean,
41 USER_DATA = {vol.Required(CONF_HOST, default=
"pchk"): str, **CONFIG_DATA}
43 CONFIG_SCHEMA = vol.Schema(CONFIG_DATA)
44 USER_SCHEMA = vol.Schema(USER_DATA)
48 hass: HomeAssistant, data: ConfigType
50 """Check config entries for already configured entries based on the ip address/port."""
54 for entry
in hass.config_entries.async_entries(DOMAIN)
55 if entry.data[CONF_IP_ADDRESS] == data[CONF_IP_ADDRESS]
56 and entry.data[CONF_PORT] == data[CONF_PORT]
63 """Validate if a connection to LCN can be established."""
65 host_name = data[CONF_HOST]
66 host = data[CONF_IP_ADDRESS]
67 port = data[CONF_PORT]
68 username = data[CONF_USERNAME]
69 password = data[CONF_PASSWORD]
70 sk_num_tries = data[CONF_SK_NUM_TRIES]
71 dim_mode = data[CONF_DIM_MODE]
72 acknowledge = data[CONF_ACKNOWLEDGE]
75 "SK_NUM_TRIES": sk_num_tries,
76 "DIM_MODE": pypck.lcn_defs.OutputPortDimMode[dim_mode],
77 "ACKNOWLEDGE": acknowledge,
80 _LOGGER.debug(
"Validating connection parameters to PCHK host '%s'", host_name)
82 connection = PchkConnectionManager(
83 host, port, username, password, settings=settings
87 await connection.async_connect(timeout=5)
88 _LOGGER.debug(
"LCN connection validated")
89 except pypck.connection.PchkAuthenticationError:
90 _LOGGER.warning(
'Authentication on PCHK "%s" failed', host_name)
91 error =
"authentication_error"
92 except pypck.connection.PchkLicenseError:
94 'Maximum number of connections on PCHK "%s" was '
95 "reached. An additional license key is required",
98 error =
"license_error"
99 except (TimeoutError, ConnectionRefusedError):
100 _LOGGER.warning(
'Connection to PCHK "%s" failed', host_name)
101 error =
"connection_refused"
103 await connection.async_close()
108 """Handle a LCN config flow."""
114 self, user_input: dict[str, Any] |
None =
None
116 """Handle a flow initiated by the user."""
117 if user_input
is None:
122 errors = {CONF_BASE:
"already_configured"}
124 errors = {CONF_BASE: error}
126 if errors
is not None:
130 USER_SCHEMA, user_input
144 self, user_input: dict[str, Any] |
None =
None
146 """Reconfigure LCN configuration."""
149 if user_input
is not None:
150 user_input[CONF_HOST] = reconfigure_entry.data[CONF_HOST]
152 await self.hass.config_entries.async_unload(reconfigure_entry.entry_id)
154 errors = {CONF_BASE: error}
158 reconfigure_entry, data_updates=user_input
161 await self.hass.config_entries.async_setup(reconfigure_entry.entry_id)
164 step_id=
"reconfigure",
166 CONFIG_SCHEMA, reconfigure_entry.data
config_entries.ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
config_entries.ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigEntry _get_reconfigure_entry(self)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
str|None validate_connection(ConfigType data)
config_entries.ConfigEntry|None get_config_entry(HomeAssistant hass, ConfigType data)