mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 09:31:51 +02:00
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:
@ -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."""
|
||||
|
@ -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."""
|
||||
|
Reference in New Issue
Block a user