hass-core/homeassistant/components/automation/template.py

37 lines
1.2 KiB
Python
Raw Normal View History

"""Offer template automation rules."""
2015-12-16 10:52:33 -07:00
import logging
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.const import CONF_VALUE_TEMPLATE, CONF_PLATFORM
from homeassistant.helpers.event import async_track_template
import homeassistant.helpers.config_validation as cv
2015-12-16 10:52:33 -07:00
_LOGGER = logging.getLogger(__name__)
TRIGGER_SCHEMA = IF_ACTION_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): 'template',
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
})
2015-12-16 10:52:33 -07:00
async def async_trigger(hass, config, action, automation_info):
2016-03-07 17:14:55 +01:00
"""Listen for state changes based on configuration."""
value_template = config.get(CONF_VALUE_TEMPLATE)
value_template.hass = hass
2015-12-16 10:52:33 -07:00
@callback
def template_listener(entity_id, from_s, to_s):
2016-03-07 20:20:07 +01:00
"""Listen for state changes and calls action."""
hass.async_run_job(action({
'trigger': {
'platform': 'template',
'entity_id': entity_id,
'from_state': from_s,
'to_state': to_s,
},
2019-04-18 20:04:30 +08:00
}, context=(to_s.context if to_s else None)))
return async_track_template(hass, value_template, template_listener)