mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Set default mode for number selector to box (#148773)
This commit is contained in:
parent
ce4a811b96
commit
29e105b0ef
@ -1108,10 +1108,12 @@ class NumberSelectorMode(StrEnum):
|
||||
|
||||
def validate_slider(data: Any) -> Any:
|
||||
"""Validate configuration."""
|
||||
if data["mode"] == "box":
|
||||
return data
|
||||
has_min_max = "min" in data and "max" in 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")
|
||||
|
||||
return data
|
||||
@ -1134,7 +1136,7 @@ class NumberSelector(Selector[NumberSelectorConfig]):
|
||||
"any", vol.All(vol.Coerce(float), vol.Range(min=1e-3))
|
||||
),
|
||||
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.Optional("translation_key"): str,
|
||||
|
@ -427,6 +427,7 @@ def test_assist_pipeline_selector_schema(
|
||||
({"mode": "box"}, (10,), ()),
|
||||
({"mode": "box", "step": "any"}, (), ()),
|
||||
({"mode": "slider", "min": 0, "max": 1, "step": "any"}, (), ()),
|
||||
({}, (), ()),
|
||||
],
|
||||
)
|
||||
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)
|
||||
|
||||
|
||||
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(
|
||||
"schema",
|
||||
[
|
||||
{}, # Must have mandatory fields
|
||||
{"mode": "slider"}, # Must have min+max in slider mode
|
||||
],
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user