-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_flow.py
More file actions
77 lines (59 loc) · 2.32 KB
/
config_flow.py
File metadata and controls
77 lines (59 loc) · 2.32 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
72
73
74
75
76
77
"""Setup dvsportal."""
import logging
from typing import Any
from dvsportal import DVSPortal, DVSPortalAuthError
import voluptuous as vol
from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.data_entry_flow import AbortFlow
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Optional("user_agent"): str,
}
)
class InvalidAuth(exceptions.HomeAssistantError):
"""Error to indicate there is invalid auth."""
class DVSPortalConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for DVSPortal."""
VERSION = 1
async def async_step_user(self, user_input=None) -> config_entries.ConfigFlowResult:
"""Handle the initial step."""
errors = {}
if user_input is not None:
try:
info = await self.validate_input(user_input)
return self.async_create_entry(title=info["title"], data=user_input)
except InvalidAuth:
errors["base"] = "invalid_auth"
except AbortFlow:
raise
except Exception: # pylint: disable=broad-except
logging.exception("Error in async step user")
errors["base"] = "unknown"
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)
async def validate_input(self, data: dict[str, Any]):
"""Validate the user input allows us to connect."""
api_host = data[CONF_HOST]
identifier = data[CONF_USERNAME]
password = data[CONF_PASSWORD]
user_agent = data.get("user_agent", "HomeAssistant")
await self.async_set_unique_id(f"{api_host}.{identifier}")
self._abort_if_unique_id_configured()
try:
async with DVSPortal(
api_host=api_host,
identifier=identifier,
password=password,
user_agent=user_agent,
) as dvs_portal:
await dvs_portal.token()
except DVSPortalAuthError:
raise InvalidAuth from None
return {"title": identifier}