2016-01-09 00:58:44 +00:00
|
|
|
"""Service calling related helpers."""
|
2021-02-12 09:58:20 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-08-16 07:50:11 +00:00
|
|
|
import asyncio
|
2020-12-01 07:01:27 +00:00
|
|
|
import dataclasses
|
2020-01-30 00:27:25 +00:00
|
|
|
from functools import partial, wraps
|
2016-01-09 00:58:44 +00:00
|
|
|
import logging
|
2021-03-18 21:58:19 +00:00
|
|
|
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, TypedDict
|
2016-08-07 23:26:35 +00:00
|
|
|
|
2016-04-21 19:22:19 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-04-13 19:54:29 +00:00
|
|
|
from homeassistant.auth.permissions.const import CAT_ENTITIES, POLICY_CONTROL
|
2020-02-04 22:42:07 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_AREA_ID,
|
2020-11-30 13:27:02 +00:00
|
|
|
ATTR_DEVICE_ID,
|
2020-02-04 22:42:07 +00:00
|
|
|
ATTR_ENTITY_ID,
|
2021-02-24 06:46:00 +00:00
|
|
|
CONF_ENTITY_ID,
|
2020-03-05 19:44:42 +00:00
|
|
|
CONF_SERVICE,
|
2021-02-08 21:53:17 +00:00
|
|
|
CONF_SERVICE_DATA,
|
2020-03-05 19:44:42 +00:00
|
|
|
CONF_SERVICE_TEMPLATE,
|
2020-11-28 22:33:32 +00:00
|
|
|
CONF_TARGET,
|
2020-02-04 22:42:07 +00:00
|
|
|
ENTITY_MATCH_ALL,
|
|
|
|
ENTITY_MATCH_NONE,
|
|
|
|
)
|
2018-01-07 22:54:16 +00:00
|
|
|
import homeassistant.core as ha
|
2019-04-12 17:09:17 +00:00
|
|
|
from homeassistant.exceptions import (
|
2019-07-31 19:25:30 +00:00
|
|
|
HomeAssistantError,
|
|
|
|
TemplateError,
|
|
|
|
Unauthorized,
|
|
|
|
UnknownUser,
|
|
|
|
)
|
2020-12-01 07:01:27 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
area_registry,
|
|
|
|
config_validation as cv,
|
|
|
|
device_registry,
|
|
|
|
entity_registry,
|
|
|
|
template,
|
|
|
|
)
|
2020-04-17 18:33:58 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType, TemplateVarsType
|
2020-11-11 07:34:54 +00:00
|
|
|
from homeassistant.loader import (
|
|
|
|
MAX_LOAD_CONCURRENTLY,
|
|
|
|
Integration,
|
|
|
|
async_get_integration,
|
|
|
|
bind_hass,
|
|
|
|
)
|
|
|
|
from homeassistant.util.async_ import gather_with_concurrency
|
2018-01-07 22:54:16 +00:00
|
|
|
from homeassistant.util.yaml import load_yaml
|
2019-08-15 15:53:25 +00:00
|
|
|
from homeassistant.util.yaml.loader import JSON_TYPE
|
2019-07-21 16:59:02 +00:00
|
|
|
|
2020-04-19 21:41:52 +00:00
|
|
|
if TYPE_CHECKING:
|
2021-03-02 08:02:04 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2020-09-20 09:03:58 +00:00
|
|
|
from homeassistant.helpers.entity_platform import EntityPlatform
|
2020-04-19 21:41:52 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SERVICE_ENTITY_ID = "entity_id"
|
|
|
|
CONF_SERVICE_DATA_TEMPLATE = "data_template"
|
2016-01-09 00:58:44 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_DESCRIPTION_CACHE = "service_description_cache"
|
2018-01-07 22:54:16 +00:00
|
|
|
|
2016-01-09 00:58:44 +00:00
|
|
|
|
2021-02-10 11:42:28 +00:00
|
|
|
class ServiceParams(TypedDict):
|
|
|
|
"""Type for service call parameters."""
|
|
|
|
|
|
|
|
domain: str
|
|
|
|
service: str
|
2021-03-17 17:34:19 +00:00
|
|
|
service_data: dict[str, Any]
|
|
|
|
target: dict | None
|
2021-02-10 11:42:28 +00:00
|
|
|
|
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
class ServiceTargetSelector:
|
|
|
|
"""Class to hold a target selector for a service."""
|
|
|
|
|
|
|
|
def __init__(self, service_call: ha.ServiceCall):
|
|
|
|
"""Extract ids from service call data."""
|
2021-03-18 21:58:19 +00:00
|
|
|
entity_ids: str | list | None = service_call.data.get(ATTR_ENTITY_ID)
|
|
|
|
device_ids: str | list | None = service_call.data.get(ATTR_DEVICE_ID)
|
|
|
|
area_ids: str | list | None = service_call.data.get(ATTR_AREA_ID)
|
2021-03-18 04:27:21 +00:00
|
|
|
|
|
|
|
self.entity_ids = (
|
|
|
|
set(cv.ensure_list(entity_ids)) if _has_match(entity_ids) else set()
|
|
|
|
)
|
|
|
|
self.device_ids = (
|
|
|
|
set(cv.ensure_list(device_ids)) if _has_match(device_ids) else set()
|
|
|
|
)
|
|
|
|
self.area_ids = set(cv.ensure_list(area_ids)) if _has_match(area_ids) else set()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_any_selector(self) -> bool:
|
|
|
|
"""Determine if any selectors are present."""
|
|
|
|
return bool(self.entity_ids or self.device_ids or self.area_ids)
|
|
|
|
|
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
@dataclasses.dataclass
|
|
|
|
class SelectedEntities:
|
|
|
|
"""Class to hold the selected entities."""
|
|
|
|
|
|
|
|
# Entities that were explicitly mentioned.
|
2021-03-17 17:34:19 +00:00
|
|
|
referenced: set[str] = dataclasses.field(default_factory=set)
|
2020-12-01 07:01:27 +00:00
|
|
|
|
|
|
|
# Entities that were referenced via device/area ID.
|
|
|
|
# Should not trigger a warning when they don't exist.
|
2021-03-17 17:34:19 +00:00
|
|
|
indirectly_referenced: set[str] = dataclasses.field(default_factory=set)
|
2020-12-01 07:01:27 +00:00
|
|
|
|
|
|
|
# Referenced items that could not be found.
|
2021-03-17 17:34:19 +00:00
|
|
|
missing_devices: set[str] = dataclasses.field(default_factory=set)
|
|
|
|
missing_areas: set[str] = dataclasses.field(default_factory=set)
|
2020-12-01 07:01:27 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
# Referenced devices
|
|
|
|
referenced_devices: set[str] = dataclasses.field(default_factory=set)
|
|
|
|
|
2021-03-17 17:34:19 +00:00
|
|
|
def log_missing(self, missing_entities: set[str]) -> None:
|
2020-12-01 07:01:27 +00:00
|
|
|
"""Log about missing items."""
|
|
|
|
parts = []
|
|
|
|
for label, items in (
|
|
|
|
("areas", self.missing_areas),
|
|
|
|
("devices", self.missing_devices),
|
|
|
|
("entities", missing_entities),
|
|
|
|
):
|
|
|
|
if items:
|
|
|
|
parts.append(f"{label} {', '.join(sorted(items))}")
|
|
|
|
|
|
|
|
if not parts:
|
|
|
|
return
|
|
|
|
|
|
|
|
_LOGGER.warning("Unable to find referenced %s", ", ".join(parts))
|
|
|
|
|
|
|
|
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2019-07-31 19:25:30 +00:00
|
|
|
def call_from_config(
|
2020-04-17 18:33:58 +00:00
|
|
|
hass: HomeAssistantType,
|
|
|
|
config: ConfigType,
|
|
|
|
blocking: bool = False,
|
|
|
|
variables: TemplateVarsType = None,
|
|
|
|
validate_config: bool = True,
|
|
|
|
) -> None:
|
2016-01-09 00:58:44 +00:00
|
|
|
"""Call a service based on a config hash."""
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_call_from_config(hass, config, blocking, variables, validate_config),
|
|
|
|
hass.loop,
|
|
|
|
).result()
|
2016-10-01 05:34:45 +00:00
|
|
|
|
|
|
|
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_call_from_config(
|
2020-04-17 18:33:58 +00:00
|
|
|
hass: HomeAssistantType,
|
|
|
|
config: ConfigType,
|
|
|
|
blocking: bool = False,
|
|
|
|
variables: TemplateVarsType = None,
|
|
|
|
validate_config: bool = True,
|
2021-03-17 17:34:19 +00:00
|
|
|
context: ha.Context | None = None,
|
2020-04-17 18:33:58 +00:00
|
|
|
) -> None:
|
2016-10-01 05:34:45 +00:00
|
|
|
"""Call a service based on a config hash."""
|
2020-03-11 23:34:50 +00:00
|
|
|
try:
|
2020-12-31 18:14:07 +00:00
|
|
|
params = async_prepare_call_from_config(
|
|
|
|
hass, config, variables, validate_config
|
|
|
|
)
|
2020-03-11 23:34:50 +00:00
|
|
|
except HomeAssistantError as ex:
|
|
|
|
if blocking:
|
|
|
|
raise
|
|
|
|
_LOGGER.error(ex)
|
|
|
|
else:
|
2021-02-10 11:42:28 +00:00
|
|
|
await hass.services.async_call(**params, blocking=blocking, context=context)
|
2020-03-11 23:34:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
@bind_hass
|
2020-04-17 18:33:58 +00:00
|
|
|
def async_prepare_call_from_config(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
config: ConfigType,
|
|
|
|
variables: TemplateVarsType = None,
|
|
|
|
validate_config: bool = False,
|
2021-02-10 11:42:28 +00:00
|
|
|
) -> ServiceParams:
|
2020-03-11 23:34:50 +00:00
|
|
|
"""Prepare to call a service based on a config hash."""
|
2016-04-23 05:11:21 +00:00
|
|
|
if validate_config:
|
|
|
|
try:
|
|
|
|
config = cv.SERVICE_SCHEMA(config)
|
|
|
|
except vol.Invalid as ex:
|
2020-03-11 23:34:50 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Invalid config for calling service: {ex}"
|
|
|
|
) from ex
|
2016-01-10 00:01:27 +00:00
|
|
|
|
2016-04-21 19:22:19 +00:00
|
|
|
if CONF_SERVICE in config:
|
|
|
|
domain_service = config[CONF_SERVICE]
|
2016-01-09 00:58:44 +00:00
|
|
|
else:
|
2020-08-24 14:21:48 +00:00
|
|
|
domain_service = config[CONF_SERVICE_TEMPLATE]
|
|
|
|
|
2020-11-30 13:27:02 +00:00
|
|
|
if isinstance(domain_service, template.Template):
|
2016-04-21 19:22:19 +00:00
|
|
|
try:
|
2020-08-24 14:21:48 +00:00
|
|
|
domain_service.hass = hass
|
|
|
|
domain_service = domain_service.async_render(variables)
|
2016-04-21 19:22:19 +00:00
|
|
|
domain_service = cv.service(domain_service)
|
|
|
|
except TemplateError as ex:
|
2020-03-11 23:34:50 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Error rendering service name template: {ex}"
|
|
|
|
) from ex
|
|
|
|
except vol.Invalid as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Template rendered invalid service: {domain_service}"
|
|
|
|
) from ex
|
|
|
|
|
|
|
|
domain, service = domain_service.split(".", 1)
|
2016-04-21 19:22:19 +00:00
|
|
|
|
2021-02-24 06:46:00 +00:00
|
|
|
target = {}
|
|
|
|
if CONF_TARGET in config:
|
|
|
|
conf = config.get(CONF_TARGET)
|
|
|
|
try:
|
|
|
|
template.attach(hass, conf)
|
|
|
|
target.update(template.render_complex(conf, variables))
|
|
|
|
if CONF_ENTITY_ID in target:
|
|
|
|
target[CONF_ENTITY_ID] = cv.comp_entity_ids(target[CONF_ENTITY_ID])
|
|
|
|
except TemplateError as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Error rendering service target template: {ex}"
|
|
|
|
) from ex
|
|
|
|
except vol.Invalid as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Template rendered invalid entity IDs: {target[CONF_ENTITY_ID]}"
|
|
|
|
) from ex
|
2020-11-28 22:33:32 +00:00
|
|
|
|
2021-02-10 11:42:28 +00:00
|
|
|
service_data = {}
|
2020-11-28 22:33:32 +00:00
|
|
|
|
2020-08-24 14:21:48 +00:00
|
|
|
for conf in [CONF_SERVICE_DATA, CONF_SERVICE_DATA_TEMPLATE]:
|
|
|
|
if conf not in config:
|
|
|
|
continue
|
2018-01-19 06:13:14 +00:00
|
|
|
try:
|
2020-08-24 14:21:48 +00:00
|
|
|
template.attach(hass, config[conf])
|
|
|
|
service_data.update(template.render_complex(config[conf], variables))
|
2018-01-19 06:13:14 +00:00
|
|
|
except TemplateError as ex:
|
2020-03-11 23:34:50 +00:00
|
|
|
raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
|
2016-04-21 19:22:19 +00:00
|
|
|
|
|
|
|
if CONF_SERVICE_ENTITY_ID in config:
|
2021-02-10 11:42:28 +00:00
|
|
|
if target:
|
|
|
|
target[ATTR_ENTITY_ID] = config[CONF_SERVICE_ENTITY_ID]
|
|
|
|
else:
|
|
|
|
target = {ATTR_ENTITY_ID: config[CONF_SERVICE_ENTITY_ID]}
|
2016-01-09 00:58:44 +00:00
|
|
|
|
2021-02-10 11:42:28 +00:00
|
|
|
return {
|
|
|
|
"domain": domain,
|
|
|
|
"service": service,
|
|
|
|
"service_data": service_data,
|
|
|
|
"target": target,
|
|
|
|
}
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2020-04-17 18:33:58 +00:00
|
|
|
def extract_entity_ids(
|
|
|
|
hass: HomeAssistantType, service_call: ha.ServiceCall, expand_group: bool = True
|
2021-03-17 17:34:19 +00:00
|
|
|
) -> set[str]:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Extract a list of entity ids from a service call.
|
2016-03-07 22:39:52 +00:00
|
|
|
|
2016-01-24 06:57:14 +00:00
|
|
|
Will convert group entity ids to the entity ids it represents.
|
|
|
|
"""
|
2019-10-01 14:59:06 +00:00
|
|
|
return asyncio.run_coroutine_threadsafe(
|
2019-03-04 17:51:12 +00:00
|
|
|
async_extract_entity_ids(hass, service_call, expand_group), hass.loop
|
|
|
|
).result()
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
|
2020-01-20 01:55:18 +00:00
|
|
|
@bind_hass
|
2020-04-17 18:33:58 +00:00
|
|
|
async def async_extract_entities(
|
|
|
|
hass: HomeAssistantType,
|
2021-02-12 09:58:20 +00:00
|
|
|
entities: Iterable[Entity],
|
2020-04-17 18:33:58 +00:00
|
|
|
service_call: ha.ServiceCall,
|
|
|
|
expand_group: bool = True,
|
2021-03-17 17:34:19 +00:00
|
|
|
) -> list[Entity]:
|
2020-01-20 01:55:18 +00:00
|
|
|
"""Extract a list of entity objects from a service call.
|
|
|
|
|
|
|
|
Will convert group entity ids to the entity ids it represents.
|
|
|
|
"""
|
|
|
|
data_ent_id = service_call.data.get(ATTR_ENTITY_ID)
|
|
|
|
|
|
|
|
if data_ent_id == ENTITY_MATCH_ALL:
|
|
|
|
return [entity for entity in entities if entity.available]
|
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
referenced = await async_extract_referenced_entity_ids(
|
|
|
|
hass, service_call, expand_group
|
|
|
|
)
|
|
|
|
combined = referenced.referenced | referenced.indirectly_referenced
|
2020-01-20 01:55:18 +00:00
|
|
|
|
2020-02-04 22:42:07 +00:00
|
|
|
found = []
|
|
|
|
|
|
|
|
for entity in entities:
|
2020-12-01 07:01:27 +00:00
|
|
|
if entity.entity_id not in combined:
|
2020-02-04 22:42:07 +00:00
|
|
|
continue
|
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
combined.remove(entity.entity_id)
|
2020-02-04 22:42:07 +00:00
|
|
|
|
|
|
|
if not entity.available:
|
|
|
|
continue
|
|
|
|
|
|
|
|
found.append(entity)
|
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
referenced.log_missing(referenced.referenced & combined)
|
2020-02-04 22:42:07 +00:00
|
|
|
|
|
|
|
return found
|
2020-01-20 01:55:18 +00:00
|
|
|
|
|
|
|
|
2019-03-04 17:51:12 +00:00
|
|
|
@bind_hass
|
2020-04-17 18:33:58 +00:00
|
|
|
async def async_extract_entity_ids(
|
|
|
|
hass: HomeAssistantType, service_call: ha.ServiceCall, expand_group: bool = True
|
2021-03-17 17:34:19 +00:00
|
|
|
) -> set[str]:
|
2020-12-01 07:01:27 +00:00
|
|
|
"""Extract a set of entity ids from a service call.
|
2016-01-24 06:57:14 +00:00
|
|
|
|
2019-03-04 17:51:12 +00:00
|
|
|
Will convert group entity ids to the entity ids it represents.
|
|
|
|
"""
|
2020-12-01 07:01:27 +00:00
|
|
|
referenced = await async_extract_referenced_entity_ids(
|
|
|
|
hass, service_call, expand_group
|
|
|
|
)
|
|
|
|
return referenced.referenced | referenced.indirectly_referenced
|
|
|
|
|
|
|
|
|
2021-03-18 21:58:19 +00:00
|
|
|
def _has_match(ids: str | list | None) -> bool:
|
2021-03-18 04:27:21 +00:00
|
|
|
"""Check if ids can match anything."""
|
|
|
|
return ids not in (None, ENTITY_MATCH_NONE)
|
|
|
|
|
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
@bind_hass
|
|
|
|
async def async_extract_referenced_entity_ids(
|
|
|
|
hass: HomeAssistantType, service_call: ha.ServiceCall, expand_group: bool = True
|
|
|
|
) -> SelectedEntities:
|
|
|
|
"""Extract referenced entity IDs from a service call."""
|
2021-03-18 04:27:21 +00:00
|
|
|
selector = ServiceTargetSelector(service_call)
|
2020-12-01 07:01:27 +00:00
|
|
|
selected = SelectedEntities()
|
2019-03-04 17:51:12 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
if not selector.has_any_selector:
|
2020-12-01 07:01:27 +00:00
|
|
|
return selected
|
2020-02-04 22:42:07 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
entity_ids = selector.entity_ids
|
|
|
|
if expand_group:
|
|
|
|
entity_ids = hass.components.group.expand_entity_ids(entity_ids)
|
2020-12-01 07:01:27 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
selected.referenced.update(entity_ids)
|
2019-03-04 17:51:12 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
if not selector.device_ids and not selector.area_ids:
|
2020-12-01 07:01:27 +00:00
|
|
|
return selected
|
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
ent_reg = entity_registry.async_get(hass)
|
|
|
|
dev_reg = device_registry.async_get(hass)
|
|
|
|
area_reg = area_registry.async_get(hass)
|
2020-11-30 13:27:02 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
for device_id in selector.device_ids:
|
|
|
|
if device_id not in dev_reg.devices:
|
|
|
|
selected.missing_devices.add(device_id)
|
2020-12-01 07:01:27 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
for area_id in selector.area_ids:
|
|
|
|
if area_id not in area_reg.areas:
|
|
|
|
selected.missing_areas.add(area_id)
|
2020-10-24 19:25:28 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
# Find devices for this area
|
|
|
|
selected.referenced_devices.update(selector.device_ids)
|
|
|
|
for device_entry in dev_reg.devices.values():
|
|
|
|
if device_entry.area_id in selector.area_ids:
|
|
|
|
selected.referenced_devices.add(device_entry.id)
|
2020-10-24 19:25:28 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
if not selector.area_ids and not selected.referenced_devices:
|
2020-12-01 07:01:27 +00:00
|
|
|
return selected
|
2020-11-30 13:27:02 +00:00
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
for ent_entry in ent_reg.entities.values():
|
|
|
|
if ent_entry.area_id in selector.area_ids or (
|
|
|
|
not ent_entry.area_id and ent_entry.device_id in selected.referenced_devices
|
|
|
|
):
|
|
|
|
selected.indirectly_referenced.add(ent_entry.entity_id)
|
2020-11-30 13:27:02 +00:00
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
return selected
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
|
2021-03-18 04:27:21 +00:00
|
|
|
@bind_hass
|
|
|
|
async def async_extract_config_entry_ids(
|
|
|
|
hass: HomeAssistantType, service_call: ha.ServiceCall, expand_group: bool = True
|
|
|
|
) -> set:
|
|
|
|
"""Extract referenced config entry ids from a service call."""
|
|
|
|
referenced = await async_extract_referenced_entity_ids(
|
|
|
|
hass, service_call, expand_group
|
|
|
|
)
|
|
|
|
ent_reg = entity_registry.async_get(hass)
|
|
|
|
dev_reg = device_registry.async_get(hass)
|
|
|
|
config_entry_ids: set[str] = set()
|
|
|
|
|
|
|
|
# Some devices may have no entities
|
|
|
|
for device_id in referenced.referenced_devices:
|
|
|
|
if device_id in dev_reg.devices:
|
|
|
|
device = dev_reg.async_get(device_id)
|
|
|
|
if device is not None:
|
|
|
|
config_entry_ids.update(device.config_entries)
|
|
|
|
|
|
|
|
for entity_id in referenced.referenced | referenced.indirectly_referenced:
|
|
|
|
entry = ent_reg.async_get(entity_id)
|
|
|
|
if entry is not None and entry.config_entry_id is not None:
|
|
|
|
config_entry_ids.add(entry.config_entry_id)
|
|
|
|
|
|
|
|
return config_entry_ids
|
|
|
|
|
|
|
|
|
2020-10-21 20:24:50 +00:00
|
|
|
def _load_services_file(hass: HomeAssistantType, integration: Integration) -> JSON_TYPE:
|
2019-04-12 17:09:17 +00:00
|
|
|
"""Load services file for an integration."""
|
|
|
|
try:
|
2020-10-21 20:24:50 +00:00
|
|
|
return load_yaml(str(integration.file_path / "services.yaml"))
|
2019-04-12 17:09:17 +00:00
|
|
|
except FileNotFoundError:
|
2020-10-21 20:24:50 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Unable to find services.yaml for the %s integration", integration.domain
|
|
|
|
)
|
2019-04-12 17:09:17 +00:00
|
|
|
return {}
|
|
|
|
except HomeAssistantError:
|
2020-10-21 20:24:50 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Unable to parse services.yaml for the %s integration", integration.domain
|
|
|
|
)
|
2019-04-12 17:09:17 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2020-10-21 20:24:50 +00:00
|
|
|
def _load_services_files(
|
|
|
|
hass: HomeAssistantType, integrations: Iterable[Integration]
|
2021-03-17 17:34:19 +00:00
|
|
|
) -> list[JSON_TYPE]:
|
2020-10-21 20:24:50 +00:00
|
|
|
"""Load service files for multiple intergrations."""
|
|
|
|
return [_load_services_file(hass, integration) for integration in integrations]
|
|
|
|
|
|
|
|
|
2018-01-07 22:54:16 +00:00
|
|
|
@bind_hass
|
2020-04-17 18:33:58 +00:00
|
|
|
async def async_get_all_descriptions(
|
|
|
|
hass: HomeAssistantType,
|
2021-03-17 17:34:19 +00:00
|
|
|
) -> dict[str, dict[str, Any]]:
|
2018-01-07 22:54:16 +00:00
|
|
|
"""Return descriptions (i.e. user documentation) for all service calls."""
|
2019-04-12 17:09:17 +00:00
|
|
|
descriptions_cache = hass.data.setdefault(SERVICE_DESCRIPTION_CACHE, {})
|
2019-07-31 19:25:30 +00:00
|
|
|
format_cache_key = "{}.{}".format
|
2018-01-07 22:54:16 +00:00
|
|
|
services = hass.services.async_services()
|
|
|
|
|
2019-04-12 17:09:17 +00:00
|
|
|
# See if there are new services not seen before.
|
|
|
|
# Any service that we saw before already has an entry in description_cache.
|
2018-01-19 05:59:03 +00:00
|
|
|
missing = set()
|
2018-01-07 22:54:16 +00:00
|
|
|
for domain in services:
|
|
|
|
for service in services[domain]:
|
2019-04-12 17:09:17 +00:00
|
|
|
if format_cache_key(domain, service) not in descriptions_cache:
|
|
|
|
missing.add(domain)
|
2018-01-19 05:59:03 +00:00
|
|
|
break
|
2018-01-07 22:54:16 +00:00
|
|
|
|
2019-04-12 17:09:17 +00:00
|
|
|
# Files we loaded for missing descriptions
|
|
|
|
loaded = {}
|
|
|
|
|
2018-01-19 05:59:03 +00:00
|
|
|
if missing:
|
2020-11-11 07:34:54 +00:00
|
|
|
integrations = await gather_with_concurrency(
|
|
|
|
MAX_LOAD_CONCURRENTLY,
|
|
|
|
*(async_get_integration(hass, domain) for domain in missing),
|
2020-10-21 20:24:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
contents = await hass.async_add_executor_job(
|
|
|
|
_load_services_files, hass, integrations
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-12 17:09:17 +00:00
|
|
|
|
|
|
|
for domain, content in zip(missing, contents):
|
|
|
|
loaded[domain] = content
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
# Build response
|
2021-03-17 17:34:19 +00:00
|
|
|
descriptions: dict[str, dict[str, Any]] = {}
|
2018-01-07 22:54:16 +00:00
|
|
|
for domain in services:
|
|
|
|
descriptions[domain] = {}
|
|
|
|
|
|
|
|
for service in services[domain]:
|
|
|
|
cache_key = format_cache_key(domain, service)
|
2019-04-12 17:09:17 +00:00
|
|
|
description = descriptions_cache.get(cache_key)
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
# Cache missing descriptions
|
|
|
|
if description is None:
|
2019-04-12 17:09:17 +00:00
|
|
|
domain_yaml = loaded[domain]
|
2020-10-21 20:24:50 +00:00
|
|
|
yaml_description = domain_yaml.get(service, {}) # type: ignore
|
2019-04-12 17:09:17 +00:00
|
|
|
|
2019-04-18 05:27:11 +00:00
|
|
|
# Don't warn for missing services, because it triggers false
|
|
|
|
# positives for things like scripts, that register as a service
|
2018-01-07 22:54:16 +00:00
|
|
|
|
2021-02-16 08:35:27 +00:00
|
|
|
description = {
|
2021-02-22 15:26:46 +00:00
|
|
|
"name": yaml_description.get("name", ""),
|
2019-07-31 19:25:30 +00:00
|
|
|
"description": yaml_description.get("description", ""),
|
|
|
|
"fields": yaml_description.get("fields", {}),
|
2018-01-07 22:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 08:35:27 +00:00
|
|
|
if "target" in yaml_description:
|
|
|
|
description["target"] = yaml_description["target"]
|
|
|
|
|
|
|
|
descriptions_cache[cache_key] = description
|
|
|
|
|
2018-01-07 22:54:16 +00:00
|
|
|
descriptions[domain][service] = description
|
|
|
|
|
|
|
|
return descriptions
|
2018-08-16 07:50:11 +00:00
|
|
|
|
|
|
|
|
2019-08-21 21:08:46 +00:00
|
|
|
@ha.callback
|
|
|
|
@bind_hass
|
2020-04-17 18:33:58 +00:00
|
|
|
def async_set_service_schema(
|
2021-03-17 17:34:19 +00:00
|
|
|
hass: HomeAssistantType, domain: str, service: str, schema: dict[str, Any]
|
2020-04-17 18:33:58 +00:00
|
|
|
) -> None:
|
2019-08-21 21:08:46 +00:00
|
|
|
"""Register a description for a service."""
|
|
|
|
hass.data.setdefault(SERVICE_DESCRIPTION_CACHE, {})
|
|
|
|
|
|
|
|
description = {
|
2021-02-22 15:26:46 +00:00
|
|
|
"name": schema.get("name", ""),
|
|
|
|
"description": schema.get("description", ""),
|
|
|
|
"fields": schema.get("fields", {}),
|
2019-08-21 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:29:57 +00:00
|
|
|
if "target" in schema:
|
|
|
|
description["target"] = schema["target"]
|
|
|
|
|
2020-01-03 13:47:06 +00:00
|
|
|
hass.data[SERVICE_DESCRIPTION_CACHE][f"{domain}.{service}"] = description
|
2019-08-21 21:08:46 +00:00
|
|
|
|
|
|
|
|
2018-08-16 07:50:11 +00:00
|
|
|
@bind_hass
|
2020-09-20 09:03:58 +00:00
|
|
|
async def entity_service_call(
|
|
|
|
hass: HomeAssistantType,
|
2021-03-18 21:58:19 +00:00
|
|
|
platforms: Iterable[EntityPlatform],
|
2021-03-17 17:34:19 +00:00
|
|
|
func: str | Callable[..., Any],
|
2020-09-20 09:03:58 +00:00
|
|
|
call: ha.ServiceCall,
|
2021-03-17 17:34:19 +00:00
|
|
|
required_features: Iterable[int] | None = None,
|
2020-09-20 09:03:58 +00:00
|
|
|
) -> None:
|
2018-08-16 07:50:11 +00:00
|
|
|
"""Handle an entity service call.
|
|
|
|
|
|
|
|
Calls all platforms simultaneously.
|
|
|
|
"""
|
2018-11-21 11:26:08 +00:00
|
|
|
if call.context.user_id:
|
|
|
|
user = await hass.auth.async_get_user(call.context.user_id)
|
|
|
|
if user is None:
|
|
|
|
raise UnknownUser(context=call.context)
|
2021-03-17 17:34:19 +00:00
|
|
|
entity_perms: None | (
|
2020-09-20 09:03:58 +00:00
|
|
|
Callable[[str, str], bool]
|
2021-03-17 17:34:19 +00:00
|
|
|
) = user.permissions.check_entity
|
2018-11-21 11:26:08 +00:00
|
|
|
else:
|
2018-11-25 17:04:48 +00:00
|
|
|
entity_perms = None
|
2018-11-21 11:26:08 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
target_all_entities = call.data.get(ATTR_ENTITY_ID) == ENTITY_MATCH_ALL
|
2018-11-21 11:26:08 +00:00
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
if target_all_entities:
|
2021-03-17 17:34:19 +00:00
|
|
|
referenced: SelectedEntities | None = None
|
|
|
|
all_referenced: set[str] | None = None
|
2020-12-01 07:01:27 +00:00
|
|
|
else:
|
2018-11-21 11:26:08 +00:00
|
|
|
# A set of entities we're trying to target.
|
2020-12-01 07:01:27 +00:00
|
|
|
referenced = await async_extract_referenced_entity_ids(hass, call, True)
|
|
|
|
all_referenced = referenced.referenced | referenced.indirectly_referenced
|
2018-08-16 07:50:11 +00:00
|
|
|
|
2018-11-21 11:26:08 +00:00
|
|
|
# If the service function is a string, we'll pass it the service call data
|
2018-08-16 07:50:11 +00:00
|
|
|
if isinstance(func, str):
|
2021-03-17 17:34:19 +00:00
|
|
|
data: dict | ha.ServiceCall = {
|
2020-02-02 23:36:39 +00:00
|
|
|
key: val
|
|
|
|
for key, val in call.data.items()
|
|
|
|
if key not in cv.ENTITY_SERVICE_FIELDS
|
|
|
|
}
|
2018-11-21 11:26:08 +00:00
|
|
|
# If the service function is not a string, we pass the service call
|
2018-08-16 07:50:11 +00:00
|
|
|
else:
|
|
|
|
data = call
|
|
|
|
|
2018-11-21 11:26:08 +00:00
|
|
|
# Check the permissions
|
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
# A list with entities to call the service on.
|
2021-03-18 21:58:19 +00:00
|
|
|
entity_candidates: list[Entity] = []
|
2018-11-21 11:26:08 +00:00
|
|
|
|
2018-11-25 17:04:48 +00:00
|
|
|
if entity_perms is None:
|
2018-11-21 11:26:08 +00:00
|
|
|
for platform in platforms:
|
|
|
|
if target_all_entities:
|
2020-02-04 23:30:15 +00:00
|
|
|
entity_candidates.extend(platform.entities.values())
|
2018-11-21 11:26:08 +00:00
|
|
|
else:
|
2020-12-01 07:01:27 +00:00
|
|
|
assert all_referenced is not None
|
2020-02-04 23:30:15 +00:00
|
|
|
entity_candidates.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
entity
|
|
|
|
for entity in platform.entities.values()
|
2020-12-01 07:01:27 +00:00
|
|
|
if entity.entity_id in all_referenced
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2018-11-21 11:26:08 +00:00
|
|
|
|
|
|
|
elif target_all_entities:
|
|
|
|
# If we target all entities, we will select all entities the user
|
|
|
|
# is allowed to control.
|
|
|
|
for platform in platforms:
|
2020-02-04 23:30:15 +00:00
|
|
|
entity_candidates.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
entity
|
|
|
|
for entity in platform.entities.values()
|
|
|
|
if entity_perms(entity.entity_id, POLICY_CONTROL)
|
|
|
|
]
|
|
|
|
)
|
2018-11-21 11:26:08 +00:00
|
|
|
|
|
|
|
else:
|
2020-12-01 07:01:27 +00:00
|
|
|
assert all_referenced is not None
|
|
|
|
|
2018-11-21 11:26:08 +00:00
|
|
|
for platform in platforms:
|
|
|
|
platform_entities = []
|
|
|
|
for entity in platform.entities.values():
|
2020-02-02 23:36:39 +00:00
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
if entity.entity_id not in all_referenced:
|
2018-11-21 11:26:08 +00:00
|
|
|
continue
|
|
|
|
|
2018-11-25 17:04:48 +00:00
|
|
|
if not entity_perms(entity.entity_id, POLICY_CONTROL):
|
2018-11-21 11:26:08 +00:00
|
|
|
raise Unauthorized(
|
|
|
|
context=call.context,
|
|
|
|
entity_id=entity.entity_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
permission=POLICY_CONTROL,
|
2018-11-21 11:26:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
platform_entities.append(entity)
|
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
entity_candidates.extend(platform_entities)
|
2018-11-21 11:26:08 +00:00
|
|
|
|
2020-02-04 22:42:07 +00:00
|
|
|
if not target_all_entities:
|
2020-12-01 07:01:27 +00:00
|
|
|
assert referenced is not None
|
|
|
|
|
|
|
|
# Only report on explicit referenced entities
|
|
|
|
missing = set(referenced.referenced)
|
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
for entity in entity_candidates:
|
2020-12-01 07:01:27 +00:00
|
|
|
missing.discard(entity.entity_id)
|
2020-02-04 22:42:07 +00:00
|
|
|
|
2020-12-01 07:01:27 +00:00
|
|
|
referenced.log_missing(missing)
|
2020-02-04 22:42:07 +00:00
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
entities = []
|
2018-08-16 07:50:11 +00:00
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
for entity in entity_candidates:
|
2018-08-16 07:50:11 +00:00
|
|
|
if not entity.available:
|
|
|
|
continue
|
|
|
|
|
2019-04-10 16:44:58 +00:00
|
|
|
# Skip entities that don't have the required feature.
|
2020-09-20 09:03:58 +00:00
|
|
|
if required_features is not None and (
|
|
|
|
entity.supported_features is None
|
|
|
|
or not any(
|
|
|
|
entity.supported_features & feature_set == feature_set
|
|
|
|
for feature_set in required_features
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-04-10 16:44:58 +00:00
|
|
|
continue
|
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
entities.append(entity)
|
2018-08-20 15:39:53 +00:00
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
if not entities:
|
|
|
|
return
|
2020-01-31 08:32:43 +00:00
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
done, pending = await asyncio.wait(
|
|
|
|
[
|
2021-02-10 13:16:58 +00:00
|
|
|
asyncio.create_task(
|
|
|
|
entity.async_request_call(
|
|
|
|
_handle_entity_call(hass, entity, func, data, call.context)
|
|
|
|
)
|
2020-01-30 00:27:25 +00:00
|
|
|
)
|
2020-02-04 23:30:15 +00:00
|
|
|
for entity in entities
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert not pending
|
|
|
|
for future in done:
|
|
|
|
future.result() # pop exception if have
|
|
|
|
|
|
|
|
tasks = []
|
|
|
|
|
|
|
|
for entity in entities:
|
|
|
|
if not entity.should_poll:
|
|
|
|
continue
|
2018-08-16 07:50:11 +00:00
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
# Context expires if the turn on commands took a long time.
|
|
|
|
# Set context again so it's there when we update
|
|
|
|
entity.async_set_context(call.context)
|
2021-02-20 18:57:46 +00:00
|
|
|
tasks.append(asyncio.create_task(entity.async_update_ha_state(True)))
|
2018-08-16 07:50:11 +00:00
|
|
|
|
|
|
|
if tasks:
|
2019-03-02 07:09:31 +00:00
|
|
|
done, pending = await asyncio.wait(tasks)
|
|
|
|
assert not pending
|
|
|
|
for future in done:
|
|
|
|
future.result() # pop exception if have
|
2019-03-13 05:09:50 +00:00
|
|
|
|
|
|
|
|
2020-09-20 09:03:58 +00:00
|
|
|
async def _handle_entity_call(
|
|
|
|
hass: HomeAssistantType,
|
2021-02-12 09:58:20 +00:00
|
|
|
entity: Entity,
|
2021-03-17 17:34:19 +00:00
|
|
|
func: str | Callable[..., Any],
|
|
|
|
data: dict | ha.ServiceCall,
|
2020-09-20 09:03:58 +00:00
|
|
|
context: ha.Context,
|
|
|
|
) -> None:
|
2020-02-04 23:30:15 +00:00
|
|
|
"""Handle calling service method."""
|
|
|
|
entity.async_set_context(context)
|
|
|
|
|
|
|
|
if isinstance(func, str):
|
2020-11-26 19:20:10 +00:00
|
|
|
result = hass.async_run_job(partial(getattr(entity, func), **data)) # type: ignore
|
2020-02-04 23:30:15 +00:00
|
|
|
else:
|
2020-11-26 19:20:10 +00:00
|
|
|
result = hass.async_run_job(func, entity, data)
|
2020-02-04 23:30:15 +00:00
|
|
|
|
2020-11-27 15:12:39 +00:00
|
|
|
# Guard because callback functions do not return a task when passed to async_run_job.
|
2020-02-04 23:30:15 +00:00
|
|
|
if result is not None:
|
|
|
|
await result
|
|
|
|
|
|
|
|
if asyncio.iscoroutine(result):
|
|
|
|
_LOGGER.error(
|
2020-07-05 21:04:19 +00:00
|
|
|
"Service %s for %s incorrectly returns a coroutine object. Await result instead in service handler. Report bug to integration author",
|
2020-02-04 23:30:15 +00:00
|
|
|
func,
|
|
|
|
entity.entity_id,
|
|
|
|
)
|
2020-09-20 09:03:58 +00:00
|
|
|
await result # type: ignore
|
2020-02-04 23:30:15 +00:00
|
|
|
|
|
|
|
|
2019-03-13 05:09:50 +00:00
|
|
|
@bind_hass
|
|
|
|
@ha.callback
|
2019-04-02 16:34:11 +00:00
|
|
|
def async_register_admin_service(
|
2020-04-17 18:33:58 +00:00
|
|
|
hass: HomeAssistantType,
|
2019-07-31 19:25:30 +00:00
|
|
|
domain: str,
|
|
|
|
service: str,
|
2021-03-17 17:34:19 +00:00
|
|
|
service_func: Callable[[ha.ServiceCall], Awaitable | None],
|
2019-07-31 19:25:30 +00:00
|
|
|
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA),
|
|
|
|
) -> None:
|
2019-03-13 05:09:50 +00:00
|
|
|
"""Register a service that requires admin access."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-13 05:09:50 +00:00
|
|
|
@wraps(service_func)
|
2020-06-06 18:34:56 +00:00
|
|
|
async def admin_handler(call: ha.ServiceCall) -> None:
|
2019-03-13 05:09:50 +00:00
|
|
|
if call.context.user_id:
|
|
|
|
user = await hass.auth.async_get_user(call.context.user_id)
|
|
|
|
if user is None:
|
|
|
|
raise UnknownUser(context=call.context)
|
|
|
|
if not user.is_admin:
|
|
|
|
raise Unauthorized(context=call.context)
|
|
|
|
|
2020-11-27 15:12:39 +00:00
|
|
|
result = hass.async_run_job(service_func, call)
|
2020-02-10 03:47:16 +00:00
|
|
|
if result is not None:
|
|
|
|
await result
|
2019-03-13 05:09:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register(domain, service, admin_handler, schema)
|
2019-04-13 19:54:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bind_hass
|
|
|
|
@ha.callback
|
2021-02-08 10:59:46 +00:00
|
|
|
def verify_domain_control(
|
|
|
|
hass: HomeAssistantType, domain: str
|
|
|
|
) -> Callable[[Callable[[ha.ServiceCall], Any]], Callable[[ha.ServiceCall], Any]]:
|
2019-04-13 19:54:29 +00:00
|
|
|
"""Ensure permission to access any entity under domain in service call."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-02-08 10:59:46 +00:00
|
|
|
def decorator(
|
|
|
|
service_handler: Callable[[ha.ServiceCall], Any]
|
|
|
|
) -> Callable[[ha.ServiceCall], Any]:
|
2019-04-13 19:54:29 +00:00
|
|
|
"""Decorate."""
|
|
|
|
if not asyncio.iscoroutinefunction(service_handler):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise HomeAssistantError("Can only decorate async functions.")
|
2019-04-13 19:54:29 +00:00
|
|
|
|
2020-09-20 09:03:58 +00:00
|
|
|
async def check_permissions(call: ha.ServiceCall) -> Any:
|
2019-04-13 19:54:29 +00:00
|
|
|
"""Check user permission and raise before call if unauthorized."""
|
|
|
|
if not call.context.user_id:
|
|
|
|
return await service_handler(call)
|
|
|
|
|
|
|
|
user = await hass.auth.async_get_user(call.context.user_id)
|
2020-02-04 23:30:15 +00:00
|
|
|
|
2019-04-13 19:54:29 +00:00
|
|
|
if user is None:
|
|
|
|
raise UnknownUser(
|
|
|
|
context=call.context,
|
|
|
|
permission=POLICY_CONTROL,
|
2019-07-31 19:25:30 +00:00
|
|
|
user_id=call.context.user_id,
|
|
|
|
)
|
2019-04-13 19:54:29 +00:00
|
|
|
|
|
|
|
reg = await hass.helpers.entity_registry.async_get_registry()
|
2020-02-04 23:30:15 +00:00
|
|
|
|
2020-04-28 21:31:25 +00:00
|
|
|
authorized = False
|
|
|
|
|
2020-02-04 23:30:15 +00:00
|
|
|
for entity in reg.entities.values():
|
|
|
|
if entity.platform != domain:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if user.permissions.check_entity(entity.entity_id, POLICY_CONTROL):
|
2020-04-28 21:31:25 +00:00
|
|
|
authorized = True
|
|
|
|
break
|
2019-04-13 19:54:29 +00:00
|
|
|
|
2020-04-28 21:31:25 +00:00
|
|
|
if not authorized:
|
|
|
|
raise Unauthorized(
|
|
|
|
context=call.context,
|
|
|
|
permission=POLICY_CONTROL,
|
|
|
|
user_id=call.context.user_id,
|
|
|
|
perm_category=CAT_ENTITIES,
|
|
|
|
)
|
|
|
|
|
|
|
|
return await service_handler(call)
|
2019-04-13 19:54:29 +00:00
|
|
|
|
|
|
|
return check_permissions
|
|
|
|
|
|
|
|
return decorator
|