-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_flow.py
More file actions
107 lines (87 loc) · 3.56 KB
/
config_flow.py
File metadata and controls
107 lines (87 loc) · 3.56 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
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
"""Config flow for Extron integration."""
import logging
from typing import Any
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.selector import selector
from pyextron import AuthenticationError, DeviceType, ExtronDevice
from .const import (
CONF_DEVICE_TYPE,
CONF_HOST,
CONF_PASSWORD,
CONF_PORT,
DOMAIN,
EXTRON_DEVICE_TIMEOUT_SECONDS,
OPTION_INPUT_NAMES,
)
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_PORT, default=23): int,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_DEVICE_TYPE): selector(
{
"select": {
"options": [
{"label": "HDMI Switcher", "value": DeviceType.HDMI_SWITCHER.value},
{"label": "Surround Sound Processor", "value": DeviceType.SURROUND_SOUND_PROCESSOR.value},
]
}
}
),
}
)
class ExtronConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Extron."""
VERSION = 1
async def async_step_user(self, user_input: dict[str, Any] | None = None):
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
# Try to connect to the device
extron_device = ExtronDevice(
user_input["host"],
user_input["port"],
user_input["password"],
timeout=EXTRON_DEVICE_TIMEOUT_SECONDS,
)
await extron_device.connect()
# Make a title for the entry
model_name = await extron_device.query_model_name()
title = f"Extron {model_name}"
# Make a unique ID for the entry, prevent adding the same device twice
unique_id = format_mac(await extron_device.query_mac_address())
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
# Disconnect, we'll connect again later, this was just for validation
await extron_device.disconnect()
except AuthenticationError:
errors["base"] = "invalid_auth"
except (BrokenPipeError, ConnectionError, OSError): # all technically OSError
errors["base"] = "cannot_connect"
else:
return self.async_create_entry(title=title, data=user_input)
return self.async_show_form(step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors)
@staticmethod
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
return ExtronOptionsFlowHandler()
class ExtronOptionsFlowHandler(OptionsFlow):
def __init__(self) -> None:
pass
async def async_step_init(self, user_input: dict[str, Any] | None = None):
"""Manage optional settings for the entry."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
OPTION_INPUT_NAMES, default=self.config_entry.options.get(OPTION_INPUT_NAMES)
): selector({"text": {"multiple": True}}),
}
),
)