2016-03-09 09:25:50 +00:00
|
|
|
"""Test service helpers."""
|
2018-01-07 22:54:16 +00:00
|
|
|
import asyncio
|
2018-11-21 11:26:08 +00:00
|
|
|
from collections import OrderedDict
|
2016-10-01 06:26:15 +00:00
|
|
|
from copy import deepcopy
|
2016-01-09 23:51:51 +00:00
|
|
|
import unittest
|
2018-11-21 11:26:08 +00:00
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
|
|
import pytest
|
2016-01-09 23:51:51 +00:00
|
|
|
|
2016-04-21 20:59:42 +00:00
|
|
|
# To prevent circular import when running just this file
|
|
|
|
import homeassistant.components # noqa
|
2018-11-21 11:26:08 +00:00
|
|
|
from homeassistant import core as ha, loader, exceptions
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID
|
2016-10-01 06:26:15 +00:00
|
|
|
from homeassistant.helpers import service, template
|
2018-01-07 22:54:16 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-10-01 06:26:15 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-11-21 11:26:08 +00:00
|
|
|
from homeassistant.auth.permissions import PolicyPermissions
|
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant, mock_service, mock_coro
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_service_platform_call():
|
|
|
|
"""Mock service platform call."""
|
|
|
|
with patch('homeassistant.helpers.service._handle_service_platform_call',
|
|
|
|
side_effect=lambda *args: mock_coro()) as mock_call:
|
|
|
|
yield mock_call
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_entities():
|
|
|
|
"""Return mock entities in an ordered dict."""
|
|
|
|
kitchen = Mock(
|
|
|
|
entity_id='light.kitchen',
|
|
|
|
available=True,
|
|
|
|
should_poll=False,
|
|
|
|
)
|
|
|
|
living_room = Mock(
|
|
|
|
entity_id='light.living_room',
|
|
|
|
available=True,
|
|
|
|
should_poll=False,
|
|
|
|
)
|
|
|
|
entities = OrderedDict()
|
|
|
|
entities[kitchen.entity_id] = kitchen
|
|
|
|
entities[living_room.entity_id] = living_room
|
|
|
|
return entities
|
2016-01-09 23:51:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up 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')
|
|
|
|
|
|
|
|
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-03-10 20:36:05 +00:00
|
|
|
def test_template_service_call(self):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Test service call with templating."""
|
2016-03-10 20:36:05 +00:00
|
|
|
config = {
|
|
|
|
'service_template': '{{ \'test_domain.test_service\' }}',
|
|
|
|
'entity_id': 'hello.world',
|
|
|
|
'data_template': {
|
|
|
|
'hello': '{{ \'goodbye\' }}',
|
2016-09-08 16:19:47 +00:00
|
|
|
'data': {
|
|
|
|
'value': '{{ \'complex\' }}',
|
|
|
|
'simple': 'simple'
|
|
|
|
},
|
|
|
|
'list': ['{{ \'list\' }}', '2'],
|
2016-03-10 20:36:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-03-10 20:36:05 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'goodbye' == self.calls[0].data['hello']
|
|
|
|
assert 'complex' == self.calls[0].data['data']['value']
|
|
|
|
assert 'simple' == self.calls[0].data['data']['simple']
|
|
|
|
assert 'list' == self.calls[0].data['list'][0]
|
2016-03-10 20:36:05 +00:00
|
|
|
|
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 }}',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config, variables={
|
|
|
|
'var_service': 'test_domain.test_service',
|
|
|
|
'var_data': 'goodbye',
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-04-21 19:22:19 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'goodbye' == self.calls[0].data['hello']
|
2016-04-21 19:22:19 +00:00
|
|
|
|
2018-02-05 08:19:56 +00:00
|
|
|
def test_bad_template(self):
|
|
|
|
"""Test passing bad template."""
|
|
|
|
config = {
|
|
|
|
'service_template': '{{ var_service }}',
|
|
|
|
'entity_id': 'hello.world',
|
|
|
|
'data_template': {
|
|
|
|
'hello': '{{ states + unknown_var }}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config, variables={
|
|
|
|
'var_service': 'test_domain.test_service',
|
|
|
|
'var_data': 'goodbye',
|
|
|
|
})
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert len(self.calls) == 0
|
2018-02-05 08:19:56 +00:00
|
|
|
|
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'
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['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-10-01 06:26:15 +00:00
|
|
|
config = cv.SERVICE_SCHEMA({
|
2016-01-10 00:01:27 +00:00
|
|
|
'service': 'test_domain.test_service',
|
|
|
|
'entity_id': 'hello.world, sensor.beer',
|
|
|
|
'data': {
|
|
|
|
'hello': 1,
|
|
|
|
},
|
2016-10-01 06:26:15 +00:00
|
|
|
'data_template': {
|
|
|
|
'nested': {
|
|
|
|
'value': '{{ 1 + 1 }}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
orig = deepcopy(config)
|
|
|
|
|
|
|
|
# Only change after call is each template getting hass attached
|
|
|
|
template.attach(self.hass, orig)
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config, validate_config=False)
|
|
|
|
assert orig == config
|
2016-01-10 00:01:27 +00:00
|
|
|
|
|
|
|
@patch('homeassistant.helpers.service._LOGGER.error')
|
|
|
|
def test_fail_silently_if_no_service(self, mock_log):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Test failing if service is missing."""
|
2016-01-10 00:01:27 +00:00
|
|
|
service.call_from_config(self.hass, None)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == mock_log.call_count
|
2016-01-10 00:01:27 +00:00
|
|
|
|
|
|
|
service.call_from_config(self.hass, {})
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == mock_log.call_count
|
2016-01-10 00:01:27 +00:00
|
|
|
|
|
|
|
service.call_from_config(self.hass, {
|
|
|
|
'service': 'invalid'
|
|
|
|
})
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 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)
|
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
loader.get_component(self.hass, 'group').Group.create_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'})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['light.bowl'] == \
|
|
|
|
service.extract_entity_ids(self.hass, call)
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
call = ha.ServiceCall('light', 'turn_on',
|
|
|
|
{ATTR_ENTITY_ID: 'group.test'})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['light.ceiling', 'light.kitchen'] == \
|
|
|
|
service.extract_entity_ids(self.hass, call)
|
2016-10-29 23:54:26 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['group.test'] == service.extract_entity_ids(
|
|
|
|
self.hass, call, expand_group=False)
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_async_get_all_descriptions(hass):
|
|
|
|
"""Test async_get_all_descriptions."""
|
2018-05-01 18:57:30 +00:00
|
|
|
group = loader.get_component(hass, 'group')
|
2018-01-07 22:54:16 +00:00
|
|
|
group_config = {group.DOMAIN: {}}
|
|
|
|
yield from async_setup_component(hass, group.DOMAIN, group_config)
|
|
|
|
descriptions = yield from service.async_get_all_descriptions(hass)
|
|
|
|
|
|
|
|
assert len(descriptions) == 1
|
|
|
|
|
|
|
|
assert 'description' in descriptions['group']['reload']
|
|
|
|
assert 'fields' in descriptions['group']['reload']
|
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
logger = loader.get_component(hass, 'logger')
|
2018-01-07 22:54:16 +00:00
|
|
|
logger_config = {logger.DOMAIN: {}}
|
|
|
|
yield from async_setup_component(hass, logger.DOMAIN, logger_config)
|
|
|
|
descriptions = yield from service.async_get_all_descriptions(hass)
|
|
|
|
|
|
|
|
assert len(descriptions) == 2
|
|
|
|
|
|
|
|
assert 'description' in descriptions[logger.DOMAIN]['set_level']
|
|
|
|
assert 'fields' in descriptions[logger.DOMAIN]['set_level']
|
2018-11-21 11:26:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_context_user_not_exist(hass):
|
|
|
|
"""Check we don't allow deleted users to do things."""
|
|
|
|
with pytest.raises(exceptions.UnknownUser) as err:
|
|
|
|
await service.entity_service_call(hass, [], Mock(), ha.ServiceCall(
|
|
|
|
'test_domain', 'test_service', context=ha.Context(
|
|
|
|
user_id='non-existing')))
|
|
|
|
|
|
|
|
assert err.value.context.user_id == 'non-existing'
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_context_target_all(hass, mock_service_platform_call,
|
|
|
|
mock_entities):
|
|
|
|
"""Check we only target allowed entities if targetting all."""
|
|
|
|
with patch('homeassistant.auth.AuthManager.async_get_user',
|
|
|
|
return_value=mock_coro(Mock(permissions=PolicyPermissions({
|
|
|
|
'entities': {
|
|
|
|
'entity_ids': {
|
|
|
|
'light.kitchen': True
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 10:41:00 +00:00
|
|
|
}, None)))):
|
2018-11-21 11:26:08 +00:00
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service',
|
|
|
|
context=ha.Context(user_id='mock-id')))
|
|
|
|
|
|
|
|
assert len(mock_service_platform_call.mock_calls) == 1
|
|
|
|
entities = mock_service_platform_call.mock_calls[0][1][2]
|
|
|
|
assert entities == [mock_entities['light.kitchen']]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_context_target_specific(hass, mock_service_platform_call,
|
|
|
|
mock_entities):
|
|
|
|
"""Check targeting specific entities."""
|
|
|
|
with patch('homeassistant.auth.AuthManager.async_get_user',
|
|
|
|
return_value=mock_coro(Mock(permissions=PolicyPermissions({
|
|
|
|
'entities': {
|
|
|
|
'entity_ids': {
|
|
|
|
'light.kitchen': True
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 10:41:00 +00:00
|
|
|
}, None)))):
|
2018-11-21 11:26:08 +00:00
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service', {
|
|
|
|
'entity_id': 'light.kitchen'
|
|
|
|
}, context=ha.Context(user_id='mock-id')))
|
|
|
|
|
|
|
|
assert len(mock_service_platform_call.mock_calls) == 1
|
|
|
|
entities = mock_service_platform_call.mock_calls[0][1][2]
|
|
|
|
assert entities == [mock_entities['light.kitchen']]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_context_target_specific_no_auth(
|
|
|
|
hass, mock_service_platform_call, mock_entities):
|
|
|
|
"""Check targeting specific entities without auth."""
|
|
|
|
with pytest.raises(exceptions.Unauthorized) as err:
|
|
|
|
with patch('homeassistant.auth.AuthManager.async_get_user',
|
|
|
|
return_value=mock_coro(Mock(
|
2018-12-05 10:41:00 +00:00
|
|
|
permissions=PolicyPermissions({}, None)))):
|
2018-11-21 11:26:08 +00:00
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service', {
|
|
|
|
'entity_id': 'light.kitchen'
|
|
|
|
}, context=ha.Context(user_id='mock-id')))
|
|
|
|
|
|
|
|
assert err.value.context.user_id == 'mock-id'
|
|
|
|
assert err.value.entity_id == 'light.kitchen'
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_no_context_target_all(hass, mock_service_platform_call,
|
|
|
|
mock_entities):
|
|
|
|
"""Check we target all if no user context given."""
|
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service'))
|
|
|
|
|
|
|
|
assert len(mock_service_platform_call.mock_calls) == 1
|
|
|
|
entities = mock_service_platform_call.mock_calls[0][1][2]
|
|
|
|
assert entities == list(mock_entities.values())
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_no_context_target_specific(
|
|
|
|
hass, mock_service_platform_call, mock_entities):
|
|
|
|
"""Check we can target specified entities."""
|
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service', {
|
|
|
|
'entity_id': ['light.kitchen', 'light.non-existing']
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert len(mock_service_platform_call.mock_calls) == 1
|
|
|
|
entities = mock_service_platform_call.mock_calls[0][1][2]
|
|
|
|
assert entities == [mock_entities['light.kitchen']]
|
2018-12-13 09:07:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_with_match_all(hass, mock_service_platform_call,
|
|
|
|
mock_entities, caplog):
|
|
|
|
"""Check we only target allowed entities if targetting all."""
|
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service', {
|
|
|
|
'entity_id': 'all'
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert len(mock_service_platform_call.mock_calls) == 1
|
|
|
|
entities = mock_service_platform_call.mock_calls[0][1][2]
|
|
|
|
assert entities == [
|
|
|
|
mock_entities['light.kitchen'], mock_entities['light.living_room']]
|
|
|
|
assert ('Not passing an entity ID to a service to target '
|
|
|
|
'all entities is deprecated') not in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_with_omit_entity_id(hass, mock_service_platform_call,
|
|
|
|
mock_entities, caplog):
|
|
|
|
"""Check we only target allowed entities if targetting all."""
|
|
|
|
await service.entity_service_call(hass, [
|
|
|
|
Mock(entities=mock_entities)
|
|
|
|
], Mock(), ha.ServiceCall('test_domain', 'test_service'))
|
|
|
|
|
|
|
|
assert len(mock_service_platform_call.mock_calls) == 1
|
|
|
|
entities = mock_service_platform_call.mock_calls[0][1][2]
|
|
|
|
assert entities == [
|
|
|
|
mock_entities['light.kitchen'], mock_entities['light.living_room']]
|
|
|
|
assert ('Not passing an entity ID to a service to target '
|
|
|
|
'all entities is deprecated') in caplog.text
|