Compare commits

...
Author SHA1 Message Date
J. Nick Koston 35769e7939 Merge remote-tracking branch 'origin/dev' into HEAD 2026-11-14 08:38:23 -06:00
J. Nick Koston c622c27baa Merge remote-tracking branch 'origin/dev' into cv-time-period-enum 2026-11-13 09:26:07 -06:00
J. Nick Koston a6456f212c Merge remote-tracking branch 'origin/dev' into cv-time-period-enum 2026-11-05 20:42:35 -06:00
J. Nick Koston 9c9758c1cc Merge remote-tracking branch 'origin/dev' into cv-time-period-enum
# Conflicts:
#	tests/script/test_build_language_schema.py
2026-11-04 10:37:19 -06:00
J. Nick Koston 7c6b1f5a02 [core] List time_period_enum options with their unit in the error 2026-10-30 06:14:08 -10:00
J. Nick Koston 932cff03b2 Annotate the time_period_enum validator 2026-10-30 01:10:30 -10:00
J. Nick Koston 9ed6d3fc21 [core] Add cv.time_period_enum
Validates a time period that must be one of a fixed set of values and
lists those values in the language schema.
2026-10-30 00:46:35 -10:00
3 changed files with 81 additions and 0 deletions

No files matched your search

+32
View File
@@ -2115,6 +2115,38 @@ def enum(mapping, **kwargs):
return validator
_TIME_PERIOD_TO_INT: dict[str, Callable[[typing.Any], int]] = {
"ms": lambda value: positive_time_period_milliseconds(value).total_milliseconds,
"us": lambda value: positive_time_period_microseconds(value).total_microseconds,
}
def time_period_enum(
mapping: dict[int, typing.Any], unit: typing.Literal["ms", "us"] = "ms"
) -> Callable[[typing.Any], core.EnumValue]:
"""Validate a time period that must be one of the keys of ``mapping``.
The keys are whole numbers of ``unit``. Returns the same value as
``enum(mapping, int=True)`` given that number.
"""
to_int = _TIME_PERIOD_TO_INT[unit]
enum_validator = enum(mapping, int=True)
@schema_extractor("enum")
def validator(value: typing.Any) -> core.EnumValue | dict[str, None]:
if value is SCHEMA_EXTRACT:
return {f"{key}{unit}": None for key in mapping}
number = to_int(value)
if number not in mapping:
options = ", ".join(f"{key}{unit}" for key in mapping)
raise Invalid(
f"Unknown value '{number}{unit}', valid options are {options}"
)
return enum_validator(number)
return validator
LAMBDA_ENTITY_ID_PROG = re.compile(r"\Wid\(\s*([a-zA-Z0-9_]+\.[.a-zA-Z0-9_]+)\s*\)")
@@ -455,6 +455,16 @@ def test_lvgl_style_schemas_are_named_and_deduped(lvgl_schema: dict) -> None:
assert refs > 100, f"STYLE_SCHEMA should be referenced via extends, got {refs}"
def test_convert_emits_time_period_enum() -> None:
"""cv.time_period_enum is dumped as an enum of time periods with their unit."""
validator = cv.time_period_enum({100: "A", 200: "B"}, unit="us")
config_var: dict = {}
_bls.convert(validator, config_var, "/test")
assert config_var["type"] == "enum"
assert config_var["values"] == {"100us": None, "200us": None}
def test_convert_typed_schema_with_an_empty_type(
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -2787,6 +2787,45 @@ def test_enum_valid_with_hyphen() -> None:
assert result.enum_value == 1
@pytest.mark.parametrize(
("unit", "value", "expected"),
[
("ms", "100ms", 100),
("ms", "0.2s", 200),
("ms", {"milliseconds": 200}, 200),
("us", "1100 us", 1100),
("us", "1.1ms", 1100),
],
)
def test_time_period_enum_valid(unit: str, value: object, expected: int) -> None:
mapping = {100: "A", 200: "B", 1100: "C"}
result = cv.time_period_enum(mapping, unit=unit)(value)
reference = cv.enum(mapping, int=True)(expected)
assert result == reference
assert type(result) is type(reference)
assert result.enum_value == reference.enum_value
@pytest.mark.parametrize(
("unit", "value", "match"),
[
("ms", "150ms", "Unknown value '150ms', valid options are 100ms, 200ms"),
("ms", "1100us", "Maximum precision is milliseconds"),
("ms", "-100ms", "value must be at least"),
("ms", 100, "has no time"),
("us", "100ns", "Maximum precision is microseconds"),
],
)
def test_time_period_enum_invalid(unit: str, value: object, match: str) -> None:
with pytest.raises(Invalid, match=match):
cv.time_period_enum({100: "A", 200: "B"}, unit=unit)(value)
def test_time_period_enum_schema_extract() -> None:
extracted = cv.time_period_enum({100: "A", 200: "B"})(SCHEMA_EXTRACT)
assert list(extracted) == ["100ms", "200ms"]
# ---------------------------------------------------------------------------
# lambda_ / returning_lambda
# ---------------------------------------------------------------------------