Script: fix template service calls and remove old config support

pull/1538/head
Paulus Schoutsen 2016-03-13 22:29:36 -07:00
parent 368668784a
commit 61b387cd0b
2 changed files with 45 additions and 42 deletions

View File

@ -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)

View File

@ -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'