-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_flow.py
169 lines (140 loc) · 5.91 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Adds config flow for Bermuda BLE Trilateration."""
from __future__ import annotations
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import bluetooth
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
from homeassistant.core import callback
from homeassistant.helpers.config_entry_flow import FlowResult
from homeassistant.helpers.selector import selector
from .const import CONF_ATTENUATION
from .const import CONF_DEVICES
from .const import CONF_DEVTRACK_TIMEOUT
from .const import CONF_MAX_RADIUS
from .const import CONF_REF_POWER
from .const import DEFAULT_ATTENUATION
from .const import DEFAULT_DEVTRACK_TIMEOUT
from .const import DEFAULT_MAX_RADIUS
from .const import DEFAULT_REF_POWER
from .const import DOMAIN
from .const import NAME
# from homeassistant import data_entry_flow
# from homeassistant.helpers.aiohttp_client import async_create_clientsession
class BermudaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for bermuda."""
VERSION = 1
# CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
def __init__(self):
"""Initialize."""
self._errors = {}
async def async_step_bluetooth(
self, discovery_info: BluetoothServiceInfoBleak
) -> FlowResult:
"""Support automatic initiation of setup through bluetooth discovery.
(we still show a confirmation form to the user, though)
This is triggered by discovery matchers set in manifest.json,
and since we track any BLE advert, we're being a little cheeky by listing any.
"""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
# Create a unique ID so that we don't get multiple discoveries appearing.
await self.async_set_unique_id(DOMAIN)
self._abort_if_unique_id_configured()
return self.async_show_form(step_id="user", description_placeholders=NAME)
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user.
We don't need any config for base setup, so we just activate
(but only for one instance)
"""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
if user_input is not None:
# create the integration!
return self.async_create_entry(
title=NAME, data={"source": "user"}, description=NAME
)
return self.async_show_form(step_id="user", description_placeholders=NAME)
@staticmethod
@callback
def async_get_options_flow(config_entry):
return BermudaOptionsFlowHandler(config_entry)
# async def _show_config_form(self, user_input): # pylint: disable=unused-argument
# """Show the configuration form to edit location data."""
# return self.async_show_form(
# step_id="user",
# data_schema=vol.Schema(
# {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
# ),
# errors=self._errors,
# )
class BermudaOptionsFlowHandler(config_entries.OptionsFlow):
"""Config flow options handler for bermuda."""
def __init__(self, config_entry):
"""Initialize HACS options flow."""
self.config_entry = config_entry
self.options = dict(config_entry.options)
async def async_step_init(self, user_input=None): # pylint: disable=unused-argument
"""Manage the options."""
return await self.async_step_globalopts()
async def async_step_globalopts(self, user_input=None):
"""Handle a flow initialized by the user."""
if user_input is not None:
self.options.update(user_input)
return await self._update_options()
options = []
for service_info in bluetooth.async_discovered_service_info(self.hass, False):
devname = (
service_info.name
or service_info.advertisement.local_name
or service_info.device.name
)
options.append(
{
"value": service_info.address.upper(),
"label": f"[{service_info.address}] {devname}",
}
)
for address in self.options.get(CONF_DEVICES, []):
if not next(
(item for item in options if item["value"] == address.upper()), False
):
options.append(
{"value": address.upper(), "label": f"[{address}] (saved)"}
)
data_schema = {
vol.Required(
CONF_MAX_RADIUS,
default=self.options.get(CONF_MAX_RADIUS, DEFAULT_MAX_RADIUS),
): vol.Coerce(float),
vol.Required(
CONF_DEVTRACK_TIMEOUT,
default=self.options.get(
CONF_DEVTRACK_TIMEOUT, DEFAULT_DEVTRACK_TIMEOUT
),
): vol.Coerce(int),
vol.Required(
CONF_ATTENUATION,
default=self.options.get(CONF_ATTENUATION, DEFAULT_ATTENUATION),
): vol.Coerce(float),
vol.Required(
CONF_REF_POWER,
default=self.options.get(CONF_REF_POWER, DEFAULT_REF_POWER),
): vol.Coerce(float),
vol.Optional(
CONF_DEVICES,
default=self.options.get(CONF_DEVICES, []),
): selector(
{
"select": {
"options": options,
"multiple": True,
}
}
),
}
return self.async_show_form(
step_id="globalopts", data_schema=vol.Schema(data_schema)
)
async def _update_options(self):
"""Update config entry options."""
return self.async_create_entry(title=NAME, data=self.options)