From a268a10031fe7ee8b71d28db03e26d0ba9b07086 Mon Sep 17 00:00:00 2001 From: nokside <276099665+nokside@users.noreply.github.com> Date: Mon, 28 Sep 2026 14:50:53 +0300 Subject: [PATCH] Add default support to object selector fields (#179810) Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- homeassistant/helpers/selector.py | 2 ++ tests/helpers/snapshots/test_selector.ambr | 1 + tests/helpers/test_selector.py | 28 ++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index db8fe76b0d0a..953fdd030965 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -1762,6 +1762,7 @@ class NumericThresholdSelector(Selector[NumericThresholdSelectorConfig]): class ObjectSelectorField(TypedDict, total=False): """Class to represent an object selector fields dict.""" + default: Any label: str required: bool selector: Required[Selector | dict[str, Any]] @@ -1790,6 +1791,7 @@ class ObjectSelector(Selector[ObjectSelectorConfig]): probatio.Required("selector"): probatio.Any( Selector, validate_selector ), + probatio.Optional("default"): cv.match_all, probatio.Optional("required"): bool, probatio.Optional("label"): str, } diff --git a/tests/helpers/snapshots/test_selector.ambr b/tests/helpers/snapshots/test_selector.ambr index ff66f606f383..a853021d7f09 100644 --- a/tests/helpers/snapshots/test_selector.ambr +++ b/tests/helpers/snapshots/test_selector.ambr @@ -255,6 +255,7 @@ }), }), 'percentage': dict({ + 'default': 0, 'selector': dict({ 'number': dict({ 'max': 100.0, diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index f002412ba497..9fb345ce2141 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -1394,6 +1394,33 @@ def test_object_selector_schema(schema, valid_selections, invalid_selections) -> _test_selector("object", schema, valid_selections, invalid_selections) +@pytest.mark.parametrize( + "default", + [ + pytest.param(0, id="zero"), + pytest.param(False, id="false"), + pytest.param("", id="empty-string"), + pytest.param([], id="empty-list"), + pytest.param({}, id="empty-dict"), + ], +) +def test_object_selector_field_default(default: object) -> None: + """Test object selector field default.""" + validated = selector.validate_selector( + { + "object": { + "fields": { + "field": { + "selector": {"text": {}}, + "default": default, + } + } + } + } + ) + assert validated["object"]["fields"]["field"]["default"] == default + + def test_object_selector_uses_selectors(snapshot: SnapshotAssertion) -> None: """Test ObjectSelector serializer with Selector in ObjectSelectorField.""" @@ -1408,6 +1435,7 @@ def test_object_selector_uses_selectors(snapshot: SnapshotAssertion) -> None: "selector": selector.NumberSelector( selector.NumberSelectorConfig(min=0, max=100) ), + "default": 0, }, }, "multiple": True,