1 """Config flow for Medcom BlE integration."""
3 from __future__
import annotations
8 from bleak
import BleakError
9 from bluetooth_data_tools
import human_readable_name
10 from medcom_ble
import MedcomBleDevice, MedcomBleDeviceData
11 from medcom_ble.const
import INSPECTOR_SERVICE_UUID
12 import voluptuous
as vol
17 async_discovered_service_info,
23 from .const
import DOMAIN
25 _LOGGER = logging.getLogger(__name__)
29 """Handle a config flow for Medcom BLE radiation monitors."""
34 """Initialize the config flow."""
36 self._discovered_devices: dict[str, BluetoothServiceInfo] = {}
39 self, service_info: BluetoothServiceInfo
41 ble_device = bluetooth.async_ble_device_from_address(
42 self.hass, service_info.address
44 if ble_device
is None:
45 _LOGGER.debug(
"no ble_device in _get_device_data")
48 inspector = MedcomBleDeviceData(_LOGGER)
50 return await inspector.update_device(ble_device)
53 self, discovery_info: BluetoothServiceInfo
54 ) -> ConfigFlowResult:
55 """Handle the bluetooth discovery step."""
56 _LOGGER.debug(
"Discovered BLE device: %s", discovery_info.name)
60 self.context[
"title_placeholders"] = {
62 None, discovery_info.name, discovery_info.address
69 self, user_input: dict[str, Any] |
None =
None
70 ) -> ConfigFlowResult:
71 """Confirm discovery."""
76 if user_input
is None:
79 step_id=
"bluetooth_confirm",
80 description_placeholders={
"name": name},
86 self, user_input: dict[str, Any] |
None =
None
87 ) -> ConfigFlowResult:
88 """Handle the user step to pick discovered device."""
89 if user_input
is not None:
90 address = user_input[CONF_ADDRESS]
98 address = discovery_info.address
99 if address
in current_addresses
or address
in self._discovered_devices:
101 "Detected a device that's already configured: %s", address
105 if INSPECTOR_SERVICE_UUID
not in discovery_info.service_uuids:
108 self._discovered_devices[discovery_info.address] = discovery_info
110 if not self._discovered_devices:
114 address: discovery.name
115 for address, discovery
in self._discovered_devices.items()
119 data_schema=vol.Schema(
121 vol.Required(CONF_ADDRESS): vol.In(titles),
127 """Check we can connect to the device before considering the configuration is successful."""
132 _LOGGER.debug(
"Checking device connection: %s", self.
_discovery_info_discovery_info.name)
141 "Error occurred reading information from %s",
145 _LOGGER.debug(
"Device connection successful, proceeding")
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
MedcomBleDevice _get_device_data(self, BluetoothServiceInfo service_info)
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfo discovery_info)
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_check_connection(self)
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)
str human_readable_name(str hostname, str vendor, str mac_address)