1 """Config flow for BTHome Bluetooth integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from bthome_ble
import BTHomeBluetoothDeviceData
as DeviceData
10 from bthome_ble.parser
import EncryptionScheme
11 import voluptuous
as vol
15 BluetoothServiceInfoBleak,
16 async_discovered_service_info,
21 from .const
import DOMAIN
24 @dataclasses.dataclass
26 """A discovered bluetooth device."""
29 discovery_info: BluetoothServiceInfoBleak
33 def _title(discovery_info: BluetoothServiceInfoBleak, device: DeviceData) -> str:
34 return device.title
or device.get_device_name()
or discovery_info.name
38 """Handle a config flow for BTHome Bluetooth."""
43 """Initialize the config flow."""
44 self.
_discovery_info_discovery_info: BluetoothServiceInfoBleak |
None =
None
46 self._discovered_devices: dict[str, Discovery] = {}
49 self, discovery_info: BluetoothServiceInfoBleak
50 ) -> ConfigFlowResult:
51 """Handle the bluetooth discovery step."""
56 if not device.supported(discovery_info):
59 title =
_title(discovery_info, device)
60 self.context[
"title_placeholders"] = {
"name": title}
64 if device.encryption_scheme == EncryptionScheme.BTHOME_BINDKEY:
69 self, user_input: dict[str, Any] |
None =
None
70 ) -> ConfigFlowResult:
71 """Enter a bindkey for an encrypted BTHome device."""
77 if user_input
is not None:
78 bindkey: str = user_input[
"bindkey"]
80 if len(bindkey) != 32:
81 errors[
"bindkey"] =
"expected_32_characters"
93 errors[
"bindkey"] =
"decryption_failed"
96 step_id=
"get_encryption_key",
97 description_placeholders=self.context[
"title_placeholders"],
98 data_schema=vol.Schema({vol.Required(
"bindkey"): vol.All(str, vol.Strip)}),
103 self, user_input: dict[str, Any] |
None =
None
104 ) -> ConfigFlowResult:
105 """Confirm discovery."""
106 if user_input
is not None or not onboarding.async_is_onboarded(self.hass):
111 step_id=
"bluetooth_confirm",
112 description_placeholders=self.context[
"title_placeholders"],
116 self, user_input: dict[str, Any] |
None =
None
117 ) -> ConfigFlowResult:
118 """Handle the user step to pick discovered device."""
119 if user_input
is not None:
120 address = user_input[CONF_ADDRESS]
123 discovery = self._discovered_devices[address]
125 self.context[
"title_placeholders"] = {
"name": discovery.title}
130 if discovery.device.encryption_scheme == EncryptionScheme.BTHOME_BINDKEY:
137 address = discovery_info.address
138 if address
in current_addresses
or address
in self._discovered_devices:
140 device = DeviceData()
141 if device.supported(discovery_info):
142 self._discovered_devices[address] =
Discovery(
143 title=
_title(discovery_info, device),
144 discovery_info=discovery_info,
148 if not self._discovered_devices:
152 address: discovery.title
153 for (address, discovery)
in self._discovered_devices.items()
157 data_schema=vol.Schema({vol.Required(CONF_ADDRESS): vol.In(titles)}),
161 self, entry_data: Mapping[str, Any]
162 ) -> ConfigFlowResult:
163 """Handle a flow initialized by a reauth event."""
164 device: DeviceData = entry_data[
"device"]
169 if device.encryption_scheme == EncryptionScheme.BTHOME_BINDKEY:
176 self, bindkey: str |
None =
None
177 ) -> ConfigFlowResult:
178 data: dict[str, Any] = {}
180 data[
"bindkey"] = bindkey
188 title=self.context[
"title_placeholders"][
"name"],
ConfigFlowResult _async_get_or_create_entry(self, str|None bindkey=None)
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_get_encryption_key(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_bluetooth_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")
ConfigEntry _get_reauth_entry(self)
None _set_confirm_only(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)
str _title(BluetoothServiceInfoBleak discovery_info, DeviceData device)