Script: fix template service calls and remove old config support
This commit is contained in:
parent
368668784a
commit
61b387cd0b
2 changed files with 45 additions and 42 deletions
|
@ -19,7 +19,8 @@ from homeassistant.const import (
|
||||||
from homeassistant.helpers.entity import ToggleEntity, split_entity_id
|
from homeassistant.helpers.entity import ToggleEntity, split_entity_id
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.event import track_point_in_utc_time
|
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
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
DOMAIN = "script"
|
DOMAIN = "script"
|
||||||
|
@ -30,9 +31,7 @@ STATE_NOT_RUNNING = 'Not Running'
|
||||||
|
|
||||||
CONF_ALIAS = "alias"
|
CONF_ALIAS = "alias"
|
||||||
CONF_SERVICE = "service"
|
CONF_SERVICE = "service"
|
||||||
CONF_SERVICE_OLD = "execute_service"
|
|
||||||
CONF_SERVICE_DATA = "data"
|
CONF_SERVICE_DATA = "data"
|
||||||
CONF_SERVICE_DATA_OLD = "service_data"
|
|
||||||
CONF_SEQUENCE = "sequence"
|
CONF_SEQUENCE = "sequence"
|
||||||
CONF_EVENT = "event"
|
CONF_EVENT = "event"
|
||||||
CONF_EVENT_DATA = "event_data"
|
CONF_EVENT_DATA = "event_data"
|
||||||
|
@ -174,7 +173,7 @@ class Script(ToggleEntity):
|
||||||
for cur, action in islice(enumerate(self.sequence), self._cur,
|
for cur, action in islice(enumerate(self.sequence), self._cur,
|
||||||
None):
|
None):
|
||||||
|
|
||||||
if CONF_SERVICE in action or CONF_SERVICE_OLD in action:
|
if validate_service_call(action) is None:
|
||||||
self._call_service(action)
|
self._call_service(action)
|
||||||
|
|
||||||
elif CONF_EVENT in action:
|
elif CONF_EVENT in action:
|
||||||
|
@ -211,14 +210,7 @@ class Script(ToggleEntity):
|
||||||
|
|
||||||
def _call_service(self, action):
|
def _call_service(self, action):
|
||||||
"""Call the service specified in the action."""
|
"""Call the service specified in the action."""
|
||||||
# Backwards compatibility
|
self._last_action = action.get(CONF_ALIAS, 'call service')
|
||||||
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])
|
|
||||||
_LOGGER.info("Executing script %s step %s", self._name,
|
_LOGGER.info("Executing script %s step %s", self._name,
|
||||||
self._last_action)
|
self._last_action)
|
||||||
call_from_config(self.hass, action, True)
|
call_from_config(self.hass, action, True)
|
||||||
|
|
|
@ -92,35 +92,6 @@ class TestScript(unittest.TestCase):
|
||||||
self.assertIsNone(
|
self.assertIsNone(
|
||||||
self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))
|
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):
|
def test_calling_service(self):
|
||||||
"""Test the calling of a service."""
|
"""Test the calling of a service."""
|
||||||
calls = []
|
calls = []
|
||||||
|
@ -136,7 +107,7 @@ class TestScript(unittest.TestCase):
|
||||||
'test': {
|
'test': {
|
||||||
'sequence': [{
|
'sequence': [{
|
||||||
'service': 'test.script',
|
'service': 'test.script',
|
||||||
'service_data': {
|
'data': {
|
||||||
'hello': 'world'
|
'hello': 'world'
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
@ -150,6 +121,46 @@ class TestScript(unittest.TestCase):
|
||||||
self.assertEqual(1, len(calls))
|
self.assertEqual(1, len(calls))
|
||||||
self.assertEqual('world', calls[0].data.get('hello'))
|
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):
|
def test_delay(self):
|
||||||
"""Test the delay."""
|
"""Test the delay."""
|
||||||
event = 'test_event'
|
event = 'test_event'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue