Support multiple for StateSelector (#146288)

This commit is contained in:
karwosts
2025-08-11 01:24:20 -07:00
committed by GitHub
parent 167e9c8f4a
commit 0089d3efa1
2 changed files with 15 additions and 4 deletions

View File

@@ -1333,6 +1333,7 @@ class StateSelectorConfig(BaseSelectorConfig, total=False):
entity_id: str
hide_states: list[str]
multiple: bool
@SELECTORS.register("state")
@@ -1350,6 +1351,7 @@ class StateSelector(Selector[StateSelectorConfig]):
# selectors into two types: one for state and one for attribute.
# Limiting the public use, prevents breaking changes in the future.
# vol.Optional("attribute"): str,
vol.Optional("multiple", default=False): cv.boolean,
}
)
@@ -1357,10 +1359,14 @@ class StateSelector(Selector[StateSelectorConfig]):
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> str:
def __call__(self, data: Any) -> str | list[str]:
"""Validate the passed selection."""
if not self.config["multiple"]:
state: str = vol.Schema(str)(data)
return state
if not isinstance(data, list):
raise vol.Invalid("Value should be a list")
return [vol.Schema(str)(val) for val in data]
class StatisticSelectorConfig(BaseSelectorConfig, total=False):

View File

@@ -563,7 +563,12 @@ def test_time_selector_schema(schema, valid_selections, invalid_selections) -> N
(
{"entity_id": "sensor.abc"},
("on", "armed"),
(None, True, 1),
(None, True, 1, ["on"]),
),
(
{"entity_id": "sensor.abc", "multiple": True},
(["on"], ["on", "off"], []),
(None, True, 1, [True], [1], "on"),
),
(
{"hide_states": ["unknown", "unavailable"]},