2016-03-09 09:25:50 +00:00
|
|
|
"""Test service helpers."""
|
2016-01-09 23:51:51 +00:00
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2016-04-21 20:59:42 +00:00
|
|
|
# To prevent circular import when running just this file
|
|
|
|
import homeassistant.components # noqa
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant import core as ha, loader
|
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID
|
2016-01-09 23:51:51 +00:00
|
|
|
from homeassistant.helpers import service
|
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant, mock_service
|
|
|
|
|
|
|
|
|
|
|
|
class TestServiceHelpers(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Home Assistant service helpers."""
|
2016-01-09 23:51:51 +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."""
|
2016-01-09 23:51:51 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.calls = mock_service(self.hass, 'test_domain', 'test_service')
|
|
|
|
|
2016-01-25 03:46:30 +00:00
|
|
|
service.HASS = self.hass
|
|
|
|
|
2016-01-09 23:51:51 +00:00
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down everything that was started."""
|
2016-01-09 23:51:51 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-01-25 03:46:30 +00:00
|
|
|
def test_service(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test service registration decorator."""
|
2016-01-25 03:46:30 +00:00
|
|
|
runs = []
|
|
|
|
|
|
|
|
decor = service.service('test', 'test')
|
|
|
|
decor(lambda x, y: runs.append(1))
|
|
|
|
|
|
|
|
self.hass.services.call('test', 'test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
2016-03-10 20:36:05 +00:00
|
|
|
def test_template_service_call(self):
|
2016-04-21 22:52:20 +00:00
|
|
|
"""Test service call with tempating."""
|
2016-03-10 20:36:05 +00:00
|
|
|
config = {
|
|
|
|
'service_template': '{{ \'test_domain.test_service\' }}',
|
|
|
|
'entity_id': 'hello.world',
|
|
|
|
'data_template': {
|
|
|
|
'hello': '{{ \'goodbye\' }}',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
decor = service.service('test_domain', 'test_service')
|
|
|
|
decor(lambda x, y: runs.append(y))
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertEqual('goodbye', runs[0].data['hello'])
|
|
|
|
|
2016-04-21 19:22:19 +00:00
|
|
|
def test_passing_variables_to_templates(self):
|
2016-04-21 22:52:20 +00:00
|
|
|
"""Test passing variables to templates."""
|
2016-04-21 19:22:19 +00:00
|
|
|
config = {
|
|
|
|
'service_template': '{{ var_service }}',
|
|
|
|
'entity_id': 'hello.world',
|
|
|
|
'data_template': {
|
|
|
|
'hello': '{{ var_data }}',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
decor = service.service('test_domain', 'test_service')
|
|
|
|
decor(lambda x, y: runs.append(y))
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config, variables={
|
|
|
|
'var_service': 'test_domain.test_service',
|
|
|
|
'var_data': 'goodbye',
|
|
|
|
})
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
self.assertEqual('goodbye', runs[0].data['hello'])
|
|
|
|
|
2016-01-09 23:51:51 +00:00
|
|
|
def test_split_entity_string(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test splitting of entity string."""
|
2016-01-09 23:51:51 +00:00
|
|
|
service.call_from_config(self.hass, {
|
|
|
|
'service': 'test_domain.test_service',
|
|
|
|
'entity_id': 'hello.world, sensor.beer'
|
|
|
|
})
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(['hello.world', 'sensor.beer'],
|
|
|
|
self.calls[-1].data.get('entity_id'))
|
2016-01-10 00:01:27 +00:00
|
|
|
|
|
|
|
def test_not_mutate_input(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for immutable input."""
|
2016-01-10 00:01:27 +00:00
|
|
|
orig = {
|
|
|
|
'service': 'test_domain.test_service',
|
|
|
|
'entity_id': 'hello.world, sensor.beer',
|
|
|
|
'data': {
|
|
|
|
'hello': 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
service.call_from_config(self.hass, orig)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual({
|
|
|
|
'service': 'test_domain.test_service',
|
|
|
|
'entity_id': 'hello.world, sensor.beer',
|
|
|
|
'data': {
|
|
|
|
'hello': 1,
|
|
|
|
},
|
|
|
|
}, orig)
|
|
|
|
|
|
|
|
@patch('homeassistant.helpers.service._LOGGER.error')
|
|
|
|
def test_fail_silently_if_no_service(self, mock_log):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test failling if service is missing."""
|
2016-01-10 00:01:27 +00:00
|
|
|
service.call_from_config(self.hass, None)
|
|
|
|
self.assertEqual(1, mock_log.call_count)
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, {})
|
|
|
|
self.assertEqual(2, mock_log.call_count)
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, {
|
|
|
|
'service': 'invalid'
|
|
|
|
})
|
|
|
|
self.assertEqual(3, mock_log.call_count)
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
def test_extract_entity_ids(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test extract_entity_ids method."""
|
2016-01-24 06:57:14 +00:00
|
|
|
self.hass.states.set('light.Bowl', STATE_ON)
|
|
|
|
self.hass.states.set('light.Ceiling', STATE_OFF)
|
|
|
|
self.hass.states.set('light.Kitchen', STATE_OFF)
|
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
loader.get_component('group').Group(
|
2016-01-24 06:57:14 +00:00
|
|
|
self.hass, 'test', ['light.Ceiling', 'light.Kitchen'])
|
|
|
|
|
|
|
|
call = ha.ServiceCall('light', 'turn_on',
|
|
|
|
{ATTR_ENTITY_ID: 'light.Bowl'})
|
|
|
|
|
|
|
|
self.assertEqual(['light.bowl'],
|
|
|
|
service.extract_entity_ids(self.hass, call))
|
|
|
|
|
|
|
|
call = ha.ServiceCall('light', 'turn_on',
|
|
|
|
{ATTR_ENTITY_ID: 'group.test'})
|
|
|
|
|
|
|
|
self.assertEqual(['light.ceiling', 'light.kitchen'],
|
|
|
|
service.extract_entity_ids(self.hass, call))
|