1 """Config flow for Acaia integration."""
6 from aioacaia.exceptions
import AcaiaDeviceNotFound, AcaiaError, AcaiaUnknownDevice
7 from aioacaia.helpers
import is_new_scale
8 import voluptuous
as vol
11 BluetoothServiceInfoBleak,
12 async_discovered_service_info,
24 from .const
import CONF_IS_NEW_STYLE_SCALE, DOMAIN
26 _LOGGER = logging.getLogger(__name__)
30 """Handle a config flow for acaia."""
33 """Initialize the config flow."""
34 self._discovered: dict[str, Any] = {}
35 self._discovered_devices: dict[str, str] = {}
38 self, user_input: dict[str, Any] |
None =
None
39 ) -> ConfigFlowResult:
40 """Handle a flow initialized by the user."""
42 errors: dict[str, str] = {}
44 if user_input
is not None:
45 mac = user_input[CONF_ADDRESS]
47 is_new_style_scale = await is_new_scale(mac)
48 except AcaiaDeviceNotFound:
49 errors[
"base"] =
"device_not_found"
51 _LOGGER.exception(
"Error occurred while connecting to the scale")
52 errors[
"base"] =
"unknown"
53 except AcaiaUnknownDevice:
61 title=self._discovered_devices[mac],
64 CONF_IS_NEW_STYLE_SCALE: is_new_style_scale,
69 self._discovered_devices[device.address] = device.name
71 if not self._discovered_devices:
77 label=f
"{device_name} ({device_mac})",
79 for device_mac, device_name
in self._discovered_devices.items()
84 data_schema=vol.Schema(
89 mode=SelectSelectorMode.DROPDOWN,
98 self, discovery_info: BluetoothServiceInfoBleak
99 ) -> ConfigFlowResult:
100 """Handle a discovered Bluetooth device."""
102 self._discovered[CONF_ADDRESS] = discovery_info.address
103 self._discovered[CONF_NAME] = discovery_info.name
109 self._discovered[CONF_IS_NEW_STYLE_SCALE] = await is_new_scale(
110 discovery_info.address
112 except AcaiaDeviceNotFound:
113 _LOGGER.debug(
"Device not found during discovery")
117 "Error occurred while connecting to the scale during discovery",
121 except AcaiaUnknownDevice:
122 _LOGGER.debug(
"Unsupported device during discovery")
128 self, user_input: dict[str, Any] |
None =
None
129 ) -> ConfigFlowResult:
130 """Handle confirmation of Bluetooth discovery."""
132 if user_input
is not None:
134 title=self._discovered[CONF_NAME],
136 CONF_ADDRESS: self._discovered[CONF_ADDRESS],
137 CONF_IS_NEW_STYLE_SCALE: self._discovered[CONF_IS_NEW_STYLE_SCALE],
141 self.context[
"title_placeholders"] = placeholders = {
142 CONF_NAME: self._discovered[CONF_NAME]
147 step_id=
"bluetooth_confirm",
148 description_placeholders=placeholders,
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
None _set_confirm_only(self)
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)