mirror of
https://github.com/home-assistant/core.git
synced 2025-09-06 05:11:35 +02:00
Support multiple
for StateSelector (#146288)
This commit is contained in:
@@ -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):
|
||||
|
@@ -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"]},
|
||||
|
Reference in New Issue
Block a user