-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_flow.py
More file actions
62 lines (50 loc) · 2.12 KB
/
config_flow.py
File metadata and controls
62 lines (50 loc) · 2.12 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
from __future__ import annotations
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .api import StiebelDheApi
from .const import (
CONF_HOST,
CONF_NAME,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
DEFAULT_NAME,
DEFAULT_SCAN_INTERVAL_SECONDS,
DOMAIN,
)
async def _validate_input(hass: HomeAssistant, data: dict) -> dict:
session = async_get_clientsession(hass)
api = StiebelDheApi(
session=session,
host=data[CONF_HOST],
token=data[CONF_TOKEN],
name=data.get(CONF_NAME, DEFAULT_NAME),
)
rb = await api.get_setpoint()
return {"title": f"Stiebel DHE ({data.get(CONF_NAME, DEFAULT_NAME)})", "token": api.token, "setpoint": rb.setpoint_c}
class StiebelDheConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
async def async_step_user(self, user_input: dict | None = None):
errors: dict[str, str] = {}
if user_input is not None:
try:
info = await _validate_input(self.hass, user_input)
except Exception: # noqa: BLE001
errors["base"] = "cannot_connect"
else:
await self.async_set_unique_id(user_input[CONF_HOST])
self._abort_if_unique_id_configured()
entry_data = {**user_input, CONF_TOKEN: info["token"]}
options = {CONF_SCAN_INTERVAL: int(user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL_SECONDS))}
entry_data.pop(CONF_SCAN_INTERVAL, None)
return self.async_create_entry(title=info["title"], data=entry_data, options=options)
schema = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_TOKEN): str,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL_SECONDS): vol.Coerce(int),
}
)
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)