mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00: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:
parent
1cb36f4c18
commit
cfdd7fbbce
@ -1117,9 +1117,23 @@ class NumberSelector(Selector[NumberSelectorConfig]):
|
|||||||
return value
|
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 ObjectSelectorConfig(BaseSelectorConfig):
|
||||||
"""Class to represent an object selector config."""
|
"""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")
|
@SELECTORS.register("object")
|
||||||
class ObjectSelector(Selector[ObjectSelectorConfig]):
|
class ObjectSelector(Selector[ObjectSelectorConfig]):
|
||||||
@ -1127,7 +1141,21 @@ class ObjectSelector(Selector[ObjectSelectorConfig]):
|
|||||||
|
|
||||||
selector_type = "object"
|
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:
|
def __init__(self, config: ObjectSelectorConfig | None = None) -> None:
|
||||||
"""Instantiate a selector."""
|
"""Instantiate a selector."""
|
||||||
|
@ -590,7 +590,28 @@ def test_action_selector_schema(schema, valid_selections, invalid_selections) ->
|
|||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("schema", "valid_selections", "invalid_selections"),
|
("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:
|
def test_object_selector_schema(schema, valid_selections, invalid_selections) -> None:
|
||||||
"""Test object selector."""
|
"""Test object selector."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user