mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 08:50:22 +00:00
[schema] Emit unit for float fields; leave union validators untyped
Address review feedback:
- float_with_unit fields now dump as type "float" with the canonical unit
(e.g. {"type": "float", "unit": "Hz"}) rather than the quantity name as the
type. This keeps the type vocabulary closed and lets editors label the field
with its unit directly instead of mapping each quantity name. The unit is the
first alternative of the accepted-units regex; current/voltage list A/V first
(no validation change).
- Drop scalar typing of validate_bytes and date_time: both accept multiple YAML
forms (unit-suffixed strings or numbers, and a formatted string or a mapping)
that a single scalar type would wrongly reject in the editor.
This commit is contained in:
@@ -1642,31 +1642,19 @@ def test_templatable_schema_extract() -> None:
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("validator", "quantity"),
|
||||
("validator", "unit"),
|
||||
[
|
||||
(cv.float_with_unit("frequency", "(Hz)?"), "frequency"),
|
||||
(cv.frequency, "frequency"),
|
||||
(cv.voltage, "voltage"),
|
||||
(cv.decibel, "decibel"),
|
||||
(cv.float_with_unit("frequency", "(Hz|HZ|hz)?"), "Hz"),
|
||||
(cv.frequency, "Hz"),
|
||||
(cv.voltage, "V"),
|
||||
(cv.current, "A"),
|
||||
(cv.decibel, "dB"),
|
||||
],
|
||||
)
|
||||
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_float_with_unit_schema_extract(validator: object, unit: str) -> None:
|
||||
# For the SCHEMA_EXTRACT sentinel the validator returns its canonical unit
|
||||
# (not the parsed value) so build_language_schema can label the float field.
|
||||
assert validator(SCHEMA_EXTRACT) == unit
|
||||
|
||||
|
||||
def test_templatable_lambda() -> None:
|
||||
|
||||
Reference in New Issue
Block a user