Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Acaia integration."""
2 
3 import logging
4 from typing import Any
5 
6 from aioacaia.exceptions import AcaiaDeviceNotFound, AcaiaError, AcaiaUnknownDevice
7 from aioacaia.helpers import is_new_scale
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_NAME
16 from homeassistant.helpers.device_registry import format_mac
18  SelectOptionDict,
19  SelectSelector,
20  SelectSelectorConfig,
21  SelectSelectorMode,
22 )
23 
24 from .const import CONF_IS_NEW_STYLE_SCALE, DOMAIN
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
29 class AcaiaConfigFlow(ConfigFlow, domain=DOMAIN):
30  """Handle a config flow for acaia."""
31 
32  def __init__(self) -> None:
33  """Initialize the config flow."""
34  self._discovered: dict[str, Any] = {}
35  self._discovered_devices: dict[str, str] = {}
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle a flow initialized by the user."""
41 
42  errors: dict[str, str] = {}
43 
44  if user_input is not None:
45  mac = user_input[CONF_ADDRESS]
46  try:
47  is_new_style_scale = await is_new_scale(mac)
48  except AcaiaDeviceNotFound:
49  errors["base"] = "device_not_found"
50  except AcaiaError:
51  _LOGGER.exception("Error occurred while connecting to the scale")
52  errors["base"] = "unknown"
53  except AcaiaUnknownDevice:
54  return self.async_abortasync_abortasync_abort(reason="unsupported_device")
55  else:
56  await self.async_set_unique_idasync_set_unique_id(format_mac(mac))
57  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
58 
59  if not errors:
60  return self.async_create_entryasync_create_entryasync_create_entry(
61  title=self._discovered_devices[mac],
62  data={
63  CONF_ADDRESS: mac,
64  CONF_IS_NEW_STYLE_SCALE: is_new_style_scale,
65  },
66  )
67 
68  for device in async_discovered_service_info(self.hass):
69  self._discovered_devices[device.address] = device.name
70 
71  if not self._discovered_devices:
72  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
73 
74  options = [
76  value=device_mac,
77  label=f"{device_name} ({device_mac})",
78  )
79  for device_mac, device_name in self._discovered_devices.items()
80  ]
81 
82  return self.async_show_formasync_show_formasync_show_form(
83  step_id="user",
84  data_schema=vol.Schema(
85  {
86  vol.Required(CONF_ADDRESS): SelectSelector(
88  options=options,
89  mode=SelectSelectorMode.DROPDOWN,
90  )
91  )
92  }
93  ),
94  errors=errors,
95  )
96 
98  self, discovery_info: BluetoothServiceInfoBleak
99  ) -> ConfigFlowResult:
100  """Handle a discovered Bluetooth device."""
101 
102  self._discovered[CONF_ADDRESS] = discovery_info.address
103  self._discovered[CONF_NAME] = discovery_info.name
104 
105  await self.async_set_unique_idasync_set_unique_id(format_mac(discovery_info.address))
106  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
107 
108  try:
109  self._discovered[CONF_IS_NEW_STYLE_SCALE] = await is_new_scale(
110  discovery_info.address
111  )
112  except AcaiaDeviceNotFound:
113  _LOGGER.debug("Device not found during discovery")
114  return self.async_abortasync_abortasync_abort(reason="device_not_found")
115  except AcaiaError:
116  _LOGGER.debug(
117  "Error occurred while connecting to the scale during discovery",
118  exc_info=True,
119  )
120  return self.async_abortasync_abortasync_abort(reason="unknown")
121  except AcaiaUnknownDevice:
122  _LOGGER.debug("Unsupported device during discovery")
123  return self.async_abortasync_abortasync_abort(reason="unsupported_device")
124 
125  return await self.async_step_bluetooth_confirmasync_step_bluetooth_confirm()
126 
128  self, user_input: dict[str, Any] | None = None
129  ) -> ConfigFlowResult:
130  """Handle confirmation of Bluetooth discovery."""
131 
132  if user_input is not None:
133  return self.async_create_entryasync_create_entryasync_create_entry(
134  title=self._discovered[CONF_NAME],
135  data={
136  CONF_ADDRESS: self._discovered[CONF_ADDRESS],
137  CONF_IS_NEW_STYLE_SCALE: self._discovered[CONF_IS_NEW_STYLE_SCALE],
138  },
139  )
140 
141  self.context["title_placeholders"] = placeholders = {
142  CONF_NAME: self._discovered[CONF_NAME]
143  }
144 
145  self._set_confirm_only_set_confirm_only()
146  return self.async_show_formasync_show_formasync_show_form(
147  step_id="bluetooth_confirm",
148  description_placeholders=placeholders,
149  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:129
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:99
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
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