diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index ad0c909003e..1003991ccec 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -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.""" - state: str = vol.Schema(str)(data) - return state + 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): diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 50d9da501c5..7f5255a203b 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -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"]},