-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
/
Copy pathconfig_flow.py
245 lines (200 loc) · 8.66 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""Config flow for Lutron Caseta."""
from __future__ import annotations
import asyncio
import logging
import os
import ssl
import async_timeout
from pylutron_caseta.pairing import PAIR_CA, PAIR_CERT, PAIR_KEY, async_pair
from pylutron_caseta.smartbridge import Smartbridge
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from .const import (
ABORT_REASON_CANNOT_CONNECT,
BRIDGE_DEVICE_ID,
BRIDGE_TIMEOUT,
CONF_CA_CERTS,
CONF_CERTFILE,
CONF_KEYFILE,
DOMAIN,
ERROR_CANNOT_CONNECT,
STEP_IMPORT_FAILED,
)
HOSTNAME = "hostname"
FILE_MAPPING = {
PAIR_KEY: CONF_KEYFILE,
PAIR_CERT: CONF_CERTFILE,
PAIR_CA: CONF_CA_CERTS,
}
_LOGGER = logging.getLogger(__name__)
ENTRY_DEFAULT_TITLE = "Caséta bridge"
DATA_SCHEMA_USER = vol.Schema({vol.Required(CONF_HOST): str})
TLS_ASSET_TEMPLATE = "lutron_caseta-{}-{}.pem"
class LutronCasetaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle Lutron Caseta config flow."""
VERSION = 1
def __init__(self):
"""Initialize a Lutron Caseta flow."""
self.data = {}
self.lutron_id = None
self.tls_assets_validated = False
self.attempted_tls_validation = False
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
if user_input is not None:
self.data[CONF_HOST] = user_input[CONF_HOST]
return await self.async_step_link()
return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA_USER)
async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle a flow initialized by zeroconf discovery."""
hostname = discovery_info.hostname
if hostname is None or not hostname.lower().startswith("lutron-"):
return self.async_abort(reason="not_lutron_device")
self.lutron_id = hostname.split("-")[1].replace(".local.", "")
await self.async_set_unique_id(self.lutron_id)
host = discovery_info.host
self._abort_if_unique_id_configured({CONF_HOST: host})
self.data[CONF_HOST] = host
self.context["title_placeholders"] = {
CONF_NAME: self.bridge_id,
CONF_HOST: host,
}
return await self.async_step_link()
async def async_step_homekit(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle a flow initialized by homekit discovery."""
return await self.async_step_zeroconf(discovery_info)
async def async_step_link(self, user_input=None):
"""Handle pairing with the hub."""
errors = {}
# Abort if existing entry with matching host exists.
self._async_abort_entries_match({CONF_HOST: self.data[CONF_HOST]})
self._configure_tls_assets()
if (
not self.attempted_tls_validation
and await self.hass.async_add_executor_job(self._tls_assets_exist)
and await self.async_get_lutron_id()
):
self.tls_assets_validated = True
self.attempted_tls_validation = True
if user_input is not None:
if self.tls_assets_validated:
# If we previous paired and the tls assets already exist,
# we do not need to go though pairing again.
return self.async_create_entry(title=self.bridge_id, data=self.data)
assets = None
try:
assets = await async_pair(self.data[CONF_HOST])
except (asyncio.TimeoutError, OSError):
errors["base"] = "cannot_connect"
if not errors:
await self.hass.async_add_executor_job(self._write_tls_assets, assets)
return self.async_create_entry(title=self.bridge_id, data=self.data)
return self.async_show_form(
step_id="link",
errors=errors,
description_placeholders={
CONF_NAME: self.bridge_id,
CONF_HOST: self.data[CONF_HOST],
},
)
@property
def bridge_id(self):
"""Return the best identifier for the bridge.
If the bridge was not discovered via zeroconf,
we fallback to using the host.
"""
return self.lutron_id or self.data[CONF_HOST]
def _write_tls_assets(self, assets):
"""Write the tls assets to disk."""
for asset_key, conf_key in FILE_MAPPING.items():
with open(
self.hass.config.path(self.data[conf_key]), "w", encoding="utf8"
) as file_handle:
file_handle.write(assets[asset_key])
def _tls_assets_exist(self):
"""Check to see if tls assets are already on disk."""
for conf_key in FILE_MAPPING.values():
if not os.path.exists(self.hass.config.path(self.data[conf_key])):
return False
return True
@callback
def _configure_tls_assets(self):
"""Fill the tls asset locations in self.data."""
for asset_key, conf_key in FILE_MAPPING.items():
self.data[conf_key] = TLS_ASSET_TEMPLATE.format(self.bridge_id, asset_key)
async def async_step_import(self, import_info):
"""Import a new Caseta bridge as a config entry.
This flow is triggered by `async_setup`.
"""
host = import_info[CONF_HOST]
# Store the imported config for other steps in this flow to access.
self.data[CONF_HOST] = host
# Abort if existing entry with matching host exists.
self._async_abort_entries_match({CONF_HOST: self.data[CONF_HOST]})
self.data[CONF_KEYFILE] = import_info[CONF_KEYFILE]
self.data[CONF_CERTFILE] = import_info[CONF_CERTFILE]
self.data[CONF_CA_CERTS] = import_info[CONF_CA_CERTS]
if not (lutron_id := await self.async_get_lutron_id()):
# Ultimately we won't have a dedicated step for import failure, but
# in order to keep configuration.yaml-based configs transparently
# working without requiring further actions from the user, we don't
# display a form at all before creating a config entry in the
# default case, so we're only going to show a form in case the
# import fails.
# This will change in an upcoming release where UI-based config flow
# will become the default for the Lutron Caseta integration (which
# will require users to go through a confirmation flow for imports).
return await self.async_step_import_failed()
await self.async_set_unique_id(lutron_id, raise_on_progress=False)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=ENTRY_DEFAULT_TITLE, data=self.data)
async def async_step_import_failed(self, user_input=None):
"""Make failed import surfaced to user."""
self.context["title_placeholders"] = {CONF_NAME: self.data[CONF_HOST]}
if user_input is None:
return self.async_show_form(
step_id=STEP_IMPORT_FAILED,
description_placeholders={"host": self.data[CONF_HOST]},
errors={"base": ERROR_CANNOT_CONNECT},
)
return self.async_abort(reason=ABORT_REASON_CANNOT_CONNECT)
async def async_get_lutron_id(self) -> str | None:
"""Check if we can connect to the bridge with the current config."""
try:
bridge = Smartbridge.create_tls(
hostname=self.data[CONF_HOST],
keyfile=self.hass.config.path(self.data[CONF_KEYFILE]),
certfile=self.hass.config.path(self.data[CONF_CERTFILE]),
ca_certs=self.hass.config.path(self.data[CONF_CA_CERTS]),
)
except ssl.SSLError:
_LOGGER.error(
"Invalid certificate used to connect to bridge at %s",
self.data[CONF_HOST],
)
return None
try:
async with async_timeout.timeout(BRIDGE_TIMEOUT):
await bridge.connect()
except asyncio.TimeoutError:
_LOGGER.error(
"Timeout while trying to connect to bridge at %s",
self.data[CONF_HOST],
)
else:
if not bridge.is_connected():
return None
devices = bridge.get_devices()
bridge_device = devices[BRIDGE_DEVICE_ID]
return hex(bridge_device["serial"])[2:].zfill(8)
finally:
await bridge.close()
return None