Add template support to numeric_state trigger's for option (#24955)
This commit is contained in:
parent
1431fd6fbd
commit
f9b9883aba
2 changed files with 219 additions and 18 deletions
|
@ -3,13 +3,14 @@ import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant import exceptions
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_VALUE_TEMPLATE, CONF_PLATFORM, CONF_ENTITY_ID,
|
CONF_VALUE_TEMPLATE, CONF_PLATFORM, CONF_ENTITY_ID,
|
||||||
CONF_BELOW, CONF_ABOVE, CONF_FOR)
|
CONF_BELOW, CONF_ABOVE, CONF_FOR)
|
||||||
from homeassistant.helpers.event import (
|
from homeassistant.helpers.event import (
|
||||||
async_track_state_change, async_track_same_state)
|
async_track_state_change, async_track_same_state)
|
||||||
from homeassistant.helpers import condition, config_validation as cv
|
from homeassistant.helpers import condition, config_validation as cv, template
|
||||||
|
|
||||||
TRIGGER_SCHEMA = vol.All(vol.Schema({
|
TRIGGER_SCHEMA = vol.All(vol.Schema({
|
||||||
vol.Required(CONF_PLATFORM): 'numeric_state',
|
vol.Required(CONF_PLATFORM): 'numeric_state',
|
||||||
|
@ -17,7 +18,9 @@ TRIGGER_SCHEMA = vol.All(vol.Schema({
|
||||||
vol.Optional(CONF_BELOW): vol.Coerce(float),
|
vol.Optional(CONF_BELOW): vol.Coerce(float),
|
||||||
vol.Optional(CONF_ABOVE): vol.Coerce(float),
|
vol.Optional(CONF_ABOVE): vol.Coerce(float),
|
||||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Optional(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),
|
||||||
}), cv.has_at_least_one_key(CONF_BELOW, CONF_ABOVE))
|
}), cv.has_at_least_one_key(CONF_BELOW, CONF_ABOVE))
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -29,9 +32,11 @@ async def async_trigger(hass, config, action, automation_info):
|
||||||
below = config.get(CONF_BELOW)
|
below = config.get(CONF_BELOW)
|
||||||
above = config.get(CONF_ABOVE)
|
above = config.get(CONF_ABOVE)
|
||||||
time_delta = config.get(CONF_FOR)
|
time_delta = config.get(CONF_FOR)
|
||||||
|
template.attach(hass, time_delta)
|
||||||
value_template = config.get(CONF_VALUE_TEMPLATE)
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||||
unsub_track_same = {}
|
unsub_track_same = {}
|
||||||
entities_triggered = set()
|
entities_triggered = set()
|
||||||
|
period = {}
|
||||||
|
|
||||||
if value_template is not None:
|
if value_template is not None:
|
||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
|
@ -67,6 +72,7 @@ async def async_trigger(hass, config, action, automation_info):
|
||||||
'above': above,
|
'above': above,
|
||||||
'from_state': from_s,
|
'from_state': from_s,
|
||||||
'to_state': to_s,
|
'to_state': to_s,
|
||||||
|
'for': time_delta if not time_delta else period[entity],
|
||||||
}
|
}
|
||||||
}, context=to_s.context))
|
}, context=to_s.context))
|
||||||
|
|
||||||
|
@ -78,8 +84,39 @@ async def async_trigger(hass, config, action, automation_info):
|
||||||
entities_triggered.add(entity)
|
entities_triggered.add(entity)
|
||||||
|
|
||||||
if time_delta:
|
if time_delta:
|
||||||
|
variables = {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'numeric_state',
|
||||||
|
'entity_id': entity,
|
||||||
|
'below': below,
|
||||||
|
'above': above,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
if isinstance(time_delta, template.Template):
|
||||||
|
period[entity] = 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[entity] = vol.All(
|
||||||
|
cv.time_period,
|
||||||
|
cv.positive_timedelta)(
|
||||||
|
time_delta_data)
|
||||||
|
else:
|
||||||
|
period[entity] = time_delta
|
||||||
|
except (exceptions.TemplateError, vol.Invalid) as ex:
|
||||||
|
_LOGGER.error("Error rendering '%s' for template: %s",
|
||||||
|
automation_info['name'], ex)
|
||||||
|
entities_triggered.discard(entity)
|
||||||
|
return
|
||||||
|
|
||||||
unsub_track_same[entity] = async_track_same_state(
|
unsub_track_same[entity] = async_track_same_state(
|
||||||
hass, time_delta, call_action, entity_ids=entity,
|
hass, period[entity], call_action, entity_ids=entity,
|
||||||
async_check_same_func=check_numeric_state)
|
async_check_same_func=check_numeric_state)
|
||||||
else:
|
else:
|
||||||
call_action()
|
call_action()
|
||||||
|
|
|
@ -703,7 +703,6 @@ async def test_if_action(hass, calls):
|
||||||
|
|
||||||
async def test_if_fails_setup_bad_for(hass, calls):
|
async def test_if_fails_setup_bad_for(hass, calls):
|
||||||
"""Test for setup failure for bad for."""
|
"""Test for setup failure for bad for."""
|
||||||
with assert_setup_component(0, automation.DOMAIN):
|
|
||||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
'trigger': {
|
'trigger': {
|
||||||
|
@ -720,6 +719,11 @@ async def test_if_fails_setup_bad_for(hass, calls):
|
||||||
}
|
}
|
||||||
}})
|
}})
|
||||||
|
|
||||||
|
with patch.object(automation.numeric_state, '_LOGGER') as mock_logger:
|
||||||
|
hass.states.async_set('test.entity', 9)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert mock_logger.error.called
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fails_setup_for_without_above_below(hass, calls):
|
async def test_if_fails_setup_for_without_above_below(hass, calls):
|
||||||
"""Test for setup failures for missing above or below."""
|
"""Test for setup failures for missing above or below."""
|
||||||
|
@ -1009,3 +1013,163 @@ async def test_if_fires_on_entities_change_overlap(hass, calls):
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert 2 == len(calls)
|
assert 2 == len(calls)
|
||||||
assert 'test.entity_2' == calls[1].data['some']
|
assert 'test.entity_2' == calls[1].data['some']
|
||||||
|
|
||||||
|
|
||||||
|
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': 'numeric_state',
|
||||||
|
'entity_id': 'test.entity',
|
||||||
|
'above': 8,
|
||||||
|
'below': 12,
|
||||||
|
'for': {
|
||||||
|
'seconds': '{{ 5 }}'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 9)
|
||||||
|
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': 'numeric_state',
|
||||||
|
'entity_id': 'test.entity',
|
||||||
|
'above': 8,
|
||||||
|
'below': 12,
|
||||||
|
'for': '{{ 5 }}',
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 9)
|
||||||
|
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': 'numeric_state',
|
||||||
|
'entity_id': 'test.entity',
|
||||||
|
'above': 8,
|
||||||
|
'below': 12,
|
||||||
|
'for': '00:00:{{ 5 }}',
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
hass.states.async_set('test.entity', 9)
|
||||||
|
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(hass, calls):
|
||||||
|
"""Test for invalid for template."""
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'numeric_state',
|
||||||
|
'entity_id': 'test.entity',
|
||||||
|
'above': 8,
|
||||||
|
'below': 12,
|
||||||
|
'for': '{{ five }}',
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
with patch.object(automation.numeric_state, '_LOGGER') as mock_logger:
|
||||||
|
hass.states.async_set('test.entity', 9)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert mock_logger.error.called
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_entities_change_overlap_for_template(hass, calls):
|
||||||
|
"""Test for firing on entities change with overlap and for template."""
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'numeric_state',
|
||||||
|
'entity_id': [
|
||||||
|
'test.entity_1',
|
||||||
|
'test.entity_2',
|
||||||
|
],
|
||||||
|
'above': 8,
|
||||||
|
'below': 12,
|
||||||
|
'for': '{{ 5 if trigger.entity_id == "test.entity_1"'
|
||||||
|
' else 10 }}',
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation',
|
||||||
|
'data_template': {
|
||||||
|
'some': '{{ trigger.entity_id }} - {{ trigger.for }}',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
utcnow = dt_util.utcnow()
|
||||||
|
with patch('homeassistant.util.dt.utcnow') as mock_utcnow:
|
||||||
|
mock_utcnow.return_value = utcnow
|
||||||
|
hass.states.async_set('test.entity_1', 9)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
mock_utcnow.return_value += timedelta(seconds=1)
|
||||||
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
||||||
|
hass.states.async_set('test.entity_2', 9)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
mock_utcnow.return_value += timedelta(seconds=1)
|
||||||
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
||||||
|
hass.states.async_set('test.entity_2', 15)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
mock_utcnow.return_value += timedelta(seconds=1)
|
||||||
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
||||||
|
hass.states.async_set('test.entity_2', 9)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 0 == len(calls)
|
||||||
|
mock_utcnow.return_value += timedelta(seconds=3)
|
||||||
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 1 == len(calls)
|
||||||
|
assert 'test.entity_1 - 0:00:05' == calls[0].data['some']
|
||||||
|
|
||||||
|
mock_utcnow.return_value += timedelta(seconds=3)
|
||||||
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 1 == len(calls)
|
||||||
|
mock_utcnow.return_value += timedelta(seconds=5)
|
||||||
|
async_fire_time_changed(hass, mock_utcnow.return_value)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert 2 == len(calls)
|
||||||
|
assert 'test.entity_2 - 0:00:10' == calls[1].data['some']
|
||||||
|
|
Loading…
Add table
Reference in a new issue