From 03e6a92cf30c4dffc5fe201740a18e5f1d073caf Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Sat, 29 Jun 2019 00:30:47 -0500 Subject: [PATCH] Add template support to template trigger's for option (#24810) --- .../components/automation/template.py | 50 +++++++--- tests/components/automation/test_template.py | 95 +++++++++++++++++++ 2 files changed, 134 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/automation/template.py b/homeassistant/components/automation/template.py index 96075e9bd1c..c3d7c02aedd 100644 --- a/homeassistant/components/automation/template.py +++ b/homeassistant/components/automation/template.py @@ -5,17 +5,20 @@ import voluptuous as vol from homeassistant.core import callback from homeassistant.const import CONF_VALUE_TEMPLATE, CONF_PLATFORM, CONF_FOR +from homeassistant import exceptions from homeassistant.helpers import condition from homeassistant.helpers.event import ( async_track_same_state, async_track_template) -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import config_validation as cv, template _LOGGER = logging.getLogger(__name__) TRIGGER_SCHEMA = IF_ACTION_SCHEMA = vol.Schema({ vol.Required(CONF_PLATFORM): 'template', vol.Required(CONF_VALUE_TEMPLATE): cv.template, - vol.Optional(CONF_FOR): vol.All(cv.time_period, cv.positive_timedelta), + vol.Optional(CONF_FOR): vol.Any( + vol.All(cv.time_period, cv.positive_timedelta), + cv.template, cv.template_complex), }) @@ -24,6 +27,7 @@ async def async_trigger(hass, config, action, automation_info): value_template = config.get(CONF_VALUE_TEMPLATE) value_template.hass = hass time_delta = config.get(CONF_FOR) + template.attach(hass, time_delta) unsub_track_same = None @callback @@ -31,24 +35,48 @@ async def async_trigger(hass, config, action, automation_info): """Listen for state changes and calls action.""" nonlocal unsub_track_same + variables = { + 'trigger': { + 'platform': 'template', + 'entity_id': entity_id, + 'from_state': from_s, + 'to_state': to_s, + }, + } + @callback def call_action(): """Call action with right context.""" - hass.async_run_job(action({ - 'trigger': { - 'platform': 'template', - 'entity_id': entity_id, - 'from_state': from_s, - 'to_state': to_s, - }, - }, context=(to_s.context if to_s else None))) + hass.async_run_job(action( + variables, context=(to_s.context if to_s else None))) if not time_delta: call_action() return + try: + if isinstance(time_delta, template.Template): + period = vol.All( + cv.time_period, + cv.positive_timedelta)( + time_delta.async_render(variables)) + elif isinstance(time_delta, dict): + time_delta_data = {} + time_delta_data.update( + template.render_complex(time_delta, variables)) + period = vol.All( + cv.time_period, + cv.positive_timedelta)( + time_delta_data) + else: + period = time_delta + except (exceptions.TemplateError, vol.Invalid) as ex: + _LOGGER.error("Error rendering '%s' for template: %s", + automation_info['name'], ex) + return + unsub_track_same = async_track_same_state( - hass, time_delta, call_action, + hass, period, call_action, lambda _, _2, _3: condition.async_template(hass, value_template), value_template.extract_entities()) diff --git a/tests/components/automation/test_template.py b/tests/components/automation/test_template.py index 815c5e440b4..48503acbc5f 100644 --- a/tests/components/automation/test_template.py +++ b/tests/components/automation/test_template.py @@ -1,5 +1,6 @@ """The tests for the Template automation.""" from datetime import timedelta +from unittest import mock import pytest @@ -525,3 +526,97 @@ async def test_if_not_fires_when_turned_off_with_for(hass, calls): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=6)) await hass.async_block_till_done() assert 0 == len(calls) + + +async def test_if_fires_on_change_with_for_template_1(hass, calls): + """Test for firing on change with for template.""" + assert await async_setup_component(hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'template', + 'value_template': "{{ is_state('test.entity', 'world') }}", + 'for': { + 'seconds': "{{ 5 }}" + }, + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + hass.states.async_set('test.entity', 'world') + await hass.async_block_till_done() + assert 0 == len(calls) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) + await hass.async_block_till_done() + assert 1 == len(calls) + + +async def test_if_fires_on_change_with_for_template_2(hass, calls): + """Test for firing on change with for template.""" + assert await async_setup_component(hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'template', + 'value_template': "{{ is_state('test.entity', 'world') }}", + 'for': "{{ 5 }}", + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + hass.states.async_set('test.entity', 'world') + await hass.async_block_till_done() + assert 0 == len(calls) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) + await hass.async_block_till_done() + assert 1 == len(calls) + + +async def test_if_fires_on_change_with_for_template_3(hass, calls): + """Test for firing on change with for template.""" + assert await async_setup_component(hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'template', + 'value_template': "{{ is_state('test.entity', 'world') }}", + 'for': "00:00:{{ 5 }}", + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + hass.states.async_set('test.entity', 'world') + await hass.async_block_till_done() + assert 0 == len(calls) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) + await hass.async_block_till_done() + assert 1 == len(calls) + + +async def test_invalid_for_template_1(hass, calls): + """Test for invalid for template.""" + assert await async_setup_component(hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'template', + 'value_template': "{{ is_state('test.entity', 'world') }}", + 'for': { + 'seconds': "{{ five }}" + }, + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + with mock.patch.object(automation.template, '_LOGGER') as mock_logger: + hass.states.async_set('test.entity', 'world') + await hass.async_block_till_done() + assert mock_logger.error.called