2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Script component."""
|
2015-10-15 06:09:52 +00:00
|
|
|
# pylint: disable=too-many-public-methods,protected-access
|
|
|
|
from datetime import timedelta
|
|
|
|
import unittest
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
from homeassistant.bootstrap import _setup_component
|
2015-10-15 06:09:52 +00:00
|
|
|
from homeassistant.components import script
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
|
|
|
from tests.common import fire_time_changed, get_test_home_assistant
|
|
|
|
|
|
|
|
|
|
|
|
ENTITY_ID = 'script.test'
|
|
|
|
|
|
|
|
|
|
|
|
class TestScript(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Script component."""
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2015-10-15 06:09:52 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-04-03 17:19:09 +00:00
|
|
|
self.hass.config.components.append('group')
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down everything that was started."""
|
2015-10-15 06:09:52 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
def test_setup_with_invalid_configs(self):
|
|
|
|
"""Test setup with invalid configs."""
|
|
|
|
for value in (
|
|
|
|
{'test': {}},
|
|
|
|
{
|
2015-11-26 21:08:13 +00:00
|
|
|
'test hello world': {
|
2016-04-04 19:18:58 +00:00
|
|
|
'sequence': [{'event': 'bla'}]
|
2015-10-15 06:09:52 +00:00
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'test': {
|
|
|
|
'sequence': {
|
|
|
|
'event': 'test_event',
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
):
|
|
|
|
assert not _setup_component(self.hass, 'script', {
|
|
|
|
'script': value
|
|
|
|
}), 'Script loaded with wrong config {}'.format(value)
|
2015-10-28 19:24:33 +00:00
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
self.assertEqual(0, len(self.hass.states.entity_ids('script')))
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
def test_firing_event(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the firing of events."""
|
2015-10-15 06:09:52 +00:00
|
|
|
event = 'test_event'
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_event(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2015-10-15 06:09:52 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
2015-10-28 19:24:33 +00:00
|
|
|
'alias': 'Test Script',
|
2015-10-15 06:09:52 +00:00
|
|
|
'sequence': [{
|
|
|
|
'event': event,
|
|
|
|
'event_data': {
|
|
|
|
'hello': 'world'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
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'))
|
2016-02-28 06:30:24 +00:00
|
|
|
self.assertIsNone(
|
|
|
|
self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))
|
2015-10-15 06:09:52 +00:00
|
|
|
|
2016-03-14 05:29:36 +00:00
|
|
|
def test_calling_service(self):
|
|
|
|
"""Test the calling of a service."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_call(service):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'script', record_call)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2015-10-15 06:09:52 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
|
|
|
'sequence': [{
|
2016-03-14 05:29:36 +00:00
|
|
|
'service': 'test.script',
|
|
|
|
'data': {
|
2015-10-15 06:09:52 +00:00
|
|
|
'hello': 'world'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
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'))
|
|
|
|
|
2016-03-14 05:29:36 +00:00
|
|
|
def test_calling_service_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the calling of a service."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_call(service):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'script', record_call)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2015-10-15 06:09:52 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
|
|
|
'sequence': [{
|
2016-03-14 05:29:36 +00:00
|
|
|
'service_template': """
|
|
|
|
{% if True %}
|
|
|
|
test.script
|
|
|
|
{% else %}
|
|
|
|
test.not_script
|
|
|
|
{% endif %}""",
|
|
|
|
'data_template': {
|
|
|
|
'hello': """
|
|
|
|
{% if True %}
|
|
|
|
world
|
|
|
|
{% else %}
|
|
|
|
Not world
|
|
|
|
{% endif %}
|
|
|
|
"""
|
2015-10-15 06:09:52 +00:00
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the delay."""
|
2015-10-15 06:09:52 +00:00
|
|
|
event = 'test_event'
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_event(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2015-10-15 06:09:52 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
|
|
|
'sequence': [{
|
|
|
|
'event': event
|
|
|
|
}, {
|
|
|
|
'delay': {
|
|
|
|
'seconds': 5
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'event': event,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
script.turn_on(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertTrue(script.is_on(self.hass, ENTITY_ID))
|
2016-02-28 06:30:24 +00:00
|
|
|
self.assertTrue(
|
2015-11-14 23:36:27 +00:00
|
|
|
self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
event,
|
|
|
|
self.hass.states.get(ENTITY_ID).attributes.get('last_action'))
|
|
|
|
self.assertEqual(1, len(calls))
|
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
fire_time_changed(self.hass, future)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertFalse(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
|
|
|
|
self.assertEqual(2, len(calls))
|
|
|
|
|
|
|
|
def test_cancel_while_delay(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the cancelling while the delay is present."""
|
2015-10-15 06:09:52 +00:00
|
|
|
event = 'test_event'
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_event(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2015-10-15 06:09:52 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2015-10-15 06:09:52 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
|
|
|
'sequence': [{
|
|
|
|
'delay': {
|
|
|
|
'seconds': 5
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'event': event,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2015-10-15 06:09:52 +00:00
|
|
|
|
|
|
|
script.turn_on(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertTrue(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
|
|
|
|
self.assertEqual(0, len(calls))
|
|
|
|
|
|
|
|
script.turn_off(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertFalse(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
|
|
|
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
|
|
|
fire_time_changed(self.hass, future)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertFalse(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
|
|
|
|
self.assertEqual(0, len(calls))
|
2016-02-21 17:11:35 +00:00
|
|
|
|
|
|
|
def test_turn_on_service(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Verify that the turn_on service."""
|
2016-02-21 17:11:35 +00:00
|
|
|
event = 'test_event'
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_event(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2016-02-21 17:11:35 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2016-02-21 17:11:35 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
|
|
|
'sequence': [{
|
|
|
|
'delay': {
|
|
|
|
'seconds': 5
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'event': event,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2016-02-21 17:11:35 +00:00
|
|
|
|
|
|
|
script.turn_on(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertTrue(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
self.assertEqual(0, len(calls))
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Calling turn_on a second time should not advance the script
|
2016-02-21 17:11:35 +00:00
|
|
|
script.turn_on(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(0, len(calls))
|
2016-02-21 17:22:38 +00:00
|
|
|
|
|
|
|
def test_toggle_service(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the toggling of a service."""
|
2016-02-21 17:22:38 +00:00
|
|
|
event = 'test_event'
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def record_event(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Add recorded event to set."""
|
2016-02-21 17:22:38 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
assert _setup_component(self.hass, 'script', {
|
2016-02-21 17:22:38 +00:00
|
|
|
'script': {
|
|
|
|
'test': {
|
|
|
|
'sequence': [{
|
|
|
|
'delay': {
|
|
|
|
'seconds': 5
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'event': event,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:19:09 +00:00
|
|
|
})
|
2016-02-21 17:22:38 +00:00
|
|
|
|
|
|
|
script.toggle(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertTrue(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
self.assertEqual(0, len(calls))
|
|
|
|
|
|
|
|
script.toggle(self.hass, ENTITY_ID)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertFalse(script.is_on(self.hass, ENTITY_ID))
|
|
|
|
self.assertEqual(0, len(calls))
|