Set default mode for number selector to box (#148773)

This commit is contained in:
Paulus Schoutsen 2025-07-16 12:19:31 +02:00 committed by GitHub
parent ce4a811b96
commit 29e105b0ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 5 deletions

View File

@ -1108,10 +1108,12 @@ class NumberSelectorMode(StrEnum):
def validate_slider(data: Any) -> Any: def validate_slider(data: Any) -> Any:
"""Validate configuration.""" """Validate configuration."""
if data["mode"] == "box": has_min_max = "min" in data and "max" in data
return data
if "min" not in data or "max" not in data: if "mode" not in data:
data["mode"] = "slider" if has_min_max else "box"
if data["mode"] == "slider" and not has_min_max:
raise vol.Invalid("min and max are required in slider mode") raise vol.Invalid("min and max are required in slider mode")
return data return data
@ -1134,7 +1136,7 @@ class NumberSelector(Selector[NumberSelectorConfig]):
"any", vol.All(vol.Coerce(float), vol.Range(min=1e-3)) "any", vol.All(vol.Coerce(float), vol.Range(min=1e-3))
), ),
vol.Optional(CONF_UNIT_OF_MEASUREMENT): str, vol.Optional(CONF_UNIT_OF_MEASUREMENT): str,
vol.Optional(CONF_MODE, default=NumberSelectorMode.SLIDER): vol.All( vol.Optional(CONF_MODE): vol.All(
vol.Coerce(NumberSelectorMode), lambda val: val.value vol.Coerce(NumberSelectorMode), lambda val: val.value
), ),
vol.Optional("translation_key"): str, vol.Optional("translation_key"): str,

View File

@ -427,6 +427,7 @@ def test_assist_pipeline_selector_schema(
({"mode": "box"}, (10,), ()), ({"mode": "box"}, (10,), ()),
({"mode": "box", "step": "any"}, (), ()), ({"mode": "box", "step": "any"}, (), ()),
({"mode": "slider", "min": 0, "max": 1, "step": "any"}, (), ()), ({"mode": "slider", "min": 0, "max": 1, "step": "any"}, (), ()),
({}, (), ()),
], ],
) )
def test_number_selector_schema(schema, valid_selections, invalid_selections) -> None: def test_number_selector_schema(schema, valid_selections, invalid_selections) -> None:
@ -434,10 +435,28 @@ def test_number_selector_schema(schema, valid_selections, invalid_selections) ->
_test_selector("number", schema, valid_selections, invalid_selections) _test_selector("number", schema, valid_selections, invalid_selections)
def test_number_selector_schema_default_mode() -> None:
"""Test number selector default mode set on min/max."""
assert selector.selector({"number": {"min": 10, "max": 50}}).config == {
"mode": "slider",
"min": 10.0,
"max": 50.0,
"step": 1.0,
}
assert selector.selector({"number": {}}).config == {
"mode": "box",
"step": 1.0,
}
assert selector.selector({"number": {"min": "10"}}).config == {
"mode": "box",
"min": 10.0,
"step": 1.0,
}
@pytest.mark.parametrize( @pytest.mark.parametrize(
"schema", "schema",
[ [
{}, # Must have mandatory fields
{"mode": "slider"}, # Must have min+max in slider mode {"mode": "slider"}, # Must have min+max in slider mode
], ],
) )