mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 03:16:37 +00:00
[schema] Type config_validation validators and range bounds in language schema dump
The language schema dump left many config_validation validators untyped (icon, mac_address, percentage, update_interval, lambdas, encryption keys, ...), so the visual editor and dashboard could not tell what YAML those fields accept. Type them via convert() and schema_extractor decorators, emit float_with_unit quantities (frequency, voltage, current, ...) as the field type, and attach min/max bounds detected from range validators next to the type.
This commit is contained in:
@@ -1641,6 +1641,34 @@ def test_templatable_schema_extract() -> None:
|
||||
assert cv.templatable(cv.int_)(SCHEMA_EXTRACT) is cv.int_
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("validator", "quantity"),
|
||||
[
|
||||
(cv.float_with_unit("frequency", "(Hz)?"), "frequency"),
|
||||
(cv.frequency, "frequency"),
|
||||
(cv.voltage, "voltage"),
|
||||
(cv.decibel, "decibel"),
|
||||
],
|
||||
)
|
||||
def test_float_with_unit_schema_extract(validator: object, quantity: str) -> None:
|
||||
# For the SCHEMA_EXTRACT sentinel the validator returns its quantity name
|
||||
# (not the parsed value) so build_language_schema can type the field by
|
||||
# quantity (e.g. "frequency") instead of a bare float.
|
||||
assert validator(SCHEMA_EXTRACT) == quantity
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"validator",
|
||||
[
|
||||
cv.date_time(date=True, time=False),
|
||||
cv.date_time(date=False, time=True),
|
||||
cv.date_time(date=True, time=True),
|
||||
],
|
||||
)
|
||||
def test_date_time_schema_extract(validator: object) -> None:
|
||||
assert validator(SCHEMA_EXTRACT) is None
|
||||
|
||||
|
||||
def test_templatable_lambda() -> None:
|
||||
result = cv.templatable(cv.int_)(Lambda("return 5;"))
|
||||
assert isinstance(result, Lambda)
|
||||
|
||||
Reference in New Issue
Block a user