forked from joakimjalden/frame_art
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_flow.py
More file actions
104 lines (85 loc) · 3.46 KB
/
config_flow.py
File metadata and controls
104 lines (85 loc) · 3.46 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
"""Config flow for Samsung Frame Art integration."""
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv
from .const import DOMAIN, CONF_HOST, CONF_NAME, CONF_TIMEOUT, DEFAULT_TIMEOUT
class FrameArtConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Samsung Frame Art."""
VERSION = 1
async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle the initial step."""
errors = {}
if user_input is not None:
# Validate input
host = user_input[CONF_HOST]
try:
# Example: Validate the host or IP address here
if not self._is_valid_host(host):
errors["base"] = "invalid_host"
else:
# Check if the host is already configured
await self.async_set_unique_id(host)
self._abort_if_unique_id_configured()
# Create the config entry
return self.async_create_entry(
title=user_input.get(CONF_NAME, host),
data={
CONF_HOST: host,
CONF_NAME: user_input.get(CONF_NAME, host),
CONF_TIMEOUT: user_input.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
},
)
except Exception:
errors["base"] = "connection_failed"
# Default form schema
data_schema = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Optional(CONF_NAME): str,
vol.Optional(CONF_TIMEOUT): cv.positive_int,
}
)
return self.async_show_form(
step_id="user", data_schema=data_schema, errors=errors
)
def _is_valid_host(self, host: str) -> bool:
"""Validate the host (e.g., an IP address or hostname)."""
# Placeholder validation logic, you can improve this
import socket
try:
socket.gethostbyname(host)
return True
except socket.error:
return False
@staticmethod
@callback
def async_get_options_flow(config_entry: config_entries.ConfigEntry):
"""Return the options flow handler."""
return FrameArtOptionsFlow(config_entry)
class FrameArtOptionsFlow(config_entries.OptionsFlow):
"""Handle options flow for Samsung Frame Art."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None) -> FlowResult:
"""Manage the options."""
errors = {}
if user_input is not None:
# Update the config entry options
return self.async_create_entry(title="", data=user_input)
# Default options schema
options_schema = vol.Schema(
{
vol.Optional(
CONF_TIMEOUT,
default=self.config_entry.options.get(
CONF_TIMEOUT, DEFAULT_TIMEOUT
),
): cv.positive_int,
}
)
return self.async_show_form(
step_id="init", data_schema=options_schema, errors=errors
)