From 61b387cd0b604179ac18b40f059dafccabc138d8 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 13 Mar 2016 22:29:36 -0700 Subject: [PATCH] Script: fix template service calls and remove old config support --- homeassistant/components/script.py | 16 ++----- tests/components/test_script.py | 71 +++++++++++++++++------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/script.py b/homeassistant/components/script.py index 071921426bb..cb5236ef1ec 100644 --- a/homeassistant/components/script.py +++ b/homeassistant/components/script.py @@ -19,7 +19,8 @@ from homeassistant.const import ( from homeassistant.helpers.entity import ToggleEntity, split_entity_id from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.event import track_point_in_utc_time -from homeassistant.helpers.service import call_from_config +from homeassistant.helpers.service import (call_from_config, + validate_service_call) from homeassistant.util import slugify DOMAIN = "script" @@ -30,9 +31,7 @@ STATE_NOT_RUNNING = 'Not Running' CONF_ALIAS = "alias" CONF_SERVICE = "service" -CONF_SERVICE_OLD = "execute_service" CONF_SERVICE_DATA = "data" -CONF_SERVICE_DATA_OLD = "service_data" CONF_SEQUENCE = "sequence" CONF_EVENT = "event" CONF_EVENT_DATA = "event_data" @@ -174,7 +173,7 @@ class Script(ToggleEntity): for cur, action in islice(enumerate(self.sequence), self._cur, None): - if CONF_SERVICE in action or CONF_SERVICE_OLD in action: + if validate_service_call(action) is None: self._call_service(action) elif CONF_EVENT in action: @@ -211,14 +210,7 @@ class Script(ToggleEntity): def _call_service(self, action): """Call the service specified in the action.""" - # Backwards compatibility - if CONF_SERVICE not in action and CONF_SERVICE_OLD in action: - action[CONF_SERVICE] = action[CONF_SERVICE_OLD] - - if CONF_SERVICE_DATA not in action and CONF_SERVICE_DATA_OLD in action: - action[CONF_SERVICE_DATA] = action[CONF_SERVICE_DATA_OLD] - - self._last_action = action.get(CONF_ALIAS, action[CONF_SERVICE]) + self._last_action = action.get(CONF_ALIAS, 'call service') _LOGGER.info("Executing script %s step %s", self._name, self._last_action) call_from_config(self.hass, action, True) diff --git a/tests/components/test_script.py b/tests/components/test_script.py index 450c75740b5..2236128227c 100644 --- a/tests/components/test_script.py +++ b/tests/components/test_script.py @@ -92,35 +92,6 @@ class TestScript(unittest.TestCase): self.assertIsNone( self.hass.states.get(ENTITY_ID).attributes.get('can_cancel')) - def test_calling_service_old(self): - """Test the calling of an old service.""" - calls = [] - - def record_call(service): - """Add recorded event to set.""" - calls.append(service) - - self.hass.services.register('test', 'script', record_call) - - self.assertTrue(script.setup(self.hass, { - 'script': { - 'test': { - 'sequence': [{ - 'execute_service': 'test.script', - 'service_data': { - 'hello': 'world' - } - }] - } - } - })) - - script.turn_on(self.hass, ENTITY_ID) - self.hass.pool.block_till_done() - - self.assertEqual(1, len(calls)) - self.assertEqual('world', calls[0].data.get('hello')) - def test_calling_service(self): """Test the calling of a service.""" calls = [] @@ -136,7 +107,7 @@ class TestScript(unittest.TestCase): 'test': { 'sequence': [{ 'service': 'test.script', - 'service_data': { + 'data': { 'hello': 'world' } }] @@ -150,6 +121,46 @@ class TestScript(unittest.TestCase): self.assertEqual(1, len(calls)) self.assertEqual('world', calls[0].data.get('hello')) + def test_calling_service_template(self): + """Test the calling of a service.""" + calls = [] + + def record_call(service): + """Add recorded event to set.""" + calls.append(service) + + self.hass.services.register('test', 'script', record_call) + + self.assertTrue(script.setup(self.hass, { + 'script': { + 'test': { + 'sequence': [{ + 'service_template': """ + {% if True %} + test.script + {% else %} + test.not_script + {% endif %}""", + 'data_template': { + 'hello': """ + {% if True %} + world + {% else %} + Not world + {% endif %} + """ + } + }] + } + } + })) + + script.turn_on(self.hass, ENTITY_ID) + self.hass.pool.block_till_done() + + self.assertEqual(1, len(calls)) + self.assertEqual('world', calls[0].data.get('hello')) + def test_delay(self): """Test the delay.""" event = 'test_event'