1 """Config flow for Idasen Desk integration."""
3 from __future__
import annotations
8 from bleak.exc
import BleakError
9 from bluetooth_data_tools
import human_readable_name
10 from idasen_ha
import Desk
11 from idasen_ha.errors
import AuthFailedError
12 import voluptuous
as vol
15 BluetoothServiceInfoBleak,
16 async_discovered_service_info,
21 from .const
import DOMAIN, EXPECTED_SERVICE_UUID
23 _LOGGER = logging.getLogger(__name__)
27 """Handle a config flow for Idasen Desk integration."""
32 """Initialize the config flow."""
33 self.
_discovery_info_discovery_info: BluetoothServiceInfoBleak |
None =
None
34 self._discovered_devices: dict[str, BluetoothServiceInfoBleak] = {}
37 self, discovery_info: BluetoothServiceInfoBleak
38 ) -> ConfigFlowResult:
39 """Handle the bluetooth discovery step."""
43 self.context[
"title_placeholders"] = {
45 None, discovery_info.name, discovery_info.address
51 self, user_input: dict[str, Any] |
None =
None
52 ) -> ConfigFlowResult:
53 """Handle the user step to pick discovered device."""
54 errors: dict[str, str] = {}
56 if user_input
is not None:
57 address = user_input[CONF_ADDRESS]
58 discovery_info = self._discovered_devices[address]
59 local_name = discovery_info.name
61 discovery_info.address, raise_on_progress=
False
65 desk = Desk(
None, monitor_height=
False)
67 await desk.connect(discovery_info.device, retry=
False)
68 except AuthFailedError:
69 errors[
"base"] =
"auth_failed"
71 errors[
"base"] =
"cannot_connect"
73 _LOGGER.exception(
"Unexpected Bluetooth error")
74 errors[
"base"] =
"cannot_connect"
76 _LOGGER.exception(
"Unexpected error")
77 errors[
"base"] =
"unknown"
79 await desk.disconnect()
83 CONF_ADDRESS: discovery_info.address,
88 self._discovered_devices[discovery.address] = discovery
93 discovery.address
in current_addresses
94 or discovery.address
in self._discovered_devices
95 or EXPECTED_SERVICE_UUID
not in discovery.service_uuids
98 self._discovered_devices[discovery.address] = discovery
100 if not self._discovered_devices:
103 data_schema = vol.Schema(
105 vol.Required(CONF_ADDRESS): vol.In(
107 service_info.address: f
"{service_info.name} ({service_info.address})"
108 for service_info
in self._discovered_devices.values()
115 data_schema=data_schema,
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
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")
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_step_user(self, dict[str, Any]|None user_input=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)
_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)
str human_readable_name(str hostname, str vendor, str mac_address)