1 """Config flow for Dormakaba dKey integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from bleak
import BleakError
10 from py_dormakaba_dkey
import DKEYLock, device_filter, errors
as dkey_errors
11 import voluptuous
as vol
14 BluetoothServiceInfoBleak,
15 async_discovered_service_info,
16 async_last_service_info,
21 from .const
import CONF_ASSOCIATION_DATA, DOMAIN
23 _LOGGER = logging.getLogger(__name__)
25 STEP_ASSOCIATE_SCHEMA = vol.Schema(
27 vol.Required(
"activation_code"): str,
33 """Handle a config flow for Dormakaba dKey."""
38 """Initialize the config flow."""
39 self.
_lock_lock: DKEYLock |
None =
None
41 self._discovered_devices: dict[str, BluetoothServiceInfoBleak] = {}
43 self.
_discovery_info_discovery_info: BluetoothServiceInfoBleak |
None =
None
46 self, user_input: dict[str, Any] |
None =
None
47 ) -> ConfigFlowResult:
48 """Handle the user step to pick discovered device."""
49 errors: dict[str, str] = {}
51 if user_input
is not None:
52 address = user_input[CONF_ADDRESS]
63 discovery.address
in current_addresses
64 or discovery.address
in self._discovered_devices
65 or not device_filter(discovery.advertisement)
68 self._discovered_devices[discovery.address] = discovery
70 if not self._discovered_devices:
73 data_schema = vol.Schema(
75 vol.Required(CONF_ADDRESS): vol.In(
77 service_info.address: (
78 f
"{service_info.name} ({service_info.address})"
80 for service_info
in self._discovered_devices.values()
87 data_schema=data_schema,
92 self, discovery_info: BluetoothServiceInfoBleak
93 ) -> ConfigFlowResult:
94 """Handle the Bluetooth discovery step."""
99 self.context[
"title_placeholders"] = {
"name": name}
103 self, user_input: dict[str, Any] |
None =
None
104 ) -> ConfigFlowResult:
105 """Handle bluetooth confirm step."""
109 if user_input
is None:
112 step_id=
"bluetooth_confirm",
113 description_placeholders={
"name": name},
119 self, entry_data: Mapping[str, Any]
120 ) -> ConfigFlowResult:
121 """Handle reauthorization request."""
125 self, user_input: dict[str, Any] |
None =
None
126 ) -> ConfigFlowResult:
127 """Handle reauthorization flow."""
130 if user_input
is not None:
136 errors = {
"base":
"no_longer_in_range"}
142 step_id=
"reauth_confirm", data_schema=vol.Schema({}), errors=errors
146 self, user_input: dict[str, Any] |
None =
None
147 ) -> ConfigFlowResult:
148 """Handle associate step."""
152 if user_input
is None:
154 step_id=
"associate", data_schema=STEP_ASSOCIATE_SCHEMA
158 if not self.
_lock_lock:
160 lock = self.
_lock_lock
163 association_data = await lock.associate(user_input[
"activation_code"])
164 except BleakError
as err:
165 _LOGGER.warning(
"BleakError", exc_info=err)
167 except dkey_errors.InvalidActivationCode:
168 errors[
"base"] =
"invalid_code"
169 except dkey_errors.WrongActivationCode:
170 errors[
"base"] =
"wrong_code"
172 _LOGGER.exception(
"Unexpected exception")
177 CONF_ASSOCIATION_DATA: association_data.to_json(),
185 title=lock.device_info.device_name
186 or lock.device_info.device_id
192 step_id=
"associate", data_schema=STEP_ASSOCIATE_SCHEMA, errors=errors
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_associate(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
set[str|None] _async_current_ids(self, bool include_ignore=True)
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_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)
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)
_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)
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
BluetoothServiceInfoBleak|None async_last_service_info(HomeAssistant hass, str address, bool connectable=True)