core/tests/components/test_script.py

160 lines
4.6 KiB
Python
Raw Normal View History

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
import unittest
from homeassistant.bootstrap import _setup_component
2015-10-15 06:09:52 +00:00
from homeassistant.components import script
from tests.common import get_test_home_assistant
2015-10-15 06:09:52 +00:00
ENTITY_ID = 'script.test'
class TestScriptComponent(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()
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()
def test_setup_with_invalid_configs(self):
"""Test setup with invalid configs."""
for value in (
{'test': {}},
{
'test hello world': {
'sequence': [{'event': 'bla'}]
2015-10-15 06:09:52 +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
self.assertEqual(0, len(self.hass.states.entity_ids('script')))
2015-10-15 06:09:52 +00:00
def test_turn_on_service(self):
2016-03-09 09:25:50 +00:00
"""Verify that the turn_on service."""
event = 'test_event'
events = []
def record_event(event):
2016-03-09 09:25:50 +00:00
"""Add recorded event to set."""
events.append(event)
self.hass.bus.listen(event, record_event)
assert _setup_component(self.hass, 'script', {
'script': {
'test': {
'sequence': [{
'delay': {
'seconds': 5
}
}, {
'event': event,
}]
}
}
})
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(events))
2016-03-09 09:25:50 +00:00
# Calling turn_on a second time should not advance the script
script.turn_on(self.hass, ENTITY_ID)
self.hass.pool.block_till_done()
self.assertEqual(0, len(events))
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'
events = []
2016-02-21 17:22:38 +00:00
def record_event(event):
2016-03-09 09:25:50 +00:00
"""Add recorded event to set."""
events.append(event)
2016-02-21 17:22:38 +00:00
self.hass.bus.listen(event, record_event)
assert _setup_component(self.hass, 'script', {
2016-02-21 17:22:38 +00:00
'script': {
'test': {
'sequence': [{
'delay': {
'seconds': 5
}
}, {
'event': event,
}]
}
}
})
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(events))
2016-02-21 17:22:38 +00:00
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(events))
def test_passing_variables(self):
"""Test different ways of passing in variables."""
calls = []
def record_call(service):
"""Add recorded event to set."""
calls.append(service)
self.hass.services.register('test', 'script', record_call)
assert _setup_component(self.hass, 'script', {
'script': {
'test': {
'sequence': {
'service': 'test.script',
'data_template': {
'hello': '{{ greeting }}',
},
},
},
},
})
script.turn_on(self.hass, ENTITY_ID, {
'greeting': 'world'
})
self.hass.pool.block_till_done()
assert len(calls) == 1
assert calls[-1].data['hello'] == 'world'
self.hass.services.call('script', 'test', {
'greeting': 'universe',
})
self.hass.pool.block_till_done()
assert len(calls) == 2
assert calls[-1].data['hello'] == 'universe'