generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_flow.py
More file actions
76 lines (63 loc) · 2.61 KB
/
config_flow.py
File metadata and controls
76 lines (63 loc) · 2.61 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
"""Adds config flow for PandaStatus."""
from __future__ import annotations
from typing import Any
import voluptuous as vol
from custom_components.panda_status import tools
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_NAME, CONF_URL
from homeassistant.helpers import selector
from .const import DOMAIN, LOGGER
from .websocket import PandaStatusWebsocketCommunicationError, PandaStatusWebsocketError
OPTIONS_SCHEMA = vol.Schema(
{
vol.Required(CONF_URL): selector.TextSelector(
selector.TextSelectorConfig(type=selector.TextSelectorType.TEXT)
),
vol.Optional(CONF_NAME): selector.TextSelector(
selector.TextSelectorConfig(type=selector.TextSelectorType.TEXT)
),
}
)
class PandaStatusFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for PandaStatus."""
VERSION = 1
MINOR_VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
errors = {}
if user_input is not None:
try:
# Try and get initial data to validate the connection
initial_data = await tools.test_credentials(
url=user_input.get(CONF_URL)
)
except PandaStatusWebsocketCommunicationError as exception:
LOGGER.error(exception)
errors["base"] = "connection"
except PandaStatusWebsocketError as exception:
LOGGER.exception(exception)
errors["base"] = "unknown"
else:
# Check if a config entry already exists
await self._async_handle_discovery_without_unique_id()
# Use tools to extract printer and device name
printer_name = tools.get_printer_name(initial_data=initial_data)
device_name = user_input.get(CONF_NAME, None) or tools.get_device_name(
initial_data=initial_data
)
# If user provided a name, use it; otherwise, use the device name
return self.async_create_entry(
title=device_name,
description=f"Panda status for {printer_name}",
data={
CONF_URL: user_input.get(CONF_URL),
CONF_NAME: device_name,
},
)
return self.async_show_form(
step_id="user",
data_schema=OPTIONS_SCHEMA,
errors=errors,
)