mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-12 23:04:03 +02:00
validate options
This commit is contained in:
parent
575b81cbc5
commit
2b0eaff884
3 changed files with 28 additions and 4 deletions
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@
|
|||
}
|
||||
},
|
||||
"error": {
|
||||
"retrive_error": "Error retriving servers list"
|
||||
"option_error": "Invalid option"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@
|
|||
}
|
||||
},
|
||||
"error": {
|
||||
"retrive_error": "Error retriving servers list"
|
||||
"option_error": "Invalid option"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue