adaptive-lighting/custom_components/adaptive_lighting/config_flow.py

118 lines
4.1 KiB
Python
Raw Permalink Normal View History

2020-09-28 13:10:41 +02:00
"""Config flow for Adaptive Lighting integration."""
2020-09-12 23:25:36 +02:00
import logging
2020-09-12 23:25:36 +02:00
from homeassistant import config_entries
2020-10-05 23:41:57 +02:00
from homeassistant.const import CONF_NAME
2020-09-12 23:25:36 +02:00
from homeassistant.core import callback
2020-09-26 17:39:17 +02:00
import homeassistant.helpers.config_validation as cv
2022-08-28 15:08:03 -07:00
import voluptuous as vol
2020-09-12 23:25:36 +02:00
2020-09-27 16:21:42 +02:00
from .const import ( # pylint: disable=unused-import
CONF_LIGHTS,
DOMAIN,
EXTRA_VALIDATION,
NONE_STR,
VALIDATION_TUPLES,
)
from .switch import _supported_features, validate
2020-09-12 23:25:36 +02:00
_LOGGER = logging.getLogger(__name__)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Adaptive Lighting."""
VERSION = 1
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
if user_input is not None:
2020-10-05 23:41:57 +02:00
await self.async_set_unique_id(user_input[CONF_NAME])
2020-09-12 23:25:36 +02:00
self._abort_if_unique_id_configured()
2020-10-05 23:41:57 +02:00
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
2020-09-12 23:25:36 +02:00
return self.async_show_form(
step_id="user",
2020-10-05 23:41:57 +02:00
data_schema=vol.Schema({vol.Required(CONF_NAME): str}),
errors=errors,
)
2020-09-12 23:25:36 +02:00
2020-09-23 18:20:25 +02:00
async def async_step_import(self, user_input=None):
"""Handle configuration by yaml file."""
2020-10-05 23:41:57 +02:00
await self.async_set_unique_id(user_input[CONF_NAME])
2020-09-28 16:10:05 +02:00
for entry in self._async_current_entries():
if entry.unique_id == self.unique_id:
2020-09-29 13:13:08 +02:00
self.hass.config_entries.async_update_entry(entry, data=user_input)
self._abort_if_unique_id_configured()
2020-10-05 23:41:57 +02:00
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
2020-09-23 18:20:25 +02:00
2020-09-12 23:25:36 +02:00
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
2020-09-23 18:20:25 +02:00
def validate_options(user_input, errors):
"""Validate the options in the OptionsFlow.
This is an extra validation step because the validators
in `EXTRA_VALIDATION` cannot be serialized to json.
"""
2022-10-27 21:57:49 -07:00
for key, (_validate, _) in EXTRA_VALIDATION.items():
2020-09-23 18:20:25 +02:00
# these are unserializable validators
value = user_input.get(key)
2020-09-23 18:20:25 +02:00
try:
2020-09-24 23:54:14 +02:00
if value is not None and value != NONE_STR:
2022-10-27 21:57:49 -07:00
_validate(value)
2020-09-23 18:20:25 +02:00
except vol.Invalid:
_LOGGER.exception("Configuration option %s=%s is incorrect", key, value)
errors["base"] = "option_error"
2020-09-12 23:25:36 +02:00
class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a option flow for Adaptive Lighting."""
def __init__(self, config_entry: config_entries.ConfigEntry):
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
"""Handle options flow."""
conf = self.config_entry
data = validate(conf)
if conf.source == config_entries.SOURCE_IMPORT:
return self.async_show_form(step_id="init", data_schema=None)
2020-09-23 12:17:51 +02:00
errors = {}
2020-09-12 23:25:36 +02:00
if user_input is not None:
2020-09-23 18:20:25 +02:00
validate_options(user_input, errors)
2020-09-23 12:17:51 +02:00
if not errors:
2020-09-24 20:44:42 +02:00
return self.async_create_entry(title="", data=user_input)
2020-09-12 23:25:36 +02:00
2020-10-04 23:15:12 +02:00
all_lights = [
light
for light in self.hass.states.async_entity_ids("light")
if _supported_features(self.hass, light)
]
for configured_light in data[CONF_LIGHTS]:
2022-10-27 21:57:49 -07:00
if configured_light not in all_lights:
2022-10-29 23:58:03 +00:00
errors = {CONF_LIGHTS: "entity_missing"}
_LOGGER.error(
"%s: light entity %s is configured, but was not found",
data[CONF_NAME],
configured_light,
)
all_lights.append(configured_light)
2020-10-04 23:15:12 +02:00
to_replace = {CONF_LIGHTS: cv.multi_select(sorted(all_lights))}
2020-09-25 00:11:22 +02:00
options_schema = {}
for name, default, validation in VALIDATION_TUPLES:
key = vol.Optional(name, default=conf.options.get(name, default))
value = to_replace.get(name, validation)
2020-09-25 00:11:22 +02:00
options_schema[key] = value
2020-09-19 11:10:54 +02:00
2020-09-23 12:17:51 +02:00
return self.async_show_form(
2020-09-25 00:11:22 +02:00
step_id="init", data_schema=vol.Schema(options_schema), errors=errors
2020-09-23 12:17:51 +02:00
)