1 """Config flow for Elexa Guardian integration."""
3 from __future__
import annotations
7 from aioguardian
import Client
8 from aioguardian.errors
import GuardianError
9 import voluptuous
as vol
16 from .const
import CONF_UID, DOMAIN, LOGGER
20 DATA_SCHEMA = vol.Schema(
22 vol.Required(CONF_IP_ADDRESS): str,
23 vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
27 UNIQUE_ID =
"guardian_{0}"
32 """Get the device's 4-digit PIN from its zeroconf-discovered hostname."""
33 return hostname.split(
".")[0].split(
"-")[1]
38 """Get the device's 4-digit PIN from its UID."""
42 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
43 """Validate the user input allows us to connect.
45 Data has the keys from DATA_SCHEMA with values provided by the user.
47 async
with Client(data[CONF_IP_ADDRESS])
as client:
48 ping_data = await client.system.ping()
51 CONF_UID: ping_data[
"data"][
"uid"],
56 """Handle a config flow for Elexa Guardian."""
65 """Set the config entry's unique ID (based on the device's 4-digit PIN)."""
69 updates={CONF_IP_ADDRESS: self.
discovery_infodiscovery_info[CONF_IP_ADDRESS]}
72 {CONF_IP_ADDRESS: self.
discovery_infodiscovery_info[CONF_IP_ADDRESS]}
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Handle configuration via the UI."""
81 if user_input
is None:
83 step_id=
"user", data_schema=DATA_SCHEMA, errors={}
88 except GuardianError
as err:
89 LOGGER.error(
"Error while connecting to unit: %s", err)
92 data_schema=DATA_SCHEMA,
93 errors={CONF_IP_ADDRESS:
"cannot_connect"},
100 title=info[CONF_UID], data={CONF_UID: info[
"uid"], **user_input}
104 self, discovery_info: dhcp.DhcpServiceInfo
105 ) -> ConfigFlowResult:
106 """Handle the configuration via dhcp."""
108 CONF_IP_ADDRESS: discovery_info.ip,
109 CONF_PORT: DEFAULT_PORT,
117 self, discovery_info: zeroconf.ZeroconfServiceInfo
118 ) -> ConfigFlowResult:
119 """Handle the configuration via zeroconf."""
121 CONF_IP_ADDRESS: discovery_info.host,
122 CONF_PORT: discovery_info.port,
129 self, user_input: dict[str, Any] |
None =
None
130 ) -> ConfigFlowResult:
131 """Finish the configuration via any discovery."""
132 if user_input
is None:
None _async_set_unique_id(self, str pin)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
ConfigFlowResult async_step_discovery_confirm(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
None _set_confirm_only(self)
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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_step_user(self, dict[str, Any]|None user_input=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
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)
_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)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)
str async_get_pin_from_discovery_hostname(str hostname)
str async_get_pin_from_uid(str uid)