From 612a017bc6138b4b0b292111fb2e538889cd85a9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 21 Apr 2016 22:36:14 -0400 Subject: [PATCH] Automation: Allow embedding script definition --- .../components/automation/__init__.py | 9 ++++--- tests/components/automation/test_init.py | 26 +++++++++++++++++++ tests/helpers/test_script.py | 1 - 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 8cbaf35a5c4bd..3ba5596fb4d9b 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -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 @@ -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, }) @@ -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 diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index f6d33c1807169..8e06f524d0e2c 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -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 diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 492b62906df07..47af833223f86 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -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