mirror of
https://github.com/home-assistant/core.git
synced 2026-10-06 14:29:21 -04:00
Add default support to object selector fields (#179810)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot Autofix powered by AI
parent
12c86c629e
commit
a268a10031
@@ -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,
|
||||
}
|
||||
|
||||
@@ -255,6 +255,7 @@
|
||||
}),
|
||||
}),
|
||||
'percentage': dict({
|
||||
'default': 0,
|
||||
'selector': dict({
|
||||
'number': dict({
|
||||
'max': 100.0,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user