Skip to content

Commit

Permalink
Add multiple option to text selector (#104635)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Resch <[email protected]>
  • Loading branch information
piitaya and edenhaus authored Nov 29, 2023
1 parent dfed104 commit 38eda9f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions homeassistant/helpers/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ class TextSelectorConfig(TypedDict, total=False):
suffix: str
type: TextSelectorType
autocomplete: str
multiple: bool


class TextSelectorType(StrEnum):
Expand Down Expand Up @@ -1243,17 +1244,22 @@ class TextSelector(Selector[TextSelectorConfig]):
vol.Coerce(TextSelectorType), lambda val: val.value
),
vol.Optional("autocomplete"): str,
vol.Optional("multiple", default=False): bool,
}
)

def __init__(self, config: TextSelectorConfig | None = None) -> None:
"""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):
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ def test_selector_in_serializer() -> None:
"selector": {
"text": {
"multiline": False,
"multiple": False,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/helpers/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 38eda9f

Please sign in to comment.