-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconfig_flow.py
More file actions
71 lines (58 loc) · 2.69 KB
/
Copy pathconfig_flow.py
File metadata and controls
71 lines (58 loc) · 2.69 KB
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
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL
import logging
import asyncio
from .coordinator import IndevoltAPI
_LOGGER = logging.getLogger(__name__)
class IndevoltConfigFlow(ConfigFlow, domain=DOMAIN):
"""Configuration flow for Indevolt integration."""
VERSION = 1
async def async_step_user(self, user_input=None):
"""
Handle the initial user configuration step.
This method is called when the user initiates the integration setup.
It presents a form for device connection parameters and validates them.
"""
errors = {}
if user_input is not None:
host = user_input["host"]
scan_interval = user_input.get("scan_interval", DEFAULT_SCAN_INTERVAL)
api = IndevoltAPI(host, DEFAULT_PORT, async_get_clientsession(self.hass))
try:
data = await api.get_config()
device = data.get("device", {})
device_model = device.get("type")
device_sn = device.get("sn")
if "SF2000" in device_model:
device_model = "SolidFlex/PowerFlex2000"
elif "BK1600" in device_model:
device_model = "BK1600/BK1600Ultra"
await self.async_set_unique_id(device_sn)
self._abort_if_unique_id_configured()
# Create configuration entry on successful connection.
return self.async_create_entry(
title=f"INDEVOLT {device_model} ({host})", # Entry title shown in HA UI.
data={
"host": host,
"port": DEFAULT_PORT,
"scan_interval": scan_interval,
"sn": device_sn,
"device_model": device_model,
"fw_version": device.get("f_ver")
}
)
except asyncio.TimeoutError:
errors["base"] = "timeout"
except Exception as e:
_LOGGER.error("Unknown error occurred while verifying device: %s", str(e), exc_info=True)
errors["base"] = "unknown"
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({
vol.Required("host"): str,
vol.Optional("scan_interval", default=DEFAULT_SCAN_INTERVAL): int,
}),
errors=errors
)