2015-12-16 17:52:33 +00:00
|
|
|
"""
|
2016-03-07 19:20:07 +00:00
|
|
|
Offer template automation rules.
|
2015-12-16 17:52:33 +00:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2017-04-06 06:23:02 +00:00
|
|
|
at https://home-assistant.io/docs/automation/trigger/#template-trigger
|
2015-12-16 17:52:33 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2016-09-28 04:29:55 +00:00
|
|
|
from homeassistant.const import CONF_VALUE_TEMPLATE, CONF_PLATFORM
|
2017-02-12 21:27:53 +00:00
|
|
|
from homeassistant.helpers.event import async_track_template
|
2016-04-04 19:18:58 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2015-12-16 17:52:33 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
TRIGGER_SCHEMA = IF_ACTION_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'template',
|
|
|
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
})
|
|
|
|
|
2015-12-16 17:52:33 +00:00
|
|
|
|
2018-11-05 08:23:58 +00:00
|
|
|
async def async_trigger(hass, config, action, automation_info):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listen for state changes based on configuration."""
|
2016-09-28 04:29:55 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
value_template.hass = hass
|
2015-12-16 17:52:33 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2017-02-12 21:27:53 +00:00
|
|
|
def template_listener(entity_id, from_s, to_s):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for state changes and calls action."""
|
2018-09-04 19:16:24 +00:00
|
|
|
hass.async_run_job(action({
|
2017-02-12 21:27:53 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'entity_id': entity_id,
|
|
|
|
'from_state': from_s,
|
|
|
|
'to_state': to_s,
|
|
|
|
},
|
2018-09-04 19:16:24 +00:00
|
|
|
}, context=to_s.context))
|
2017-02-12 21:27:53 +00:00
|
|
|
|
|
|
|
return async_track_template(hass, value_template, template_listener)
|