Skip to content

Commit

Permalink
Automation: Allow embedding script definition
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Apr 22, 2016
1 parent b8e4db9 commit 612a017
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
9 changes: 5 additions & 4 deletions homeassistant/components/automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
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

Expand Down Expand Up @@ -88,7 +87,7 @@ def validator(config):
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,
})


Expand Down Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,29 @@ def test_automation_list_setting(self):
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
1 change: 0 additions & 1 deletion tests/helpers/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 612a017

Please sign in to comment.