forked from wlcrs/huawei_solar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_flow.py
507 lines (418 loc) · 17.4 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""Config flow for Huawei Solar integration."""
from __future__ import annotations
import logging
from typing import Any
import serial.tools.list_ports
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import usb
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
CONF_PORT,
CONF_TYPE,
CONF_USERNAME,
)
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv
from huawei_solar import (
ConnectionException,
HuaweiSolarBridge,
HuaweiSolarException,
InvalidCredentials,
ReadException,
)
from .const import (
CONF_ENABLE_PARAMETER_CONFIGURATION,
CONF_SLAVE_IDS,
DEFAULT_PORT,
DEFAULT_SERIAL_SLAVE_ID,
DEFAULT_SLAVE_ID,
DEFAULT_USERNAME,
DOMAIN,
)
_LOGGER = logging.getLogger(__name__)
STEP_SETUP_NETWORK_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Required(CONF_SLAVE_IDS, default=str(DEFAULT_SLAVE_ID)): str,
vol.Required(CONF_ENABLE_PARAMETER_CONFIGURATION, default=False): bool,
}
)
STEP_LOGIN_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
)
CONF_MANUAL_PATH = "Enter Manually"
async def validate_serial_setup(data: dict[str, Any]) -> dict[str, Any]:
"""Validate the serial device that was passed by the user."""
bridge = None
try:
bridge = await HuaweiSolarBridge.create_rtu(
port=data[CONF_PORT],
slave_id=data[CONF_SLAVE_IDS][0],
)
_LOGGER.info(
"Successfully connected to inverter %s with SN %s",
bridge.model_name,
bridge.serial_number,
)
result = {
"model_name": bridge.model_name,
"serial_number": bridge.serial_number,
}
# Also validate the other slave-ids
for slave_id in data[CONF_SLAVE_IDS][1:]:
try:
slave_bridge = await HuaweiSolarBridge.create_extra_slave(
bridge, slave_id
)
_LOGGER.info(
"Successfully connected to slave inverter %s: %s with SN %s",
slave_id,
slave_bridge.model_name,
slave_bridge.serial_number,
)
except HuaweiSolarException as err:
_LOGGER.error("Could not connect to slave %s", slave_id)
raise SlaveException(f"Could not connect to slave {slave_id}") from err
# Return info that you want to store in the config entry.
return result
finally:
if bridge is not None:
# Cleanup this inverter object explicitly to prevent it from trying to maintain a modbus connection
await bridge.stop()
async def validate_network_setup(data: dict[str, Any]) -> dict[str, Any]:
"""Validate the user input allows us to connect.
Data has the keys from STEP_SETUP_NETWORK_DATA_SCHEMA with values provided by the user.
"""
bridge = None
try:
bridge = await HuaweiSolarBridge.create(
host=data[CONF_HOST],
port=data[CONF_PORT],
slave_id=data[CONF_SLAVE_IDS][0],
)
_LOGGER.info(
"Successfully connected to inverter %s with SN %s",
bridge.model_name,
bridge.serial_number,
)
result = {
"model_name": bridge.model_name,
"serial_number": bridge.serial_number,
}
if data[CONF_ENABLE_PARAMETER_CONFIGURATION]:
# Check if we have write access. If this is not the case, we will
# need to login (and request the username/password from the user to be
# able to do this).
result["has_write_permission"] = await bridge.has_write_permission()
# Also validate the other slave-ids
for slave_id in data[CONF_SLAVE_IDS][1:]:
try:
slave_bridge = await HuaweiSolarBridge.create_extra_slave(
bridge, slave_id
)
_LOGGER.info(
"Successfully connected to slave inverter %s: %s with SN %s",
slave_id,
slave_bridge.model_name,
slave_bridge.serial_number,
)
except HuaweiSolarException as err:
_LOGGER.error("Could not connect to slave %s", slave_id)
raise SlaveException(f"Could not connect to slave {slave_id}") from err
# Return info that you want to store in the config entry.
return result
finally:
if bridge:
# Cleanup this inverter object explicitly to prevent it from trying to maintain a modbus connection
await bridge.stop()
async def validate_network_setup_login(
host: str, port: int, slave_id: int, login_data: dict[str, Any]
) -> bool:
"""Verify the installer username/password and test if it can perform a write-operation."""
bridge = None
try:
# these parameters have already been tested in validate_input, so they should work fine!
bridge = await HuaweiSolarBridge.create(
host=host,
port=port,
slave_id=slave_id,
)
await bridge.login(login_data[CONF_USERNAME], login_data[CONF_PASSWORD])
# verify that we have write-permission now
return await bridge.has_write_permission()
except InvalidCredentials:
return False
finally:
if bridge is not None:
# Cleanup this inverter object explicitly to prevent it from trying to maintain a modbus connection
await bridge.stop()
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Huawei Solar."""
VERSION = 1
def __init__(self) -> None:
"""Initialize flow."""
self._host: str | None = None
self._port: int | None = None
self._slave_ids: list[int] | None = None
self._enable_parameter_configuration = False
self._inverter_info: dict | None = None
self._username: str | None = None
self._password: str | None = None
# Only used in reauth flows:
self._reauth_entry: config_entries.ConfigEntry | None = None
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Step when user initializes a integration."""
if user_input is not None:
user_selection = user_input[CONF_TYPE]
if user_selection == "Serial":
return await self.async_step_setup_serial()
return await self.async_step_setup_network()
list_of_types = ["Serial", "Network"]
schema = vol.Schema({vol.Required(CONF_TYPE): vol.In(list_of_types)})
return self.async_show_form(step_id="user", data_schema=schema)
async def async_step_setup_serial(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle connection parameters when using ModbusRTU."""
# Parameter configuration is always possible over serial connection
self._enable_parameter_configuration = True
errors = {}
if user_input is not None:
user_selection = user_input[CONF_PORT]
if user_selection == CONF_MANUAL_PATH:
self._slave_ids = user_input[CONF_SLAVE_IDS]
return await self.async_step_setup_serial_manual_path()
user_input[CONF_PORT] = await self.hass.async_add_executor_job(
usb.get_serial_by_id, user_input[CONF_PORT]
)
try:
user_input[CONF_SLAVE_IDS] = list(
map(int, user_input[CONF_SLAVE_IDS].split(","))
)
except ValueError:
errors["base"] = "invalid_slave_ids"
else:
try:
info = await validate_serial_setup(user_input)
except ConnectionException:
errors["base"] = "cannot_connect"
except SlaveException:
errors["base"] = "slave_cannot_connect"
except ReadException:
errors["base"] = "read_error"
except Exception as exception: # pylint: disable=broad-except
_LOGGER.exception(exception)
errors["base"] = "unknown"
else:
await self.async_set_unique_id(info["serial_number"])
self._abort_if_unique_id_configured(
updates={
CONF_HOST: None,
CONF_PORT: user_input[CONF_PORT],
CONF_SLAVE_IDS: user_input[CONF_SLAVE_IDS],
}
)
self._port = user_input[CONF_PORT]
self._slave_ids = user_input[CONF_SLAVE_IDS]
self._inverter_info = info
self.context["title_placeholders"] = {"name": info["model_name"]}
# We can directly make the new entry
return await self._create_entry()
ports = await self.hass.async_add_executor_job(serial.tools.list_ports.comports)
list_of_ports = {
port.device: usb.human_readable_device_name(
port.device,
port.serial_number,
port.manufacturer,
port.description,
port.vid,
port.pid,
)
for port in ports
}
list_of_ports[CONF_MANUAL_PATH] = CONF_MANUAL_PATH
schema = vol.Schema(
{
vol.Required(CONF_PORT): vol.In(list_of_ports),
vol.Required(CONF_SLAVE_IDS, default=str(DEFAULT_SERIAL_SLAVE_ID)): str,
}
)
return self.async_show_form(
step_id="setup_serial",
data_schema=schema,
errors=errors,
)
async def async_step_setup_serial_manual_path(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Select path manually."""
errors = {}
if user_input is not None:
try:
user_input[CONF_SLAVE_IDS] = list(
map(int, user_input[CONF_SLAVE_IDS].split(","))
)
except ValueError:
errors["base"] = "invalid_slave_ids"
else:
try:
info = await validate_serial_setup(user_input)
except ConnectionException:
errors["base"] = "cannot_connect"
except SlaveException:
errors["base"] = "slave_cannot_connect"
except ReadException:
errors["base"] = "read_error"
except Exception as exception: # pylint: disable=broad-except
_LOGGER.exception(exception)
errors["base"] = "unknown"
else:
await self.async_set_unique_id(info["serial_number"])
self._abort_if_unique_id_configured(
updates={
CONF_HOST: None,
CONF_PORT: user_input[CONF_PORT],
CONF_SLAVE_IDS: user_input[CONF_SLAVE_IDS],
}
)
self._port = user_input[CONF_PORT]
self._slave_ids = user_input[CONF_SLAVE_IDS]
self._inverter_info = info
self.context["title_placeholders"] = {"name": info["model_name"]}
# We can directly make the new entry
return await self._create_entry()
schema = vol.Schema(
{
vol.Required(CONF_PORT): str,
vol.Required(CONF_SLAVE_IDS, default=self._slave_ids): str,
}
)
return self.async_show_form(
step_id="setup_serial_manual_path", data_schema=schema, errors=errors
)
async def async_step_setup_network(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle connection parameters when using ModbusTCP."""
errors = {}
if user_input is not None:
try:
user_input[CONF_SLAVE_IDS] = list(
map(int, user_input[CONF_SLAVE_IDS].split(","))
)
except ValueError:
errors["base"] = "invalid_slave_ids"
else:
try:
info = await validate_network_setup(user_input)
except ConnectionException:
errors["base"] = "cannot_connect"
except SlaveException:
errors["base"] = "slave_cannot_connect"
except ReadException as exception:
_LOGGER.exception(exception)
errors["base"] = "read_error"
except Exception as exception: # pylint: disable=broad-except
_LOGGER.exception(exception)
errors["base"] = "unknown"
else:
await self.async_set_unique_id(info["serial_number"])
self._abort_if_unique_id_configured()
self._host = user_input[CONF_HOST]
self._port = user_input[CONF_PORT]
self._slave_ids = user_input[CONF_SLAVE_IDS]
self._enable_parameter_configuration = user_input[
CONF_ENABLE_PARAMETER_CONFIGURATION
]
self._inverter_info = info
self.context["title_placeholders"] = {"name": info["model_name"]}
# Check if we need to ask for the login details
if (
self._enable_parameter_configuration
# This can also be None, in which case login is not supported.
and info["has_write_permission"] is not None
and info["has_write_permission"] is False
):
return await self.async_step_network_login()
# Otherwise, we can directly create the device entry!
return await self._create_entry()
return self.async_show_form(
step_id="setup_network",
data_schema=STEP_SETUP_NETWORK_DATA_SCHEMA,
errors=errors,
)
async def async_step_network_login(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle username/password input."""
assert self._host is not None
assert self._port is not None
assert self._slave_ids is not None
errors = {}
if user_input is not None:
try:
login_success = await validate_network_setup_login(
self._host, self._port, self._slave_ids[0], user_input
)
if login_success:
self._username = user_input[CONF_USERNAME]
self._password = user_input[CONF_PASSWORD]
return await self._create_entry()
errors["base"] = "invalid_auth"
except ConnectionException:
errors["base"] = "cannot_connect"
except SlaveException:
errors["base"] = "slave_cannot_connect"
except ReadException as exception:
_LOGGER.exception(exception)
errors["base"] = "read_error"
except Exception as exception: # pylint: disable=broad-except
_LOGGER.exception(exception)
errors["base"] = "unknown"
return self.async_show_form(
step_id="network_login", data_schema=STEP_LOGIN_DATA_SCHEMA, errors=errors
)
async def _create_entry(self):
"""Create the entry."""
assert self._port is not None
assert self._slave_ids is not None
data = {
CONF_HOST: self._host,
CONF_PORT: self._port,
CONF_SLAVE_IDS: self._slave_ids,
CONF_ENABLE_PARAMETER_CONFIGURATION: self._enable_parameter_configuration,
CONF_USERNAME: self._username,
CONF_PASSWORD: self._password,
}
if self._reauth_entry:
self.hass.config_entries.async_update_entry(self._reauth_entry, data=data)
await self.hass.config_entries.async_reload(self._reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_create_entry(
title=self._inverter_info["model_name"], data=data
)
async def async_step_reauth(
self, config: dict[str, Any] | None = None
) -> FlowResult:
"""Perform reauth upon an login error."""
assert config is not None
self._host = config.get(CONF_HOST)
self._port = config.get(CONF_PORT)
self._slave_ids = config.get(CONF_SLAVE_IDS)
self._enable_parameter_configuration = config.get(
CONF_ENABLE_PARAMETER_CONFIGURATION, False
)
self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_network_login()
class SlaveException(Exception):
"""Error while testing communication with a slave."""