diff --git a/custom_components/adaptive_lighting/__init__.py b/custom_components/adaptive_lighting/__init__.py index c4a91076..99ed5563 100755 --- a/custom_components/adaptive_lighting/__init__.py +++ b/custom_components/adaptive_lighting/__init__.py @@ -34,7 +34,7 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from .const import CONF_NAME, DOMAIN, UNDO_UPDATE_LISTENER, get_domain_schema +from .const import _DOMAIN_SCHEMA, CONF_NAME, DOMAIN, UNDO_UPDATE_LISTENER _LOGGER = logging.getLogger(__name__) @@ -49,13 +49,8 @@ def _all_unique_profiles(value): return value -_DOMAIN_SCHEMA = get_domain_schema(yaml=True) CONFIG_SCHEMA = vol.Schema( - { - DOMAIN: vol.All( - cv.ensure_list, [vol.Schema(_DOMAIN_SCHEMA)], _all_unique_profiles - ) - }, + {DOMAIN: vol.All(cv.ensure_list, [_DOMAIN_SCHEMA], _all_unique_profiles)}, extra=vol.ALLOW_EXTRA, ) diff --git a/custom_components/adaptive_lighting/config_flow.py b/custom_components/adaptive_lighting/config_flow.py index 097ea5be..c9da026d 100644 --- a/custom_components/adaptive_lighting/config_flow.py +++ b/custom_components/adaptive_lighting/config_flow.py @@ -8,13 +8,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant import config_entries from homeassistant.core import callback -from .const import ( - DOMAIN, - EXTRA_VALIDATION, - FAKE_NONE, - VALIDATION_TUPLES, - get_domain_schema, -) +from .const import DOMAIN, EXTRA_VALIDATION, FAKE_NONE, VALIDATION_TUPLES _LOGGER = logging.getLogger(__name__) @@ -41,9 +35,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): async def async_step_import(self, user_input=None): """Handle configuration by yaml file.""" - _DOMAIN_SCHEMA = get_domain_schema(yaml=False) - schema = {k: v for k, v in _DOMAIN_SCHEMA.items() if k in user_input} - vol.Schema(schema)(user_input) await self.async_set_unique_id(user_input["name"]) self._abort_if_unique_id_configured() return self.async_create_entry(title=user_input["name"], data=user_input) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index dd3c30e3..86e5b9d8 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -74,7 +74,8 @@ def join_strings(lst): return ",".join(lst) -# these validators cannot be serialized +# conf_option: (validator, coerce) tuples +# these validators cannot be serialized but can be serialized when coerced by coerce. EXTRA_VALIDATION = { CONF_DISABLE_ENTITY: (cv.entity_id, str), CONF_DISABLE_STATE: (vol.All(cv.ensure_list_csv, [cv.string]), join_strings), @@ -88,27 +89,25 @@ EXTRA_VALIDATION = { } -def get_domain_schema(with_fake_none=False, yaml=False): - def get_validation(key, validation): - validation, coerce = EXTRA_VALIDATION.get(key, (validation, None)) - return ( - vol.All(validation, vol.Coerce(coerce)) - if yaml and coerce is not None - else validation - ) +def maybe_coerse(key, validation): + validation, coerce = EXTRA_VALIDATION.get(key, (validation, None)) + if coerce is not None: + return vol.All(validation, vol.Coerce(coerce)) + return validation - validation_tuples = [ - (key, default, get_validation(key, validation)) - for key, default, validation in VALIDATION_TUPLES - ] - validation_tuples.append((CONF_NAME, DEFAULT_NAME, cv.string)) - def replace_none(x): - if not with_fake_none and x == FAKE_NONE: - return vol.UNDEFINED - return x +def replace_none(x): + return x if x != FAKE_NONE else vol.UNDEFINED - return { + +validation_tuples = [ + (key, default, maybe_coerse(key, validation)) + for key, default, validation in VALIDATION_TUPLES +] + [(CONF_NAME, DEFAULT_NAME, cv.string)] + +_DOMAIN_SCHEMA = vol.Schema( + { vol.Optional(key, default=replace_none(default)): validation for key, default, validation in validation_tuples } +)