2016-03-09 10:25:50 +01:00
|
|
|
"""Test service helpers."""
|
2018-11-21 12:26:08 +01:00
|
|
|
from collections import OrderedDict
|
2016-10-01 08:26:15 +02:00
|
|
|
from copy import deepcopy
|
2016-01-10 00:51:51 +01:00
|
|
|
import unittest
|
2021-01-01 22:31:56 +01:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
import pytest
|
2019-12-09 16:52:24 +01:00
|
|
|
import voluptuous as vol
|
2016-01-10 00:51:51 +01:00
|
|
|
|
2016-04-21 22:59:42 +02:00
|
|
|
# To prevent circular import when running just this file
|
2019-04-15 07:31:01 +02:00
|
|
|
from homeassistant import core as ha, exceptions
|
2018-11-21 12:26:08 +01:00
|
|
|
from homeassistant.auth.permissions import PolicyPermissions
|
2020-04-06 12:51:48 +02:00
|
|
|
import homeassistant.components # noqa: F401, pylint: disable=unused-import
|
2020-02-04 23:42:07 +01:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ENTITY_MATCH_ALL,
|
|
|
|
ENTITY_MATCH_NONE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2019-03-04 18:51:12 +01:00
|
|
|
from homeassistant.helpers import (
|
2019-07-31 21:25:30 +02:00
|
|
|
device_registry as dev_reg,
|
|
|
|
entity_registry as ent_reg,
|
2019-12-09 16:52:24 +01:00
|
|
|
service,
|
|
|
|
template,
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2019-12-09 16:52:24 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2019-03-04 18:51:12 +01:00
|
|
|
from tests.common import (
|
2020-01-20 02:55:18 +01:00
|
|
|
MockEntity,
|
2022-01-07 17:42:47 +01:00
|
|
|
async_mock_service,
|
2019-07-31 21:25:30 +02:00
|
|
|
get_test_home_assistant,
|
|
|
|
mock_device_registry,
|
2019-12-09 16:52:24 +01:00
|
|
|
mock_registry,
|
|
|
|
mock_service,
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2020-05-23 18:11:51 +02:00
|
|
|
SUPPORT_A = 1
|
|
|
|
SUPPORT_B = 2
|
|
|
|
SUPPORT_C = 4
|
|
|
|
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
@pytest.fixture
|
2020-02-05 00:30:15 +01:00
|
|
|
def mock_handle_entity_call():
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Mock service platform call."""
|
2020-04-30 22:29:50 +02:00
|
|
|
with patch(
|
2020-08-27 13:56:20 +02:00
|
|
|
"homeassistant.helpers.service._handle_entity_call",
|
|
|
|
return_value=None,
|
2020-04-30 22:29:50 +02:00
|
|
|
) as mock_call:
|
2018-11-21 12:26:08 +01:00
|
|
|
yield mock_call
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-02-05 00:30:15 +01:00
|
|
|
def mock_entities(hass):
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Return mock entities in an ordered dict."""
|
2020-02-05 00:30:15 +01:00
|
|
|
kitchen = MockEntity(
|
2019-07-31 21:25:30 +02:00
|
|
|
entity_id="light.kitchen",
|
2018-11-21 12:26:08 +01:00
|
|
|
available=True,
|
|
|
|
should_poll=False,
|
2020-05-23 18:11:51 +02:00
|
|
|
supported_features=SUPPORT_A,
|
2018-11-21 12:26:08 +01:00
|
|
|
)
|
2020-02-05 00:30:15 +01:00
|
|
|
living_room = MockEntity(
|
2019-07-31 21:25:30 +02:00
|
|
|
entity_id="light.living_room",
|
2018-11-21 12:26:08 +01:00
|
|
|
available=True,
|
|
|
|
should_poll=False,
|
2020-05-23 18:11:51 +02:00
|
|
|
supported_features=SUPPORT_B,
|
|
|
|
)
|
|
|
|
bedroom = MockEntity(
|
|
|
|
entity_id="light.bedroom",
|
|
|
|
available=True,
|
|
|
|
should_poll=False,
|
|
|
|
supported_features=(SUPPORT_A | SUPPORT_B),
|
|
|
|
)
|
|
|
|
bathroom = MockEntity(
|
|
|
|
entity_id="light.bathroom",
|
|
|
|
available=True,
|
|
|
|
should_poll=False,
|
|
|
|
supported_features=(SUPPORT_B | SUPPORT_C),
|
2018-11-21 12:26:08 +01:00
|
|
|
)
|
|
|
|
entities = OrderedDict()
|
|
|
|
entities[kitchen.entity_id] = kitchen
|
|
|
|
entities[living_room.entity_id] = living_room
|
2020-05-23 18:11:51 +02:00
|
|
|
entities[bedroom.entity_id] = bedroom
|
|
|
|
entities[bathroom.entity_id] = bathroom
|
2018-11-21 12:26:08 +01:00
|
|
|
return entities
|
2016-01-10 00:51:51 +01:00
|
|
|
|
|
|
|
|
2020-01-20 02:55:18 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def area_mock(hass):
|
|
|
|
"""Mock including area info."""
|
|
|
|
hass.states.async_set("light.Bowl", STATE_ON)
|
|
|
|
hass.states.async_set("light.Ceiling", STATE_OFF)
|
|
|
|
hass.states.async_set("light.Kitchen", STATE_OFF)
|
|
|
|
|
|
|
|
device_in_area = dev_reg.DeviceEntry(area_id="test-area")
|
2020-11-30 14:27:02 +01:00
|
|
|
device_no_area = dev_reg.DeviceEntry(id="device-no-area-id")
|
2020-01-20 02:55:18 +01:00
|
|
|
device_diff_area = dev_reg.DeviceEntry(area_id="diff-area")
|
2021-04-14 14:12:26 +02:00
|
|
|
device_area_a = dev_reg.DeviceEntry(id="device-area-a-id", area_id="area-a")
|
2020-01-20 02:55:18 +01:00
|
|
|
|
|
|
|
mock_device_registry(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
device_in_area.id: device_in_area,
|
|
|
|
device_no_area.id: device_no_area,
|
|
|
|
device_diff_area.id: device_diff_area,
|
2021-04-14 14:12:26 +02:00
|
|
|
device_area_a.id: device_area_a,
|
2020-01-20 02:55:18 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-10-24 21:25:28 +02:00
|
|
|
entity_in_own_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.in_own_area",
|
|
|
|
unique_id="in-own-area-id",
|
|
|
|
platform="test",
|
|
|
|
area_id="own-area",
|
|
|
|
)
|
2021-10-15 17:46:43 +02:00
|
|
|
config_entity_in_own_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.config_in_own_area",
|
|
|
|
unique_id="config-in-own-area-id",
|
|
|
|
platform="test",
|
|
|
|
area_id="own-area",
|
|
|
|
entity_category="config",
|
|
|
|
)
|
2022-03-15 16:42:22 +01:00
|
|
|
hidden_entity_in_own_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.hidden_in_own_area",
|
|
|
|
unique_id="hidden-in-own-area-id",
|
|
|
|
platform="test",
|
|
|
|
area_id="own-area",
|
|
|
|
hidden_by=ent_reg.RegistryEntryHider.USER,
|
|
|
|
)
|
2020-01-20 02:55:18 +01:00
|
|
|
entity_in_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.in_area",
|
|
|
|
unique_id="in-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_in_area.id,
|
|
|
|
)
|
2021-10-15 17:46:43 +02:00
|
|
|
config_entity_in_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.config_in_area",
|
|
|
|
unique_id="config-in-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_in_area.id,
|
|
|
|
entity_category="config",
|
|
|
|
)
|
2022-03-15 16:42:22 +01:00
|
|
|
hidden_entity_in_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.hidden_in_area",
|
|
|
|
unique_id="hidden-in-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_in_area.id,
|
|
|
|
hidden_by=ent_reg.RegistryEntryHider.USER,
|
|
|
|
)
|
2020-10-24 21:25:28 +02:00
|
|
|
entity_in_other_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.in_other_area",
|
2021-04-14 14:12:26 +02:00
|
|
|
unique_id="in-area-a-id",
|
2020-10-24 21:25:28 +02:00
|
|
|
platform="test",
|
|
|
|
device_id=device_in_area.id,
|
|
|
|
area_id="other-area",
|
|
|
|
)
|
|
|
|
entity_assigned_to_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.assigned_to_area",
|
|
|
|
unique_id="assigned-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_in_area.id,
|
|
|
|
area_id="test-area",
|
|
|
|
)
|
2020-01-20 02:55:18 +01:00
|
|
|
entity_no_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.no_area",
|
|
|
|
unique_id="no-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_no_area.id,
|
|
|
|
)
|
2021-10-15 17:46:43 +02:00
|
|
|
config_entity_no_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.config_no_area",
|
|
|
|
unique_id="config-no-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_no_area.id,
|
|
|
|
entity_category="config",
|
|
|
|
)
|
2022-03-15 16:42:22 +01:00
|
|
|
hidden_entity_no_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.hidden_no_area",
|
|
|
|
unique_id="hidden-no-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_no_area.id,
|
|
|
|
hidden_by=ent_reg.RegistryEntryHider.USER,
|
|
|
|
)
|
2020-01-20 02:55:18 +01:00
|
|
|
entity_diff_area = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.diff_area",
|
|
|
|
unique_id="diff-area-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_diff_area.id,
|
|
|
|
)
|
2021-04-14 14:12:26 +02:00
|
|
|
entity_in_area_a = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.in_area_a",
|
|
|
|
unique_id="in-area-a-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_area_a.id,
|
|
|
|
area_id="area-a",
|
|
|
|
)
|
|
|
|
entity_in_area_b = ent_reg.RegistryEntry(
|
|
|
|
entity_id="light.in_area_b",
|
|
|
|
unique_id="in-area-b-id",
|
|
|
|
platform="test",
|
|
|
|
device_id=device_area_a.id,
|
|
|
|
area_id="area-b",
|
|
|
|
)
|
2020-01-20 02:55:18 +01:00
|
|
|
mock_registry(
|
|
|
|
hass,
|
|
|
|
{
|
2020-10-24 21:25:28 +02:00
|
|
|
entity_in_own_area.entity_id: entity_in_own_area,
|
2021-10-15 17:46:43 +02:00
|
|
|
config_entity_in_own_area.entity_id: config_entity_in_own_area,
|
2022-03-15 16:42:22 +01:00
|
|
|
hidden_entity_in_own_area.entity_id: hidden_entity_in_own_area,
|
2020-01-20 02:55:18 +01:00
|
|
|
entity_in_area.entity_id: entity_in_area,
|
2021-10-15 17:46:43 +02:00
|
|
|
config_entity_in_area.entity_id: config_entity_in_area,
|
2022-03-15 16:42:22 +01:00
|
|
|
hidden_entity_in_area.entity_id: hidden_entity_in_area,
|
2020-10-24 21:25:28 +02:00
|
|
|
entity_in_other_area.entity_id: entity_in_other_area,
|
|
|
|
entity_assigned_to_area.entity_id: entity_assigned_to_area,
|
2020-01-20 02:55:18 +01:00
|
|
|
entity_no_area.entity_id: entity_no_area,
|
2021-10-15 17:46:43 +02:00
|
|
|
config_entity_no_area.entity_id: config_entity_no_area,
|
2022-03-15 16:42:22 +01:00
|
|
|
hidden_entity_no_area.entity_id: hidden_entity_no_area,
|
2020-01-20 02:55:18 +01:00
|
|
|
entity_diff_area.entity_id: entity_diff_area,
|
2021-04-14 14:12:26 +02:00
|
|
|
entity_in_area_a.entity_id: entity_in_area_a,
|
|
|
|
entity_in_area_b.entity_id: entity_in_area_b,
|
2020-01-20 02:55:18 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-01-10 00:51:51 +01:00
|
|
|
class TestServiceHelpers(unittest.TestCase):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test the Home Assistant service helpers."""
|
2016-01-10 00:51:51 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2018-08-19 22:29:08 +02:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-01-10 00:51:51 +01:00
|
|
|
self.hass = get_test_home_assistant()
|
2019-07-31 21:25:30 +02:00
|
|
|
self.calls = mock_service(self.hass, "test_domain", "test_service")
|
2016-01-10 00:51:51 +01:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Stop down everything that was started."""
|
2016-01-10 00:51:51 +01:00
|
|
|
self.hass.stop()
|
|
|
|
|
2020-08-24 16:21:48 +02:00
|
|
|
def test_service_call(self):
|
2018-01-29 23:37:19 +01:00
|
|
|
"""Test service call with templating."""
|
2016-03-10 21:36:05 +01:00
|
|
|
config = {
|
2020-08-24 16:21:48 +02:00
|
|
|
"service": "{{ 'test_domain.test_service' }}",
|
2019-07-31 21:25:30 +02:00
|
|
|
"entity_id": "hello.world",
|
2020-08-24 16:21:48 +02:00
|
|
|
"data": {
|
2019-07-31 21:25:30 +02:00
|
|
|
"hello": "{{ 'goodbye' }}",
|
2020-11-28 23:33:32 +01:00
|
|
|
"effect": {"value": "{{ 'complex' }}", "simple": "simple"},
|
2016-03-10 21:36:05 +01:00
|
|
|
},
|
2020-08-24 16:21:48 +02:00
|
|
|
"data_template": {"list": ["{{ 'list' }}", "2"]},
|
2020-11-28 23:33:32 +01:00
|
|
|
"target": {"area_id": "test-area-id", "entity_id": "will.be_overridden"},
|
2016-03-10 21:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config)
|
2016-09-13 04:16:14 +02:00
|
|
|
self.hass.block_till_done()
|
2016-03-10 21:36:05 +01:00
|
|
|
|
2020-11-28 23:33:32 +01:00
|
|
|
assert dict(self.calls[0].data) == {
|
|
|
|
"hello": "goodbye",
|
|
|
|
"effect": {
|
|
|
|
"value": "complex",
|
|
|
|
"simple": "simple",
|
|
|
|
},
|
|
|
|
"list": ["list", "2"],
|
|
|
|
"entity_id": ["hello.world"],
|
|
|
|
"area_id": ["test-area-id"],
|
|
|
|
}
|
2016-03-10 21:36:05 +01:00
|
|
|
|
2021-02-24 07:46:00 +01:00
|
|
|
config = {
|
|
|
|
"service": "{{ 'test_domain.test_service' }}",
|
|
|
|
"target": {
|
|
|
|
"area_id": ["area-42", "{{ 'area-51' }}"],
|
|
|
|
"device_id": ["abcdef", "{{ 'fedcba' }}"],
|
|
|
|
"entity_id": ["light.static", "{{ 'light.dynamic' }}"],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert dict(self.calls[1].data) == {
|
|
|
|
"area_id": ["area-42", "area-51"],
|
|
|
|
"device_id": ["abcdef", "fedcba"],
|
|
|
|
"entity_id": ["light.static", "light.dynamic"],
|
|
|
|
}
|
|
|
|
|
2021-04-01 22:10:01 +02:00
|
|
|
config = {
|
|
|
|
"service": "{{ 'test_domain.test_service' }}",
|
|
|
|
"target": "{{ var_target }}",
|
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(
|
|
|
|
self.hass,
|
|
|
|
config,
|
|
|
|
variables={
|
|
|
|
"var_target": {
|
|
|
|
"entity_id": "light.static",
|
|
|
|
"area_id": ["area-42", "area-51"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert dict(self.calls[2].data) == {
|
|
|
|
"area_id": ["area-42", "area-51"],
|
|
|
|
"entity_id": ["light.static"],
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:21:48 +02:00
|
|
|
def test_service_template_service_call(self):
|
|
|
|
"""Test legacy service_template call with templating."""
|
|
|
|
config = {
|
|
|
|
"service_template": "{{ 'test_domain.test_service' }}",
|
|
|
|
"entity_id": "hello.world",
|
|
|
|
"data": {"hello": "goodbye"},
|
|
|
|
}
|
|
|
|
|
|
|
|
service.call_from_config(self.hass, config)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert self.calls[0].data["hello"] == "goodbye"
|
|
|
|
|
2016-04-21 21:22:19 +02:00
|
|
|
def test_passing_variables_to_templates(self):
|
2016-04-22 00:52:20 +02:00
|
|
|
"""Test passing variables to templates."""
|
2016-04-21 21:22:19 +02:00
|
|
|
config = {
|
2019-07-31 21:25:30 +02:00
|
|
|
"service_template": "{{ var_service }}",
|
|
|
|
"entity_id": "hello.world",
|
|
|
|
"data_template": {"hello": "{{ var_data }}"},
|
2016-04-21 21:22:19 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
service.call_from_config(
|
|
|
|
self.hass,
|
|
|
|
config,
|
|
|
|
variables={
|
|
|
|
"var_service": "test_domain.test_service",
|
|
|
|
"var_data": "goodbye",
|
|
|
|
},
|
|
|
|
)
|
2016-09-13 04:16:14 +02:00
|
|
|
self.hass.block_till_done()
|
2016-04-21 21:22:19 +02:00
|
|
|
|
2020-04-06 12:51:48 +02:00
|
|
|
assert self.calls[0].data["hello"] == "goodbye"
|
2016-04-21 21:22:19 +02:00
|
|
|
|
2018-02-05 09:19:56 +01:00
|
|
|
def test_bad_template(self):
|
|
|
|
"""Test passing bad template."""
|
|
|
|
config = {
|
2019-07-31 21:25:30 +02:00
|
|
|
"service_template": "{{ var_service }}",
|
|
|
|
"entity_id": "hello.world",
|
|
|
|
"data_template": {"hello": "{{ states + unknown_var }}"},
|
2018-02-05 09:19:56 +01:00
|
|
|
}
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
service.call_from_config(
|
|
|
|
self.hass,
|
|
|
|
config,
|
|
|
|
variables={
|
|
|
|
"var_service": "test_domain.test_service",
|
|
|
|
"var_data": "goodbye",
|
|
|
|
},
|
|
|
|
)
|
2018-02-05 09:19:56 +01:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2018-10-24 12:10:05 +02:00
|
|
|
assert len(self.calls) == 0
|
2018-02-05 09:19:56 +01:00
|
|
|
|
2016-01-10 00:51:51 +01:00
|
|
|
def test_split_entity_string(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test splitting of entity string."""
|
2019-07-31 21:25:30 +02:00
|
|
|
service.call_from_config(
|
|
|
|
self.hass,
|
|
|
|
{
|
|
|
|
"service": "test_domain.test_service",
|
|
|
|
"entity_id": "hello.world, sensor.beer",
|
|
|
|
},
|
|
|
|
)
|
2016-09-13 04:16:14 +02:00
|
|
|
self.hass.block_till_done()
|
2019-07-31 21:25:30 +02:00
|
|
|
assert ["hello.world", "sensor.beer"] == self.calls[-1].data.get("entity_id")
|
2016-01-10 01:01:27 +01:00
|
|
|
|
|
|
|
def test_not_mutate_input(self):
|
2016-03-09 10:25:50 +01:00
|
|
|
"""Test for immutable input."""
|
2019-07-31 21:25:30 +02:00
|
|
|
config = cv.SERVICE_SCHEMA(
|
|
|
|
{
|
|
|
|
"service": "test_domain.test_service",
|
|
|
|
"entity_id": "hello.world, sensor.beer",
|
|
|
|
"data": {"hello": 1},
|
|
|
|
"data_template": {"nested": {"value": "{{ 1 + 1 }}"}},
|
2016-10-01 08:26:15 +02:00
|
|
|
}
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2016-10-01 08:26:15 +02:00
|
|
|
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 01:01:27 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
@patch("homeassistant.helpers.service._LOGGER.error")
|
2016-01-10 01:01:27 +01:00
|
|
|
def test_fail_silently_if_no_service(self, mock_log):
|
2018-01-29 23:37:19 +01:00
|
|
|
"""Test failing if service is missing."""
|
2016-01-10 01:01:27 +01:00
|
|
|
service.call_from_config(self.hass, None)
|
2020-04-06 12:51:48 +02:00
|
|
|
assert mock_log.call_count == 1
|
2016-01-10 01:01:27 +01:00
|
|
|
|
|
|
|
service.call_from_config(self.hass, {})
|
2020-04-06 12:51:48 +02:00
|
|
|
assert mock_log.call_count == 2
|
2016-01-10 01:01:27 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
service.call_from_config(self.hass, {"service": "invalid"})
|
2020-04-06 12:51:48 +02:00
|
|
|
assert mock_log.call_count == 3
|
2016-01-24 07:57:14 +01:00
|
|
|
|
|
|
|
|
2022-01-07 17:42:47 +01:00
|
|
|
async def test_service_call_entry_id(hass):
|
|
|
|
"""Test service call with entity specified by entity registry ID."""
|
|
|
|
registry = ent_reg.async_get(hass)
|
|
|
|
calls = async_mock_service(hass, "test_domain", "test_service")
|
|
|
|
entry = registry.async_get_or_create(
|
|
|
|
"hello", "hue", "1234", suggested_object_id="world"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert entry.entity_id == "hello.world"
|
|
|
|
|
|
|
|
config = {
|
|
|
|
"service": "test_domain.test_service",
|
|
|
|
"target": {"entity_id": entry.id},
|
|
|
|
}
|
|
|
|
|
|
|
|
await service.async_call_from_config(hass, config)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert dict(calls[0].data) == {"entity_id": ["hello.world"]}
|
|
|
|
|
|
|
|
|
2019-03-04 18:51:12 +01:00
|
|
|
async def test_extract_entity_ids(hass):
|
|
|
|
"""Test extract_entity_ids method."""
|
2019-07-31 21:25:30 +02:00
|
|
|
hass.states.async_set("light.Bowl", STATE_ON)
|
|
|
|
hass.states.async_set("light.Ceiling", STATE_OFF)
|
|
|
|
hass.states.async_set("light.Kitchen", STATE_OFF)
|
2019-03-04 18:51:12 +01:00
|
|
|
|
2020-09-30 16:13:53 +02:00
|
|
|
assert await async_setup_component(hass, "group", {})
|
|
|
|
await hass.async_block_till_done()
|
2019-04-15 07:31:01 +02:00
|
|
|
await hass.components.group.Group.async_create_group(
|
2019-07-31 21:25:30 +02:00
|
|
|
hass, "test", ["light.Ceiling", "light.Kitchen"]
|
|
|
|
)
|
2019-03-04 18:51:12 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
call = ha.ServiceCall("light", "turn_on", {ATTR_ENTITY_ID: "light.Bowl"})
|
2019-03-04 18:51:12 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert {"light.bowl"} == await service.async_extract_entity_ids(hass, call)
|
2019-03-04 18:51:12 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
call = ha.ServiceCall("light", "turn_on", {ATTR_ENTITY_ID: "group.test"})
|
2019-03-04 18:51:12 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert {"light.ceiling", "light.kitchen"} == await service.async_extract_entity_ids(
|
|
|
|
hass, call
|
|
|
|
)
|
2019-03-04 18:51:12 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert {"group.test"} == await service.async_extract_entity_ids(
|
|
|
|
hass, call, expand_group=False
|
|
|
|
)
|
2016-01-24 07:57:14 +01:00
|
|
|
|
2020-02-04 23:42:07 +01:00
|
|
|
assert (
|
|
|
|
await service.async_extract_entity_ids(
|
|
|
|
hass,
|
|
|
|
ha.ServiceCall("light", "turn_on", {ATTR_ENTITY_ID: ENTITY_MATCH_NONE}),
|
|
|
|
)
|
|
|
|
== set()
|
|
|
|
)
|
|
|
|
|
2016-01-24 07:57:14 +01:00
|
|
|
|
2020-01-20 02:55:18 +01:00
|
|
|
async def test_extract_entity_ids_from_area(hass, area_mock):
|
2019-03-04 18:51:12 +01:00
|
|
|
"""Test extract_entity_ids method with areas."""
|
2020-10-24 21:25:28 +02:00
|
|
|
call = ha.ServiceCall("light", "turn_on", {"area_id": "own-area"})
|
|
|
|
|
|
|
|
assert {
|
|
|
|
"light.in_own_area",
|
|
|
|
} == await service.async_extract_entity_ids(hass, call)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
call = ha.ServiceCall("light", "turn_on", {"area_id": "test-area"})
|
2016-01-24 07:57:14 +01:00
|
|
|
|
2020-10-24 21:25:28 +02:00
|
|
|
assert {
|
|
|
|
"light.in_area",
|
|
|
|
"light.assigned_to_area",
|
|
|
|
} == await service.async_extract_entity_ids(hass, call)
|
2016-01-24 07:57:14 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
call = ha.ServiceCall("light", "turn_on", {"area_id": ["test-area", "diff-area"]})
|
2016-10-30 01:54:26 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert {
|
|
|
|
"light.in_area",
|
|
|
|
"light.diff_area",
|
2020-10-24 21:25:28 +02:00
|
|
|
"light.assigned_to_area",
|
2019-07-31 21:25:30 +02:00
|
|
|
} == await service.async_extract_entity_ids(hass, call)
|
2018-01-07 23:54:16 +01:00
|
|
|
|
2020-02-04 23:42:07 +01:00
|
|
|
assert (
|
|
|
|
await service.async_extract_entity_ids(
|
|
|
|
hass, ha.ServiceCall("light", "turn_on", {"area_id": ENTITY_MATCH_NONE})
|
|
|
|
)
|
|
|
|
== set()
|
|
|
|
)
|
|
|
|
|
2018-01-07 23:54:16 +01:00
|
|
|
|
2021-04-14 14:12:26 +02:00
|
|
|
async def test_extract_entity_ids_from_devices(hass, area_mock):
|
|
|
|
"""Test extract_entity_ids method with devices."""
|
|
|
|
assert await service.async_extract_entity_ids(
|
|
|
|
hass, ha.ServiceCall("light", "turn_on", {"device_id": "device-no-area-id"})
|
|
|
|
) == {
|
|
|
|
"light.no_area",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert await service.async_extract_entity_ids(
|
|
|
|
hass, ha.ServiceCall("light", "turn_on", {"device_id": "device-area-a-id"})
|
|
|
|
) == {
|
|
|
|
"light.in_area_a",
|
|
|
|
"light.in_area_b",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (
|
|
|
|
await service.async_extract_entity_ids(
|
|
|
|
hass, ha.ServiceCall("light", "turn_on", {"device_id": "non-existing-id"})
|
|
|
|
)
|
|
|
|
== set()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-07 18:33:23 +02:00
|
|
|
async def test_async_get_all_descriptions(hass):
|
2018-01-07 23:54:16 +01:00
|
|
|
"""Test async_get_all_descriptions."""
|
2019-04-15 07:31:01 +02:00
|
|
|
group = hass.components.group
|
2018-01-07 23:54:16 +01:00
|
|
|
group_config = {group.DOMAIN: {}}
|
2020-04-07 18:33:23 +02:00
|
|
|
await async_setup_component(hass, group.DOMAIN, group_config)
|
|
|
|
descriptions = await service.async_get_all_descriptions(hass)
|
2018-01-07 23:54:16 +01:00
|
|
|
|
|
|
|
assert len(descriptions) == 1
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert "description" in descriptions["group"]["reload"]
|
|
|
|
assert "fields" in descriptions["group"]["reload"]
|
2018-01-07 23:54:16 +01:00
|
|
|
|
2019-04-15 07:31:01 +02:00
|
|
|
logger = hass.components.logger
|
2018-01-07 23:54:16 +01:00
|
|
|
logger_config = {logger.DOMAIN: {}}
|
2020-04-07 18:33:23 +02:00
|
|
|
await async_setup_component(hass, logger.DOMAIN, logger_config)
|
|
|
|
descriptions = await service.async_get_all_descriptions(hass)
|
2018-01-07 23:54:16 +01:00
|
|
|
|
|
|
|
assert len(descriptions) == 2
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert "description" in descriptions[logger.DOMAIN]["set_level"]
|
|
|
|
assert "fields" in descriptions[logger.DOMAIN]["set_level"]
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
|
2019-04-10 18:44:58 +02:00
|
|
|
async def test_call_with_required_features(hass, mock_entities):
|
2020-05-23 18:11:51 +02:00
|
|
|
"""Test service calls invoked only if entity has required features."""
|
|
|
|
test_service_mock = AsyncMock(return_value=None)
|
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
test_service_mock,
|
|
|
|
ha.ServiceCall("test_domain", "test_service", {"entity_id": "all"}),
|
|
|
|
required_features=[SUPPORT_A],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert test_service_mock.call_count == 2
|
|
|
|
expected = [
|
|
|
|
mock_entities["light.kitchen"],
|
|
|
|
mock_entities["light.bedroom"],
|
|
|
|
]
|
|
|
|
actual = [call[0][0] for call in test_service_mock.call_args_list]
|
|
|
|
assert all(entity in actual for entity in expected)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_with_both_required_features(hass, mock_entities):
|
|
|
|
"""Test service calls invoked only if entity has both features."""
|
2020-04-30 22:29:50 +02:00
|
|
|
test_service_mock = AsyncMock(return_value=None)
|
2019-07-31 21:25:30 +02:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
test_service_mock,
|
|
|
|
ha.ServiceCall("test_domain", "test_service", {"entity_id": "all"}),
|
2020-05-23 18:11:51 +02:00
|
|
|
required_features=[SUPPORT_A | SUPPORT_B],
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2020-05-23 18:11:51 +02:00
|
|
|
|
2019-04-10 18:44:58 +02:00
|
|
|
assert test_service_mock.call_count == 1
|
2020-05-23 18:11:51 +02:00
|
|
|
assert [call[0][0] for call in test_service_mock.call_args_list] == [
|
|
|
|
mock_entities["light.bedroom"]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_with_one_of_required_features(hass, mock_entities):
|
|
|
|
"""Test service calls invoked with one entity having the required features."""
|
|
|
|
test_service_mock = AsyncMock(return_value=None)
|
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
test_service_mock,
|
|
|
|
ha.ServiceCall("test_domain", "test_service", {"entity_id": "all"}),
|
|
|
|
required_features=[SUPPORT_A, SUPPORT_C],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert test_service_mock.call_count == 3
|
|
|
|
expected = [
|
|
|
|
mock_entities["light.kitchen"],
|
|
|
|
mock_entities["light.bedroom"],
|
|
|
|
mock_entities["light.bathroom"],
|
|
|
|
]
|
|
|
|
actual = [call[0][0] for call in test_service_mock.call_args_list]
|
|
|
|
assert all(entity in actual for entity in expected)
|
2019-04-10 18:44:58 +02:00
|
|
|
|
|
|
|
|
2020-01-30 01:27:25 +01:00
|
|
|
async def test_call_with_sync_func(hass, mock_entities):
|
|
|
|
"""Test invoking sync service calls."""
|
2020-04-30 22:29:50 +02:00
|
|
|
test_service_mock = Mock(return_value=None)
|
2020-01-30 01:27:25 +01:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
test_service_mock,
|
|
|
|
ha.ServiceCall("test_domain", "test_service", {"entity_id": "light.kitchen"}),
|
|
|
|
)
|
|
|
|
assert test_service_mock.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_with_sync_attr(hass, mock_entities):
|
|
|
|
"""Test invoking sync service calls."""
|
2020-04-30 22:29:50 +02:00
|
|
|
mock_method = mock_entities["light.kitchen"].sync_method = Mock(return_value=None)
|
2020-01-30 01:27:25 +01:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
"sync_method",
|
2020-02-03 00:36:39 +01:00
|
|
|
ha.ServiceCall(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
{"entity_id": "light.kitchen", "area_id": "abcd"},
|
|
|
|
),
|
2020-01-30 01:27:25 +01:00
|
|
|
)
|
2020-02-03 00:36:39 +01:00
|
|
|
assert mock_method.call_count == 1
|
|
|
|
# We pass empty kwargs because both entity_id and area_id are filtered out
|
|
|
|
assert mock_method.mock_calls[0][2] == {}
|
2020-01-30 01:27:25 +01:00
|
|
|
|
|
|
|
|
2018-11-21 12:26:08 +01: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:
|
2019-07-31 21:25:30 +02:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[],
|
|
|
|
Mock(),
|
|
|
|
ha.ServiceCall(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
context=ha.Context(user_id="non-existing"),
|
|
|
|
),
|
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert err.value.context.user_id == "non-existing"
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
async def test_call_context_target_all(hass, mock_handle_entity_call, mock_entities):
|
2020-01-31 17:33:00 +01:00
|
|
|
"""Check we only target allowed entities if targeting all."""
|
2019-07-31 21:25:30 +02:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.AuthManager.async_get_user",
|
2020-04-26 00:52:50 +02:00
|
|
|
return_value=Mock(
|
|
|
|
permissions=PolicyPermissions(
|
|
|
|
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
):
|
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
Mock(),
|
|
|
|
ha.ServiceCall(
|
2019-12-03 01:23:12 +01:00
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
data={"entity_id": ENTITY_MATCH_ALL},
|
|
|
|
context=ha.Context(user_id="mock-id"),
|
2019-07-31 21:25:30 +02:00
|
|
|
),
|
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(mock_handle_entity_call.mock_calls) == 1
|
|
|
|
assert mock_handle_entity_call.mock_calls[0][1][1].entity_id == "light.kitchen"
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
async def test_call_context_target_specific(
|
2020-02-05 00:30:15 +01:00
|
|
|
hass, mock_handle_entity_call, mock_entities
|
2019-07-31 21:25:30 +02:00
|
|
|
):
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Check targeting specific entities."""
|
2019-07-31 21:25:30 +02:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.AuthManager.async_get_user",
|
2020-04-26 00:52:50 +02:00
|
|
|
return_value=Mock(
|
|
|
|
permissions=PolicyPermissions(
|
|
|
|
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
|
2019-07-31 21:25:30 +02: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"),
|
|
|
|
),
|
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(mock_handle_entity_call.mock_calls) == 1
|
|
|
|
assert mock_handle_entity_call.mock_calls[0][1][1].entity_id == "light.kitchen"
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_context_target_specific_no_auth(
|
2020-02-05 00:30:15 +01:00
|
|
|
hass, mock_handle_entity_call, mock_entities
|
2019-07-31 21:25:30 +02:00
|
|
|
):
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Check targeting specific entities without auth."""
|
2021-03-27 09:17:15 +01:00
|
|
|
with pytest.raises(exceptions.Unauthorized) as err, patch(
|
|
|
|
"homeassistant.auth.AuthManager.async_get_user",
|
|
|
|
return_value=Mock(permissions=PolicyPermissions({}, None)),
|
|
|
|
):
|
|
|
|
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"),
|
|
|
|
),
|
|
|
|
)
|
2019-07-31 21:25:30 +02:00
|
|
|
|
|
|
|
assert err.value.context.user_id == "mock-id"
|
|
|
|
assert err.value.entity_id == "light.kitchen"
|
|
|
|
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
async def test_call_no_context_target_all(hass, mock_handle_entity_call, mock_entities):
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Check we target all if no user context given."""
|
2019-07-31 21:25:30 +02:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
Mock(),
|
2019-12-03 01:23:12 +01:00
|
|
|
ha.ServiceCall(
|
|
|
|
"test_domain", "test_service", data={"entity_id": ENTITY_MATCH_ALL}
|
|
|
|
),
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2020-05-23 18:11:51 +02:00
|
|
|
assert len(mock_handle_entity_call.mock_calls) == 4
|
2020-02-05 00:30:15 +01:00
|
|
|
assert [call[1][1] for call in mock_handle_entity_call.mock_calls] == list(
|
|
|
|
mock_entities.values()
|
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_no_context_target_specific(
|
2020-02-05 00:30:15 +01:00
|
|
|
hass, mock_handle_entity_call, mock_entities
|
2019-07-31 21:25:30 +02:00
|
|
|
):
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Check we can target specified entities."""
|
2019-07-31 21:25:30 +02:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
Mock(),
|
|
|
|
ha.ServiceCall(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
{"entity_id": ["light.kitchen", "light.non-existing"]},
|
|
|
|
),
|
|
|
|
)
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(mock_handle_entity_call.mock_calls) == 1
|
|
|
|
assert mock_handle_entity_call.mock_calls[0][1][1].entity_id == "light.kitchen"
|
2018-12-13 10:07:59 +01:00
|
|
|
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
async def test_call_with_match_all(
|
2020-02-05 00:30:15 +01:00
|
|
|
hass, mock_handle_entity_call, mock_entities, caplog
|
2019-07-31 21:25:30 +02:00
|
|
|
):
|
2020-01-31 17:33:00 +01:00
|
|
|
"""Check we only target allowed entities if targeting all."""
|
2019-07-31 21:25:30 +02:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
Mock(),
|
|
|
|
ha.ServiceCall("test_domain", "test_service", {"entity_id": "all"}),
|
|
|
|
)
|
2018-12-13 10:07:59 +01:00
|
|
|
|
2020-05-23 18:11:51 +02:00
|
|
|
assert len(mock_handle_entity_call.mock_calls) == 4
|
2020-02-05 00:30:15 +01:00
|
|
|
assert [call[1][1] for call in mock_handle_entity_call.mock_calls] == list(
|
|
|
|
mock_entities.values()
|
|
|
|
)
|
2018-12-13 10:07:59 +01:00
|
|
|
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
async def test_call_with_omit_entity_id(hass, mock_handle_entity_call, mock_entities):
|
2019-12-03 01:23:12 +01:00
|
|
|
"""Check service call if we do not pass an entity ID."""
|
2019-07-31 21:25:30 +02:00
|
|
|
await service.entity_service_call(
|
|
|
|
hass,
|
|
|
|
[Mock(entities=mock_entities)],
|
|
|
|
Mock(),
|
|
|
|
ha.ServiceCall("test_domain", "test_service"),
|
|
|
|
)
|
2018-12-13 10:07:59 +01:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(mock_handle_entity_call.mock_calls) == 0
|
2019-03-13 06:09:50 +01:00
|
|
|
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
async def test_register_admin_service(hass, hass_read_only_user, hass_admin_user):
|
2019-03-13 06:09:50 +01:00
|
|
|
"""Test the register admin service."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def mock_service(call):
|
|
|
|
calls.append(call)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
hass.helpers.service.async_register_admin_service("test", "test", mock_service)
|
2019-03-13 06:09:50 +01:00
|
|
|
hass.helpers.service.async_register_admin_service(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
mock_service,
|
|
|
|
vol.Schema({vol.Required("required"): cv.boolean}),
|
2019-03-13 06:09:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(exceptions.UnknownUser):
|
|
|
|
await hass.services.async_call(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test",
|
|
|
|
"test",
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id="non-existing"),
|
|
|
|
)
|
2019-03-13 06:09:50 +01:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
with pytest.raises(exceptions.Unauthorized):
|
|
|
|
await hass.services.async_call(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test",
|
|
|
|
"test",
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=hass_read_only_user.id),
|
|
|
|
)
|
2019-03-13 06:09:50 +01:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
2019-04-02 18:34:11 +02:00
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
await hass.services.async_call(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test",
|
|
|
|
"test",
|
|
|
|
{"invalid": True},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2019-04-02 18:34:11 +02:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
await hass.services.async_call(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2019-04-02 18:34:11 +02:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
2019-03-13 06:09:50 +01:00
|
|
|
await hass.services.async_call(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
{"required": True},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2019-03-13 06:09:50 +01:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].context.user_id == hass_admin_user.id
|
2019-04-13 21:54:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_domain_control_not_async(hass, mock_entities):
|
|
|
|
"""Test domain verification in a service call with an unknown user."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def mock_service_log(call):
|
|
|
|
"""Define a protected service."""
|
|
|
|
calls.append(call)
|
|
|
|
|
|
|
|
with pytest.raises(exceptions.HomeAssistantError):
|
2019-07-31 21:25:30 +02:00
|
|
|
hass.helpers.service.verify_domain_control("test_domain")(mock_service_log)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_domain_control_unknown(hass, mock_entities):
|
|
|
|
"""Test domain verification in a service call with an unknown user."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def mock_service_log(call):
|
|
|
|
"""Define a protected service."""
|
|
|
|
calls.append(call)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.entity_registry.async_get_registry",
|
2020-04-26 00:52:50 +02:00
|
|
|
return_value=Mock(entities=mock_entities),
|
2019-07-31 21:25:30 +02:00
|
|
|
):
|
2019-04-13 21:54:29 +02:00
|
|
|
protected_mock_service = hass.helpers.service.verify_domain_control(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test_domain"
|
|
|
|
)(mock_service_log)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
|
|
|
hass.services.async_register(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test_domain", "test_service", protected_mock_service, schema=None
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
|
|
|
with pytest.raises(exceptions.UnknownUser):
|
|
|
|
await hass.services.async_call(
|
2019-07-31 21:25:30 +02:00
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
{},
|
2019-04-13 21:54:29 +02:00
|
|
|
blocking=True,
|
2019-07-31 21:25:30 +02:00
|
|
|
context=ha.Context(user_id="fake_user_id"),
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
async def test_domain_control_unauthorized(hass, hass_read_only_user):
|
2019-04-13 21:54:29 +02:00
|
|
|
"""Test domain verification in a service call with an unauthorized user."""
|
2020-02-05 00:30:15 +01:00
|
|
|
mock_registry(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"light.kitchen": ent_reg.RegistryEntry(
|
2020-08-27 13:56:20 +02:00
|
|
|
entity_id="light.kitchen",
|
|
|
|
unique_id="kitchen",
|
|
|
|
platform="test_domain",
|
2020-02-05 00:30:15 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-04-13 21:54:29 +02:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def mock_service_log(call):
|
|
|
|
"""Define a protected service."""
|
|
|
|
calls.append(call)
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")(
|
|
|
|
mock_service_log
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain", "test_service", protected_mock_service, schema=None
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(exceptions.Unauthorized):
|
|
|
|
await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=hass_read_only_user.id),
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(calls) == 0
|
2019-04-13 21:54:29 +02:00
|
|
|
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
async def test_domain_control_admin(hass, hass_admin_user):
|
2019-04-13 21:54:29 +02:00
|
|
|
"""Test domain verification in a service call with an admin user."""
|
2020-02-05 00:30:15 +01:00
|
|
|
mock_registry(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"light.kitchen": ent_reg.RegistryEntry(
|
2020-08-27 13:56:20 +02:00
|
|
|
entity_id="light.kitchen",
|
|
|
|
unique_id="kitchen",
|
|
|
|
platform="test_domain",
|
2020-02-05 00:30:15 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-04-13 21:54:29 +02:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def mock_service_log(call):
|
|
|
|
"""Define a protected service."""
|
|
|
|
calls.append(call)
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")(
|
|
|
|
mock_service_log
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain", "test_service", protected_mock_service, schema=None
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(calls) == 1
|
2019-04-13 21:54:29 +02:00
|
|
|
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
async def test_domain_control_no_user(hass):
|
2019-04-13 21:54:29 +02:00
|
|
|
"""Test domain verification in a service call with no user."""
|
2020-02-05 00:30:15 +01:00
|
|
|
mock_registry(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
"light.kitchen": ent_reg.RegistryEntry(
|
2020-08-27 13:56:20 +02:00
|
|
|
entity_id="light.kitchen",
|
|
|
|
unique_id="kitchen",
|
|
|
|
platform="test_domain",
|
2020-02-05 00:30:15 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-04-13 21:54:29 +02:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def mock_service_log(call):
|
|
|
|
"""Define a protected service."""
|
|
|
|
calls.append(call)
|
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")(
|
|
|
|
mock_service_log
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain", "test_service", protected_mock_service, schema=None
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
{},
|
|
|
|
blocking=True,
|
|
|
|
context=ha.Context(user_id=None),
|
|
|
|
)
|
2019-04-13 21:54:29 +02:00
|
|
|
|
2020-02-05 00:30:15 +01:00
|
|
|
assert len(calls) == 1
|
2020-01-20 02:55:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_extract_from_service_available_device(hass):
|
|
|
|
"""Test the extraction of entity from service and device is available."""
|
|
|
|
entities = [
|
|
|
|
MockEntity(name="test_1", entity_id="test_domain.test_1"),
|
|
|
|
MockEntity(name="test_2", entity_id="test_domain.test_2", available=False),
|
|
|
|
MockEntity(name="test_3", entity_id="test_domain.test_3"),
|
|
|
|
MockEntity(name="test_4", entity_id="test_domain.test_4", available=False),
|
|
|
|
]
|
|
|
|
|
|
|
|
call_1 = ha.ServiceCall("test", "service", data={"entity_id": ENTITY_MATCH_ALL})
|
|
|
|
|
|
|
|
assert ["test_domain.test_1", "test_domain.test_3"] == [
|
|
|
|
ent.entity_id
|
|
|
|
for ent in (await service.async_extract_entities(hass, entities, call_1))
|
|
|
|
]
|
|
|
|
|
|
|
|
call_2 = ha.ServiceCall(
|
|
|
|
"test",
|
|
|
|
"service",
|
|
|
|
data={"entity_id": ["test_domain.test_3", "test_domain.test_4"]},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert ["test_domain.test_3"] == [
|
|
|
|
ent.entity_id
|
|
|
|
for ent in (await service.async_extract_entities(hass, entities, call_2))
|
|
|
|
]
|
|
|
|
|
2020-02-04 23:42:07 +01:00
|
|
|
assert (
|
|
|
|
await service.async_extract_entities(
|
|
|
|
hass,
|
|
|
|
entities,
|
2020-08-27 13:56:20 +02:00
|
|
|
ha.ServiceCall(
|
|
|
|
"test",
|
|
|
|
"service",
|
|
|
|
data={"entity_id": ENTITY_MATCH_NONE},
|
|
|
|
),
|
2020-02-04 23:42:07 +01:00
|
|
|
)
|
|
|
|
== []
|
|
|
|
)
|
|
|
|
|
2020-01-20 02:55:18 +01:00
|
|
|
|
|
|
|
async def test_extract_from_service_empty_if_no_entity_id(hass):
|
|
|
|
"""Test the extraction from service without specifying entity."""
|
|
|
|
entities = [
|
|
|
|
MockEntity(name="test_1", entity_id="test_domain.test_1"),
|
|
|
|
MockEntity(name="test_2", entity_id="test_domain.test_2"),
|
|
|
|
]
|
|
|
|
call = ha.ServiceCall("test", "service")
|
|
|
|
|
|
|
|
assert [] == [
|
|
|
|
ent.entity_id
|
|
|
|
for ent in (await service.async_extract_entities(hass, entities, call))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_extract_from_service_filter_out_non_existing_entities(hass):
|
|
|
|
"""Test the extraction of non existing entities from service."""
|
|
|
|
entities = [
|
|
|
|
MockEntity(name="test_1", entity_id="test_domain.test_1"),
|
|
|
|
MockEntity(name="test_2", entity_id="test_domain.test_2"),
|
|
|
|
]
|
|
|
|
|
|
|
|
call = ha.ServiceCall(
|
|
|
|
"test",
|
|
|
|
"service",
|
|
|
|
{"entity_id": ["test_domain.test_2", "test_domain.non_exist"]},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert ["test_domain.test_2"] == [
|
|
|
|
ent.entity_id
|
|
|
|
for ent in (await service.async_extract_entities(hass, entities, call))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_extract_from_service_area_id(hass, area_mock):
|
|
|
|
"""Test the extraction using area ID as reference."""
|
|
|
|
entities = [
|
|
|
|
MockEntity(name="in_area", entity_id="light.in_area"),
|
|
|
|
MockEntity(name="no_area", entity_id="light.no_area"),
|
|
|
|
MockEntity(name="diff_area", entity_id="light.diff_area"),
|
|
|
|
]
|
|
|
|
|
|
|
|
call = ha.ServiceCall("light", "turn_on", {"area_id": "test-area"})
|
|
|
|
extracted = await service.async_extract_entities(hass, entities, call)
|
|
|
|
assert len(extracted) == 1
|
|
|
|
assert extracted[0].entity_id == "light.in_area"
|
|
|
|
|
|
|
|
call = ha.ServiceCall("light", "turn_on", {"area_id": ["test-area", "diff-area"]})
|
|
|
|
extracted = await service.async_extract_entities(hass, entities, call)
|
|
|
|
assert len(extracted) == 2
|
|
|
|
assert sorted(ent.entity_id for ent in extracted) == [
|
|
|
|
"light.diff_area",
|
|
|
|
"light.in_area",
|
|
|
|
]
|
2020-11-30 14:27:02 +01:00
|
|
|
|
|
|
|
call = ha.ServiceCall(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"area_id": ["test-area", "diff-area"], "device_id": "device-no-area-id"},
|
|
|
|
)
|
|
|
|
extracted = await service.async_extract_entities(hass, entities, call)
|
|
|
|
assert len(extracted) == 3
|
|
|
|
assert sorted(ent.entity_id for ent in extracted) == [
|
|
|
|
"light.diff_area",
|
|
|
|
"light.in_area",
|
|
|
|
"light.no_area",
|
|
|
|
]
|
2020-12-01 08:01:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_service_call_warn_referenced(hass, caplog):
|
|
|
|
"""Test we only warn for referenced entities in entity_service_call."""
|
|
|
|
call = ha.ServiceCall(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{
|
|
|
|
"area_id": "non-existent-area",
|
|
|
|
"entity_id": "non.existent",
|
|
|
|
"device_id": "non-existent-device",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await service.entity_service_call(hass, {}, "", call)
|
|
|
|
assert (
|
|
|
|
"Unable to find referenced areas non-existent-area, devices non-existent-device, entities non.existent"
|
|
|
|
in caplog.text
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_async_extract_entities_warn_referenced(hass, caplog):
|
|
|
|
"""Test we only warn for referenced entities in async_extract_entities."""
|
|
|
|
call = ha.ServiceCall(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{
|
|
|
|
"area_id": "non-existent-area",
|
|
|
|
"entity_id": "non.existent",
|
|
|
|
"device_id": "non-existent-device",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
extracted = await service.async_extract_entities(hass, {}, call)
|
|
|
|
assert len(extracted) == 0
|
|
|
|
assert (
|
|
|
|
"Unable to find referenced areas non-existent-area, devices non-existent-device, entities non.existent"
|
|
|
|
in caplog.text
|
|
|
|
)
|
2021-03-18 05:27:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_async_extract_config_entry_ids(hass):
|
|
|
|
"""Test we can find devices that have no entities."""
|
|
|
|
|
|
|
|
device_no_entities = dev_reg.DeviceEntry(
|
|
|
|
id="device-no-entities", config_entries={"abc"}
|
|
|
|
)
|
|
|
|
|
|
|
|
call = ha.ServiceCall(
|
|
|
|
"homeassistant",
|
|
|
|
"reload_config_entry",
|
|
|
|
{
|
|
|
|
"device_id": "device-no-entities",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_device_registry(
|
|
|
|
hass,
|
|
|
|
{
|
|
|
|
device_no_entities.id: device_no_entities,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert await service.async_extract_config_entry_ids(hass, call) == {"abc"}
|