Automation: Allow embedding script definition

This commit is contained in:
Paulus Schoutsen 2016-04-21 22:36:14 -04:00
parent b8e4db9161
commit 612a017bc6
3 changed files with 31 additions and 5 deletions

View file

@ -11,8 +11,7 @@ import voluptuous as vol
from homeassistant.bootstrap import prepare_setup_platform
from homeassistant.const import CONF_PLATFORM
from homeassistant.components import logbook
from homeassistant.helpers import extract_domain_configs
from homeassistant.helpers.service import call_from_config
from homeassistant.helpers import extract_domain_configs, script
from homeassistant.loader import get_platform
import homeassistant.helpers.config_validation as cv
@ -88,7 +87,7 @@ PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_CONDITION_TYPE, default=DEFAULT_CONDITION_TYPE):
vol.All(vol.Lower, vol.Any(CONDITION_TYPE_AND, CONDITION_TYPE_OR)),
CONF_CONDITION: _CONDITION_SCHEMA,
vol.Required(CONF_ACTION): cv.SERVICE_SCHEMA,
vol.Required(CONF_ACTION): cv.SCRIPT_SCHEMA,
})
@ -122,11 +121,13 @@ def _setup_automation(hass, config_block, name, config):
def _get_action(hass, config, name):
"""Return an action based on a configuration."""
script_obj = script.Script(hass, config, name)
def action(variables=None):
"""Action to be executed."""
_LOGGER.info('Executing %s', name)
logbook.log_entry(hass, name, 'has been triggered', DOMAIN)
call_from_config(hass, config, variables=variables)
script_obj.run(variables)
return action

View file

@ -316,3 +316,29 @@ class TestAutomation(unittest.TestCase):
self.hass.bus.fire('test_event_2')
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))
def test_automation_calling_two_actions(self):
"""Test if we can call two actions from automation definition."""
self.assertTrue(_setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'action': [{
'service': 'test.automation',
'data': {'position': 0},
}, {
'service': 'test.automation',
'data': {'position': 1},
}],
}
}))
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
assert len(self.calls) == 2
assert self.calls[0].data['position'] == 0
assert self.calls[1].data['position'] == 1

View file

@ -3,7 +3,6 @@
from datetime import timedelta
import unittest
from homeassistant.bootstrap import _setup_component
import homeassistant.util.dt as dt_util
from homeassistant.helpers import script