Add fields and multiple support to object selector (#147215)

* Add schema supports to object selector

* Update format

* Update homeassistant/helpers/selector.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Paul Bottein
2025-06-24 15:54:06 +02:00
committed by GitHub
parent 1cb36f4c18
commit cfdd7fbbce
2 changed files with 51 additions and 2 deletions

View File

@ -1117,9 +1117,23 @@ class NumberSelector(Selector[NumberSelectorConfig]):
return value
class ObjectSelectorField(TypedDict):
"""Class to represent an object selector fields dict."""
label: str
required: bool
selector: dict[str, Any]
class ObjectSelectorConfig(BaseSelectorConfig):
"""Class to represent an object selector config."""
fields: dict[str, ObjectSelectorField]
multiple: bool
label_field: str
description_field: bool
translation_key: str
@SELECTORS.register("object")
class ObjectSelector(Selector[ObjectSelectorConfig]):
@ -1127,7 +1141,21 @@ class ObjectSelector(Selector[ObjectSelectorConfig]):
selector_type = "object"
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
{
vol.Optional("fields"): {
str: {
vol.Required("selector"): dict,
vol.Optional("required"): bool,
vol.Optional("label"): str,
}
},
vol.Optional("multiple", default=False): bool,
vol.Optional("label_field"): str,
vol.Optional("description_field"): str,
vol.Optional("translation_key"): str,
}
)
def __init__(self, config: ObjectSelectorConfig | None = None) -> None:
"""Instantiate a selector."""

View File

@ -590,7 +590,28 @@ def test_action_selector_schema(schema, valid_selections, invalid_selections) ->
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
[({}, ("abc123",), ())],
[
({}, ("abc123",), ()),
(
{
"fields": {
"name": {
"required": True,
"selector": {"text": {}},
},
"percentage": {
"selector": {"number": {}},
},
},
"multiple": True,
"label_field": "name",
"description_field": "percentage",
},
(),
(),
),
],
[],
)
def test_object_selector_schema(schema, valid_selections, invalid_selections) -> None:
"""Test object selector."""