-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_flow.py
52 lines (44 loc) · 1.88 KB
/
config_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from homeassistant import config_entries
from homeassistant.components.bluetooth import (
BluetoothServiceInfoBleak,
async_discovered_service_info,
)
from homeassistant.data_entry_flow import FlowResult
from homeassistant.const import CONF_ADDRESS
import voluptuous as vol
from .const import DOMAIN
class BLEScaleConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
def __init__(self) -> None:
self.discovered_devices: dict[str, BluetoothServiceInfoBleak] = {}
async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
errors = {}
# Get discovered devices
for discovery_info in async_discovered_service_info(self.hass):
address = discovery_info.address
self.discovered_devices[address] = discovery_info
if user_input is not None:
address = user_input[CONF_ADDRESS]
await self.async_set_unique_id(address)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=f"BLE Scale ({address})",
data=user_input,
)
device_list = {
address: f"{info.name} ({address})" if info.name else address
for address, info in self.discovered_devices.items()
}
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({
vol.Required(CONF_ADDRESS): vol.In(device_list),
}),
errors=errors,
)
async def async_step_bluetooth(self, discovery_info: BluetoothServiceInfoBleak) -> FlowResult:
"""Handle the bluetooth discovery step."""
await self.async_set_unique_id(discovery_info.address)
self._abort_if_unique_id_configured()
self.discovered_devices[discovery_info.address] = discovery_info
return await self.async_step_user()