diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index 438998aafb8..6f8df828c37 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -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.""" diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 97c02bdc837..8947ea8099c 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -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."""