diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index 4666be51d4c..eecc66c8332 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -765,6 +765,28 @@ class TargetSelector(Selector): return target +class TemplateSelectorConfig(TypedDict): + """Class to represent an template selector config.""" + + +@SELECTORS.register("template") +class TemplateSelector(Selector): + """Selector for an template.""" + + selector_type = "template" + + CONFIG_SCHEMA = vol.Schema({}) + + def __init__(self, config: TemplateSelectorConfig | None = None) -> None: + """Instantiate a selector.""" + super().__init__(config) + + def __call__(self, data: Any) -> str: + """Validate the passed selection.""" + template = cv.template(data) + return template.template + + class TextSelectorConfig(TypedDict, total=False): """Class to represent a text selector config.""" diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 326b9f3081e..cb0ad95eb6b 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -627,3 +627,12 @@ def test_datetime_selector_schema(schema, valid_selections, invalid_selections): """Test datetime selector.""" _test_selector("datetime", schema, valid_selections, invalid_selections) + + +@pytest.mark.parametrize( + "schema,valid_selections,invalid_selections", + (({}, ("abc123", "{{ now() }}"), (None, "{{ incomplete }", "{% if True %}Hi!")),), +) +def test_template_selector_schema(schema, valid_selections, invalid_selections): + """Test template selector.""" + _test_selector("template", schema, valid_selections, invalid_selections)