[schema] Unit-type number-with-unit validators; leave dimensions untyped

Address esphbot review: validators that accept a number optionally carrying a
unit (percentage, possibly_negative_percentage, temperature, temperature_delta,
color_temperature, percentage_int, validate_bytes) are dumped as their base
numeric type plus the canonical unit (e.g. {type: float, unit: "%"}), like
float_with_unit, so the editor accepts both the bare-number and the unit-string
form instead of rejecting one. cv.dimensions accepts a "WIDTHxHEIGHT" string or
a [w, h] list, so it is left untyped rather than misrepresented as a string.
This commit is contained in:
Jesse Hills
2026-09-14 13:52:22 +12:00
parent b40c497d3e
commit 59044484e2
2 changed files with 48 additions and 15 deletions
+16 -11
View File
@@ -152,17 +152,22 @@ _CV_STRING_VALIDATORS = (
cv.time_of_day,
cv.directory,
cv.file_,
cv.dimensions,
cv.none,
)
_CV_INTEGER_VALIDATORS = (cv.hex_int, cv.percentage_int, cv.mqtt_qos)
_CV_FLOAT_VALIDATORS = (
cv.percentage,
cv.possibly_negative_percentage,
cv.temperature,
cv.temperature_delta,
cv.color_temperature,
)
_CV_INTEGER_VALIDATORS = (cv.hex_int, cv.mqtt_qos)
# Validators that accept a number optionally carrying a unit suffix (e.g.
# "50%", "25°C", "10kB"). Dumped as their base numeric type with the canonical
# unit attached, mirroring float_with_unit, so editors accept both the
# bare-number and the unit-string form instead of rejecting one of them.
_CV_UNIT_VALIDATORS = {
cv.percentage: ("float", "%"),
cv.possibly_negative_percentage: ("float", "%"),
cv.temperature: ("float", "°C"),
cv.temperature_delta: ("float", "°C"),
cv.color_temperature: ("float", "mireds"),
cv.percentage_int: ("integer", "%"),
cv.validate_bytes: ("integer", "B"),
}
_CV_TIME_VALIDATORS = (
cv.update_interval,
cv.time_period_str_unit,
@@ -1051,8 +1056,8 @@ def convert(schema, config_var, path):
schema in _CV_STRING_VALIDATORS
):
config_var[S_TYPE] = "string"
elif schema in _CV_FLOAT_VALIDATORS:
config_var[S_TYPE] = "float"
elif callable(schema) and schema in _CV_UNIT_VALIDATORS:
config_var[S_TYPE], config_var["unit"] = _CV_UNIT_VALIDATORS[schema]
elif schema in _CV_TIME_VALIDATORS:
config_var[S_TYPE] = "time"
elif schema in _CV_LAMBDA_VALIDATORS: