diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 4650a4d92c2..83fe36d35c5 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -31,6 +31,7 @@ _RE_GET_ENTITIES = re.compile( r"(?:(?:states\.|(?:is_state|is_state_attr|state_attr|states)" r"\((?:[\ \'\"]?))([\w]+\.[\w]+)|([\w]+))", re.I | re.M ) +_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{") @bind_hass @@ -59,7 +60,10 @@ def render_complex(value, variables=None): def extract_entities(template, variables=None): """Extract all entities for state_changed listener from template string.""" - if template is None or _RE_NONE_ENTITIES.search(template): + if template is None or _RE_JINJA_DELIMITERS.search(template) is None: + return [] + + if _RE_NONE_ENTITIES.search(template): return MATCH_ALL extraction = _RE_GET_ENTITIES.findall(template) diff --git a/tests/components/automation/test_template.py b/tests/components/automation/test_template.py index d9ad765db3f..c326c7f03f4 100644 --- a/tests/components/automation/test_template.py +++ b/tests/components/automation/test_template.py @@ -55,7 +55,7 @@ async def test_if_fires_on_change_str(hass, calls): automation.DOMAIN: { 'trigger': { 'platform': 'template', - 'value_template': 'true', + 'value_template': '{{ "true" }}', }, 'action': { 'service': 'test.automation' @@ -74,7 +74,7 @@ async def test_if_fires_on_change_str_crazy(hass, calls): automation.DOMAIN: { 'trigger': { 'platform': 'template', - 'value_template': 'TrUE', + 'value_template': '{{ "TrUE" }}', }, 'action': { 'service': 'test.automation' @@ -112,7 +112,7 @@ async def test_if_not_fires_on_change_str(hass, calls): automation.DOMAIN: { 'trigger': { 'platform': 'template', - 'value_template': 'False', + 'value_template': 'true', }, 'action': { 'service': 'test.automation' @@ -131,7 +131,7 @@ async def test_if_not_fires_on_change_str_crazy(hass, calls): automation.DOMAIN: { 'trigger': { 'platform': 'template', - 'value_template': 'Anything other than "true" is false.', + 'value_template': '{{ "Anything other than true is false." }}', }, 'action': { 'service': 'test.automation' diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 802138898a4..3f32f444aaf 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -849,7 +849,9 @@ class TestHelpersTemplate(unittest.TestCase): def test_extract_entities_none_exclude_stuff(self): """Test extract entities function with none or exclude stuff.""" - assert MATCH_ALL == template.extract_entities(None) + assert [] == template.extract_entities(None) + + assert [] == template.extract_entities("mdi:water") assert MATCH_ALL == \ template.extract_entities( @@ -896,7 +898,7 @@ class TestHelpersTemplate(unittest.TestCase): assert ['device_tracker.phone_2'] == \ template.extract_entities(""" -is_state_attr('device_tracker.phone_2', 'battery', 40) +{{ is_state_attr('device_tracker.phone_2', 'battery', 40) }} """) assert sorted([