mirror of
https://github.com/esphome/esphome.git
synced 2025-07-27 13:46:48 +00:00
[lvgl] Improve error messages from text validation (#8872)
This commit is contained in:
parent
090feb55e9
commit
d1e55252d0
@ -36,29 +36,43 @@ from .types import (
|
|||||||
# this will be populated later, in __init__.py to avoid circular imports.
|
# this will be populated later, in __init__.py to avoid circular imports.
|
||||||
WIDGET_TYPES: dict = {}
|
WIDGET_TYPES: dict = {}
|
||||||
|
|
||||||
|
TIME_TEXT_SCHEMA = cv.Schema(
|
||||||
|
{
|
||||||
|
cv.Required(CONF_TIME_FORMAT): cv.string,
|
||||||
|
cv.GenerateID(CONF_TIME): cv.templatable(cv.use_id(RealTimeClock)),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
PRINTF_TEXT_SCHEMA = cv.All(
|
||||||
|
cv.Schema(
|
||||||
|
{
|
||||||
|
cv.Required(CONF_FORMAT): cv.string,
|
||||||
|
cv.Optional(CONF_ARGS, default=list): cv.ensure_list(cv.lambda_),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
validate_printf,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_text(value):
|
||||||
|
"""
|
||||||
|
Do some sanity checking of the format to get better error messages
|
||||||
|
than using cv.Any
|
||||||
|
"""
|
||||||
|
if value is None:
|
||||||
|
raise cv.Invalid("No text specified")
|
||||||
|
if isinstance(value, dict):
|
||||||
|
if CONF_TIME_FORMAT in value:
|
||||||
|
return TIME_TEXT_SCHEMA(value)
|
||||||
|
return PRINTF_TEXT_SCHEMA(value)
|
||||||
|
|
||||||
|
return cv.templatable(cv.string)(value)
|
||||||
|
|
||||||
|
|
||||||
# A schema for text properties
|
# A schema for text properties
|
||||||
TEXT_SCHEMA = cv.Schema(
|
TEXT_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
cv.Optional(CONF_TEXT): cv.Any(
|
cv.Optional(CONF_TEXT): _validate_text,
|
||||||
cv.All(
|
|
||||||
cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Required(CONF_FORMAT): cv.string,
|
|
||||||
cv.Optional(CONF_ARGS, default=list): cv.ensure_list(
|
|
||||||
cv.lambda_
|
|
||||||
),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
validate_printf,
|
|
||||||
),
|
|
||||||
cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Required(CONF_TIME_FORMAT): cv.string,
|
|
||||||
cv.GenerateID(CONF_TIME): cv.templatable(cv.use_id(RealTimeClock)),
|
|
||||||
}
|
|
||||||
),
|
|
||||||
cv.templatable(cv.string),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user