mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
RFC: Static templates match no entities, not all (#17991)
* Static templates match no entities, not all * Clean up test values
This commit is contained in:
parent
71b56363d3
commit
3de822a0e2
@ -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)
|
||||
|
@ -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'
|
||||
|
@ -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([
|
||||
|
Loading…
x
Reference in New Issue
Block a user