1 """Config flow to configure the Nuki integration."""
3 from collections.abc
import Mapping
7 from pynuki
import NukiBridge
8 from pynuki.bridge
import InvalidCredentialsException
9 from requests.exceptions
import RequestException
10 import voluptuous
as vol
17 from .const
import CONF_ENCRYPT_TOKEN, DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN
18 from .helpers
import CannotConnect, InvalidAuth, parse_id
20 _LOGGER = logging.getLogger(__name__)
22 USER_SCHEMA = vol.Schema(
24 vol.Required(CONF_HOST): str,
25 vol.Optional(CONF_PORT, default=DEFAULT_PORT): vol.Coerce(int),
26 vol.Required(CONF_TOKEN): str,
30 REAUTH_SCHEMA = vol.Schema(
32 vol.Required(CONF_TOKEN): str,
33 vol.Optional(CONF_ENCRYPT_TOKEN, default=
True): bool,
38 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
39 """Validate the user input allows us to connect.
41 Data has the keys from USER_SCHEMA with values provided by the user.
45 bridge = await hass.async_add_executor_job(
50 data.get(CONF_ENCRYPT_TOKEN,
True),
54 info = await hass.async_add_executor_job(bridge.info)
55 except InvalidCredentialsException
as err:
56 raise InvalidAuth
from err
57 except RequestException
as err:
58 raise CannotConnect
from err
64 """Nuki config flow."""
67 """Initialize the Nuki config flow."""
69 self.
_data_data: Mapping[str, Any] = {}
72 self, user_input: dict[str, Any] |
None =
None
73 ) -> ConfigFlowResult:
74 """Handle a flow initiated by the user."""
78 self, discovery_info: dhcp.DhcpServiceInfo
79 ) -> ConfigFlowResult:
80 """Prepare configuration for a DHCP discovered Nuki bridge."""
87 vol.Required(CONF_HOST, default=discovery_info.ip): str,
88 vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
89 vol.Required(CONF_TOKEN): str,
96 self, entry_data: Mapping[str, Any]
97 ) -> ConfigFlowResult:
98 """Perform reauth upon an API authentication error."""
104 self, user_input: dict[str, Any] |
None =
None
105 ) -> ConfigFlowResult:
106 """Dialog that inform the user that reauth is required."""
108 if user_input
is None:
110 step_id=
"reauth_confirm", data_schema=REAUTH_SCHEMA
114 CONF_HOST: self.
_data_data[CONF_HOST],
115 CONF_PORT: self.
_data_data[CONF_PORT],
116 CONF_TOKEN: user_input[CONF_TOKEN],
117 CONF_ENCRYPT_TOKEN: user_input[CONF_ENCRYPT_TOKEN],
122 except CannotConnect:
123 errors[
"base"] =
"cannot_connect"
125 errors[
"base"] =
"invalid_auth"
127 _LOGGER.exception(
"Unexpected exception")
128 errors[
"base"] =
"unknown"
135 self.hass.config_entries.async_update_entry(existing_entry, data=conf)
136 self.hass.async_create_task(
137 self.hass.config_entries.async_reload(existing_entry.entry_id)
140 errors[
"base"] =
"unknown"
143 step_id=
"reauth_confirm", data_schema=REAUTH_SCHEMA, errors=errors
147 self, user_input: dict[str, Any] |
None =
None
148 ) -> ConfigFlowResult:
149 """Handle init step of a flow."""
154 if user_input
is not None:
155 data_schema = USER_SCHEMA.extend(
157 vol.Optional(CONF_ENCRYPT_TOKEN, default=
True): bool,
162 except CannotConnect:
163 errors[
"base"] =
"cannot_connect"
165 errors[
"base"] =
"invalid_auth"
167 _LOGGER.exception(
"Unexpected exception")
168 errors[
"base"] =
"unknown"
170 if "base" not in errors:
171 bridge_id =
parse_id(info[
"ids"][
"hardwareId"])
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
ConfigFlowResult async_step_validate(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(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")
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)
def parse_id(hardware_id)