1 """Config flow for Xiaomi Bluetooth integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
10 import voluptuous
as vol
11 from xiaomi_ble
import (
12 XiaomiBluetoothDeviceData
as DeviceData,
14 XiaomiCloudInvalidAuthenticationException,
15 XiaomiCloudTokenFetch,
17 from xiaomi_ble.parser
import EncryptionScheme
21 BluetoothScanningMode,
23 async_discovered_service_info,
24 async_process_advertisements,
31 from .const
import DOMAIN
34 ADDITIONAL_DISCOVERY_TIMEOUT = 60
36 _LOGGER = logging.getLogger(__name__)
39 @dataclasses.dataclass
41 """A discovered bluetooth device."""
44 discovery_info: BluetoothServiceInfo
48 def _title(discovery_info: BluetoothServiceInfo, device: DeviceData) -> str:
49 return device.title
or device.get_device_name()
or discovery_info.name
53 """Handle a config flow for Xiaomi Bluetooth."""
58 """Initialize the config flow."""
61 self._discovered_devices: dict[str, Discovery] = {}
64 self, discovery_info: BluetoothServiceInfo, device: DeviceData
65 ) -> BluetoothServiceInfo:
66 """Sometimes first advertisement we receive is blank or incomplete.
68 Wait until we get a useful one.
70 if not device.pending:
73 def _process_more_advertisements(
74 service_info: BluetoothServiceInfo,
76 device.update(service_info)
77 return not device.pending
81 _process_more_advertisements,
82 {
"address": discovery_info.address},
83 BluetoothScanningMode.ACTIVE,
84 ADDITIONAL_DISCOVERY_TIMEOUT,
88 self, discovery_info: BluetoothServiceInfo
89 ) -> ConfigFlowResult:
90 """Handle the bluetooth discovery step."""
94 if not device.supported(discovery_info):
97 title =
_title(discovery_info, device)
98 self.context[
"title_placeholders"] = {
"name": title}
106 discovery_info, device
114 if device.encryption_scheme == EncryptionScheme.MIBEACON_LEGACY:
116 if device.encryption_scheme == EncryptionScheme.MIBEACON_4_5:
121 self, user_input: dict[str, Any] |
None =
None
122 ) -> ConfigFlowResult:
123 """Enter a legacy bindkey for a v2/v3 MiBeacon device."""
129 if user_input
is not None:
130 bindkey = user_input[
"bindkey"]
132 if len(bindkey) != 24:
133 errors[
"bindkey"] =
"expected_24_characters"
145 errors[
"bindkey"] =
"decryption_failed"
148 step_id=
"get_encryption_key_legacy",
149 description_placeholders=self.context[
"title_placeholders"],
150 data_schema=vol.Schema({vol.Required(
"bindkey"): vol.All(str, vol.Strip)}),
155 self, user_input: dict[str, Any] |
None =
None
156 ) -> ConfigFlowResult:
157 """Enter a bindkey for a v4/v5 MiBeacon device."""
163 if user_input
is not None:
164 bindkey = user_input[
"bindkey"]
166 if len(bindkey) != 32:
167 errors[
"bindkey"] =
"expected_32_characters"
179 errors[
"bindkey"] =
"decryption_failed"
182 step_id=
"get_encryption_key_4_5",
183 description_placeholders=self.context[
"title_placeholders"],
184 data_schema=vol.Schema({vol.Required(
"bindkey"): vol.All(str, vol.Strip)}),
189 self, user_input: dict[str, Any] |
None =
None
190 ) -> ConfigFlowResult:
191 """Handle the cloud auth step."""
194 errors: dict[str, str] = {}
195 description_placeholders: dict[str, str] = {}
196 if user_input
is not None:
198 fetcher = XiaomiCloudTokenFetch(
199 user_input[CONF_USERNAME], user_input[CONF_PASSWORD], session
202 device_details = await fetcher.get_device_info(
205 except XiaomiCloudInvalidAuthenticationException
as ex:
206 _LOGGER.debug(
"Authentication failed: %s", ex, exc_info=
True)
207 errors = {
"base":
"auth_failed"}
208 description_placeholders = {
"error_detail":
str(ex)}
209 except XiaomiCloudException
as ex:
210 _LOGGER.debug(
"Failed to connect to MI API: %s", ex, exc_info=
True)
212 "api_error", description_placeholders={
"error_detail":
str(ex)}
217 {
"bindkey": device_details.bindkey}
219 errors = {
"base":
"api_device_not_found"}
221 user_input = user_input
or {}
223 step_id=
"cloud_auth",
225 data_schema=vol.Schema(
228 CONF_USERNAME, default=user_input.get(CONF_USERNAME)
230 vol.Required(CONF_PASSWORD): str,
233 description_placeholders={
234 **self.context[
"title_placeholders"],
235 **description_placeholders,
240 self, user_input: dict[str, Any] |
None =
None
241 ) -> ConfigFlowResult:
242 """Choose method to get the bind key for a version 4/5 device."""
244 step_id=
"get_encryption_key_4_5_choose_method",
245 menu_options=[
"cloud_auth",
"get_encryption_key_4_5"],
246 description_placeholders=self.context[
"title_placeholders"],
250 self, user_input: dict[str, Any] |
None =
None
251 ) -> ConfigFlowResult:
252 """Confirm discovery."""
253 if user_input
is not None or not onboarding.async_is_onboarded(self.hass):
258 step_id=
"bluetooth_confirm",
259 description_placeholders=self.context[
"title_placeholders"],
263 self, user_input: dict[str, Any] |
None =
None
264 ) -> ConfigFlowResult:
265 """Ack that device is slow."""
266 if user_input
is not None:
271 step_id=
"confirm_slow",
272 description_placeholders=self.context[
"title_placeholders"],
276 self, user_input: dict[str, Any] |
None =
None
277 ) -> ConfigFlowResult:
278 """Handle the user step to pick discovered device."""
279 if user_input
is not None:
280 address = user_input[CONF_ADDRESS]
283 discovery = self._discovered_devices[address]
285 self.context[
"title_placeholders"] = {
"name": discovery.title}
291 discovery.discovery_info, discovery.device
301 if discovery.device.encryption_scheme == EncryptionScheme.MIBEACON_LEGACY:
304 if discovery.device.encryption_scheme == EncryptionScheme.MIBEACON_4_5:
311 address = discovery_info.address
312 if address
in current_addresses
or address
in self._discovered_devices:
314 device = DeviceData()
315 if device.supported(discovery_info):
316 self._discovered_devices[address] =
Discovery(
317 title=
_title(discovery_info, device),
318 discovery_info=discovery_info,
322 if not self._discovered_devices:
326 address: discovery.title
327 for (address, discovery)
in self._discovered_devices.items()
331 data_schema=vol.Schema({vol.Required(CONF_ADDRESS): vol.In(titles)}),
335 self, entry_data: Mapping[str, Any]
336 ) -> ConfigFlowResult:
337 """Handle a flow initialized by a reauth event."""
338 device: DeviceData = entry_data[
"device"]
343 if device.encryption_scheme == EncryptionScheme.MIBEACON_LEGACY:
346 if device.encryption_scheme == EncryptionScheme.MIBEACON_4_5:
353 self, bindkey: str |
None =
None
354 ) -> ConfigFlowResult:
355 data: dict[str, Any] = {}
358 data[
"bindkey"] = bindkey
366 title=self.context[
"title_placeholders"][
"name"],
ConfigFlowResult async_step_get_encryption_key_4_5_choose_method(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_get_encryption_key_legacy(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
BluetoothServiceInfo _async_wait_for_full_advertisement(self, BluetoothServiceInfo discovery_info, DeviceData device)
ConfigFlowResult async_step_confirm_slow(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)
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfo discovery_info)
ConfigFlowResult async_step_cloud_auth(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_get_encryption_key_4_5(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _async_get_or_create_entry(self, str|None bindkey=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_show_menu(self, *str|None step_id=None, Container[str] menu_options, Mapping[str, str]|None description_placeholders=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 async_process_advertisements(HomeAssistant hass, ProcessAdvertisementCallback callback, BluetoothCallbackMatcher match_dict, BluetoothScanningMode mode, int timeout)
str _title(BluetoothServiceInfo discovery_info, DeviceData device)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)