mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Add multiple option to text selector (#104635)
Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
parent
dfed10420c
commit
38eda9f46e
@ -1206,6 +1206,7 @@ class TextSelectorConfig(TypedDict, total=False):
|
||||
suffix: str
|
||||
type: TextSelectorType
|
||||
autocomplete: str
|
||||
multiple: bool
|
||||
|
||||
|
||||
class TextSelectorType(StrEnum):
|
||||
@ -1243,6 +1244,7 @@ class TextSelector(Selector[TextSelectorConfig]):
|
||||
vol.Coerce(TextSelectorType), lambda val: val.value
|
||||
),
|
||||
vol.Optional("autocomplete"): str,
|
||||
vol.Optional("multiple", default=False): bool,
|
||||
}
|
||||
)
|
||||
|
||||
@ -1250,10 +1252,14 @@ class TextSelector(Selector[TextSelectorConfig]):
|
||||
"""Instantiate a selector."""
|
||||
super().__init__(config)
|
||||
|
||||
def __call__(self, data: Any) -> str:
|
||||
def __call__(self, data: Any) -> str | list[str]:
|
||||
"""Validate the passed selection."""
|
||||
text: str = vol.Schema(str)(data)
|
||||
return text
|
||||
if not self.config["multiple"]:
|
||||
text: str = vol.Schema(str)(data)
|
||||
return text
|
||||
if not isinstance(data, list):
|
||||
raise vol.Invalid("Value should be a list")
|
||||
return [vol.Schema(str)(val) for val in data]
|
||||
|
||||
|
||||
class ThemeSelectorConfig(TypedDict):
|
||||
|
@ -832,6 +832,7 @@ def test_selector_in_serializer() -> None:
|
||||
"selector": {
|
||||
"text": {
|
||||
"multiline": False,
|
||||
"multiple": False,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -602,6 +602,11 @@ def test_object_selector_schema(schema, valid_selections, invalid_selections) ->
|
||||
({"multiline": True}, (), ()),
|
||||
({"multiline": False, "type": "email"}, (), ()),
|
||||
({"prefix": "before", "suffix": "after"}, (), ()),
|
||||
(
|
||||
{"multiple": True},
|
||||
(["abc123", "def456"],),
|
||||
("abc123", None, ["abc123", None]),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_text_selector_schema(schema, valid_selections, invalid_selections) -> None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user