Add Template selector (#70229)

This commit is contained in:
Franck Nijhof 2022-04-18 20:28:01 +02:00 committed by GitHub
parent 1e4aacaeb1
commit ce1f074ca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -765,6 +765,28 @@ class TargetSelector(Selector):
return target 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 TextSelectorConfig(TypedDict, total=False):
"""Class to represent a text selector config.""" """Class to represent a text selector config."""

View File

@ -627,3 +627,12 @@ def test_datetime_selector_schema(schema, valid_selections, invalid_selections):
"""Test datetime selector.""" """Test datetime selector."""
_test_selector("datetime", schema, valid_selections, invalid_selections) _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)