From e513b7d0ebb3927ca66297661c22fb15e75fa599 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 12 Jul 2023 16:58:35 +0200 Subject: [PATCH] Add condition selector for blueprint (#96350) * Add condition selector for blueprint * Add tests and validation * Update comment --- homeassistant/helpers/selector.py | 21 +++++++++++++++++++++ tests/helpers/test_selector.py | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index abd4d2e623e..c996fcaf524 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -453,6 +453,27 @@ class ColorTempSelector(Selector[ColorTempSelectorConfig]): return value +class ConditionSelectorConfig(TypedDict): + """Class to represent an action selector config.""" + + +@SELECTORS.register("condition") +class ConditionSelector(Selector[ConditionSelectorConfig]): + """Selector of an condition sequence (script syntax).""" + + selector_type = "condition" + + CONFIG_SCHEMA = vol.Schema({}) + + def __init__(self, config: ConditionSelectorConfig | None = None) -> None: + """Instantiate a selector.""" + super().__init__(config) + + def __call__(self, data: Any) -> Any: + """Validate the passed selection.""" + return vol.Schema(cv.CONDITIONS_SCHEMA)(data) + + class ConfigEntrySelectorConfig(TypedDict, total=False): """Class to represent a config entry selector config.""" diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index fd2dba4b084..09cf79116a0 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -1017,3 +1017,29 @@ def test_conversation_agent_selector_schema( ) -> None: """Test conversation agent selector.""" _test_selector("conversation_agent", schema, valid_selections, invalid_selections) + + +@pytest.mark.parametrize( + ("schema", "valid_selections", "invalid_selections"), + ( + ( + {}, + ( + [ + { + "condition": "numeric_state", + "entity_id": ["sensor.temperature"], + "below": 20, + } + ], + [], + ), + ("abc"), + ), + ), +) +def test_condition_selector_schema( + schema, valid_selections, invalid_selections +) -> None: + """Test condition sequence selector.""" + _test_selector("condition", schema, valid_selections, invalid_selections)