mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Automation: Allow embedding script definition
This commit is contained in:
parent
b8e4db9161
commit
612a017bc6
@ -11,8 +11,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.bootstrap import prepare_setup_platform
|
from homeassistant.bootstrap import prepare_setup_platform
|
||||||
from homeassistant.const import CONF_PLATFORM
|
from homeassistant.const import CONF_PLATFORM
|
||||||
from homeassistant.components import logbook
|
from homeassistant.components import logbook
|
||||||
from homeassistant.helpers import extract_domain_configs
|
from homeassistant.helpers import extract_domain_configs, script
|
||||||
from homeassistant.helpers.service import call_from_config
|
|
||||||
from homeassistant.loader import get_platform
|
from homeassistant.loader import get_platform
|
||||||
import homeassistant.helpers.config_validation as cv
|
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.Required(CONF_CONDITION_TYPE, default=DEFAULT_CONDITION_TYPE):
|
||||||
vol.All(vol.Lower, vol.Any(CONDITION_TYPE_AND, CONDITION_TYPE_OR)),
|
vol.All(vol.Lower, vol.Any(CONDITION_TYPE_AND, CONDITION_TYPE_OR)),
|
||||||
CONF_CONDITION: _CONDITION_SCHEMA,
|
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):
|
def _get_action(hass, config, name):
|
||||||
"""Return an action based on a configuration."""
|
"""Return an action based on a configuration."""
|
||||||
|
script_obj = script.Script(hass, config, name)
|
||||||
|
|
||||||
def action(variables=None):
|
def action(variables=None):
|
||||||
"""Action to be executed."""
|
"""Action to be executed."""
|
||||||
_LOGGER.info('Executing %s', name)
|
_LOGGER.info('Executing %s', name)
|
||||||
logbook.log_entry(hass, name, 'has been triggered', DOMAIN)
|
logbook.log_entry(hass, name, 'has been triggered', DOMAIN)
|
||||||
call_from_config(hass, config, variables=variables)
|
script_obj.run(variables)
|
||||||
|
|
||||||
return action
|
return action
|
||||||
|
|
||||||
|
@ -316,3 +316,29 @@ class TestAutomation(unittest.TestCase):
|
|||||||
self.hass.bus.fire('test_event_2')
|
self.hass.bus.fire('test_event_2')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(2, len(self.calls))
|
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
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from homeassistant.bootstrap import _setup_component
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.helpers import script
|
from homeassistant.helpers import script
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user