Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the EufyLife integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from eufylife_ble_client import MODEL_TO_NAME
8 import voluptuous as vol
9 
11  BluetoothServiceInfoBleak,
12  async_discovered_service_info,
13 )
14 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
15 from homeassistant.const import CONF_ADDRESS, CONF_MODEL
16 
17 from .const import DOMAIN
18 
19 
20 class EufyLifeConfigFlow(ConfigFlow, domain=DOMAIN):
21  """Handle a config flow for EufyLife."""
22 
23  VERSION = 1
24 
25  def __init__(self) -> None:
26  """Initialize the config flow."""
27  self._discovery_info_discovery_info: BluetoothServiceInfoBleak | None = None
28  self._discovered_devices: dict[str, str] = {}
29 
31  self, discovery_info: BluetoothServiceInfoBleak
32  ) -> ConfigFlowResult:
33  """Handle the bluetooth discovery step."""
34  await self.async_set_unique_idasync_set_unique_id(discovery_info.address)
35  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
36 
37  if discovery_info.name not in MODEL_TO_NAME:
38  return self.async_abortasync_abortasync_abort(reason="not_supported")
39 
40  self._discovery_info_discovery_info = discovery_info
41  return await self.async_step_bluetooth_confirmasync_step_bluetooth_confirm()
42 
44  self, user_input: dict[str, Any] | None = None
45  ) -> ConfigFlowResult:
46  """Confirm discovery."""
47  assert self._discovery_info_discovery_info is not None
48  discovery_info = self._discovery_info_discovery_info
49 
50  model_name = MODEL_TO_NAME.get(discovery_info.name)
51  assert model_name is not None
52 
53  if user_input is not None:
54  return self.async_create_entryasync_create_entryasync_create_entry(
55  title=model_name, data={CONF_MODEL: discovery_info.name}
56  )
57 
58  self._set_confirm_only_set_confirm_only()
59  placeholders = {"name": model_name}
60  self.context["title_placeholders"] = placeholders
61  return self.async_show_formasync_show_formasync_show_form(
62  step_id="bluetooth_confirm", description_placeholders=placeholders
63  )
64 
65  async def async_step_user(
66  self, user_input: dict[str, Any] | None = None
67  ) -> ConfigFlowResult:
68  """Handle the user step to pick discovered device."""
69  if user_input is not None:
70  address = user_input[CONF_ADDRESS]
71  await self.async_set_unique_idasync_set_unique_id(address, raise_on_progress=False)
72  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
73 
74  model = self._discovered_devices[address]
75  return self.async_create_entryasync_create_entryasync_create_entry(
76  title=MODEL_TO_NAME[model],
77  data={CONF_MODEL: model},
78  )
79 
80  current_addresses = self._async_current_ids_async_current_ids()
81  for discovery_info in async_discovered_service_info(self.hass, False):
82  address = discovery_info.address
83  if (
84  address in current_addresses
85  or address in self._discovered_devices
86  or discovery_info.name not in MODEL_TO_NAME
87  ):
88  continue
89  self._discovered_devices[address] = discovery_info.name
90 
91  if not self._discovered_devices:
92  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
93 
94  return self.async_show_formasync_show_formasync_show_form(
95  step_id="user",
96  data_schema=vol.Schema(
97  {vol.Required(CONF_ADDRESS): vol.In(self._discovered_devices)}
98  ),
99  )
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:45
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:67
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:32
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_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)
Definition: api.py:72