From 2b0eaff884e8d343aa0b0aed7cb939d064d9b573 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 23 Sep 2020 12:17:51 +0200 Subject: [PATCH] validate options --- .../adaptive_lighting/config_flow.py | 28 +++++++++++++++++-- .../adaptive_lighting/strings.json | 2 +- .../adaptive_lighting/translations/en.json | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/custom_components/adaptive_lighting/config_flow.py b/custom_components/adaptive_lighting/config_flow.py index 6a340cf9..f505022f 100644 --- a/custom_components/adaptive_lighting/config_flow.py +++ b/custom_components/adaptive_lighting/config_flow.py @@ -86,8 +86,30 @@ class OptionsFlowHandler(config_entries.OptionsFlow): async def async_step_init(self, user_input=None): """Handle options flow.""" + errors = {} if user_input is not None: - return self.async_create_entry(title="", data=user_input) + for key, validate in [ + (CONF_SUNRISE_TIME, cv.time), + (CONF_SUNSET_TIME, cv.time), + (CONF_SUNRISE_OFFSET, cv.time_period), + (CONF_SUNSET_OFFSET, cv.time_period), + (CONF_INTERVAL, cv.time_period), + (CONF_DISABLE_ENTITY, cv.entity_id), + (CONF_SLEEP_ENTITY, cv.entity_id), + (CONF_DISABLE_STATE, vol.All(cv.ensure_list_csv, [cv.string])), + (CONF_SLEEP_STATE, vol.All(cv.ensure_list_csv, [cv.string])), + ]: + try: + value = user_input.get(key) + if value == FAKE_NONE: + value = None + if value is not None: + validate(user_input[key]) + except vol.Invalid: + _LOGGER.exception("Configuration option %s=%s is incorrect", key, value) + errors["base"] = "option_error" + if not errors: + return self.async_create_entry(title="", data=user_input) options = self.config_entry.options @@ -160,4 +182,6 @@ class OptionsFlowHandler(config_entries.OptionsFlow): } ) - return self.async_show_form(step_id="init", data_schema=options_schema) + return self.async_show_form( + step_id="init", data_schema=options_schema, errors=errors + ) diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index 9fc0df8d..0e02634a 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -47,7 +47,7 @@ } }, "error": { - "retrive_error": "Error retriving servers list" + "option_error": "Invalid option" } } } diff --git a/custom_components/adaptive_lighting/translations/en.json b/custom_components/adaptive_lighting/translations/en.json index 9fc0df8d..0e02634a 100644 --- a/custom_components/adaptive_lighting/translations/en.json +++ b/custom_components/adaptive_lighting/translations/en.json @@ -47,7 +47,7 @@ } }, "error": { - "retrive_error": "Error retriving servers list" + "option_error": "Invalid option" } } }