2016-01-09 00:58:44 +00:00
|
|
|
"""Service calling related helpers."""
|
|
|
|
import logging
|
2016-08-07 23:26:35 +00:00
|
|
|
# pylint: disable=unused-import
|
|
|
|
from typing import Optional # NOQA
|
2018-01-07 22:54:16 +00:00
|
|
|
from os import path
|
2016-08-07 23:26:35 +00:00
|
|
|
|
2016-04-21 19:22:19 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-01-09 00:58:44 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
2018-01-07 22:54:16 +00:00
|
|
|
import homeassistant.core as ha
|
2016-04-21 19:22:19 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2018-01-19 06:13:14 +00:00
|
|
|
from homeassistant.helpers import template
|
2017-10-08 15:17:54 +00:00
|
|
|
from homeassistant.loader import get_component, bind_hass
|
2018-01-07 22:54:16 +00:00
|
|
|
from homeassistant.util.yaml import load_yaml
|
2016-04-21 19:22:19 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-01 05:34:45 +00:00
|
|
|
from homeassistant.util.async import run_coroutine_threadsafe
|
2016-01-09 00:58:44 +00:00
|
|
|
|
|
|
|
CONF_SERVICE = 'service'
|
2016-03-10 20:36:05 +00:00
|
|
|
CONF_SERVICE_TEMPLATE = 'service_template'
|
2016-01-09 00:58:44 +00:00
|
|
|
CONF_SERVICE_ENTITY_ID = 'entity_id'
|
|
|
|
CONF_SERVICE_DATA = 'data'
|
2016-03-10 20:36:05 +00:00
|
|
|
CONF_SERVICE_DATA_TEMPLATE = 'data_template'
|
2016-01-09 00:58:44 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-01-07 22:54:16 +00:00
|
|
|
SERVICE_DESCRIPTION_CACHE = 'service_description_cache'
|
|
|
|
|
2016-01-09 00:58:44 +00:00
|
|
|
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2016-04-23 05:11:21 +00:00
|
|
|
def call_from_config(hass, config, blocking=False, variables=None,
|
|
|
|
validate_config=True):
|
2016-01-09 00:58:44 +00:00
|
|
|
"""Call a service based on a config hash."""
|
2016-10-01 05:34:45 +00:00
|
|
|
run_coroutine_threadsafe(
|
|
|
|
async_call_from_config(hass, config, blocking, variables,
|
|
|
|
validate_config), hass.loop).result()
|
|
|
|
|
|
|
|
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2018-02-25 11:38:46 +00:00
|
|
|
async def async_call_from_config(hass, config, blocking=False, variables=None,
|
|
|
|
validate_config=True):
|
2016-10-01 05:34:45 +00:00
|
|
|
"""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:
|
|
|
|
_LOGGER.error("Invalid config for calling service: %s", ex)
|
|
|
|
return
|
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:
|
2016-04-21 19:22:19 +00:00
|
|
|
try:
|
2016-09-28 04:29:55 +00:00
|
|
|
config[CONF_SERVICE_TEMPLATE].hass = hass
|
2016-10-01 05:34:45 +00:00
|
|
|
domain_service = config[CONF_SERVICE_TEMPLATE].async_render(
|
|
|
|
variables)
|
2016-04-21 19:22:19 +00:00
|
|
|
domain_service = cv.service(domain_service)
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error('Error rendering service name template: %s', ex)
|
|
|
|
return
|
|
|
|
except vol.Invalid as ex:
|
|
|
|
_LOGGER.error('Template rendered invalid service: %s',
|
|
|
|
domain_service)
|
|
|
|
return
|
|
|
|
|
|
|
|
domain, service_name = domain_service.split('.', 1)
|
|
|
|
service_data = dict(config.get(CONF_SERVICE_DATA, {}))
|
|
|
|
|
|
|
|
if CONF_SERVICE_DATA_TEMPLATE in config:
|
2018-01-19 06:13:14 +00:00
|
|
|
try:
|
|
|
|
template.attach(hass, config[CONF_SERVICE_DATA_TEMPLATE])
|
|
|
|
service_data.update(template.render_complex(
|
|
|
|
config[CONF_SERVICE_DATA_TEMPLATE], variables))
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error('Error rendering data template: %s', ex)
|
2018-02-05 08:19:56 +00:00
|
|
|
return
|
2016-04-21 19:22:19 +00:00
|
|
|
|
|
|
|
if CONF_SERVICE_ENTITY_ID in config:
|
|
|
|
service_data[ATTR_ENTITY_ID] = config[CONF_SERVICE_ENTITY_ID]
|
2016-01-09 00:58:44 +00:00
|
|
|
|
2018-02-25 11:38:46 +00:00
|
|
|
await hass.services.async_call(
|
2016-10-01 05:34:45 +00:00
|
|
|
domain, service_name, service_data, blocking)
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2016-10-29 23:54:26 +00:00
|
|
|
def extract_entity_ids(hass, service_call, expand_group=True):
|
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.
|
2016-10-16 16:35:46 +00:00
|
|
|
|
|
|
|
Async friendly.
|
2016-01-24 06:57:14 +00:00
|
|
|
"""
|
2016-01-25 04:00:43 +00:00
|
|
|
if not (service_call.data and ATTR_ENTITY_ID in service_call.data):
|
2016-01-24 06:57:14 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
group = get_component('group')
|
|
|
|
|
|
|
|
# Entity ID attr can be a list or a string
|
2016-01-25 04:00:43 +00:00
|
|
|
service_ent_id = service_call.data[ATTR_ENTITY_ID]
|
2016-01-24 06:57:14 +00:00
|
|
|
|
2016-10-29 23:54:26 +00:00
|
|
|
if expand_group:
|
2016-01-24 06:57:14 +00:00
|
|
|
|
2016-10-29 23:54:26 +00:00
|
|
|
if isinstance(service_ent_id, str):
|
|
|
|
return group.expand_entity_ids(hass, [service_ent_id])
|
|
|
|
|
|
|
|
return [ent_id for ent_id in
|
|
|
|
group.expand_entity_ids(hass, service_ent_id)]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if isinstance(service_ent_id, str):
|
|
|
|
return [service_ent_id]
|
|
|
|
|
|
|
|
return service_ent_id
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bind_hass
|
2018-02-25 11:38:46 +00:00
|
|
|
async def async_get_all_descriptions(hass):
|
2018-01-07 22:54:16 +00:00
|
|
|
"""Return descriptions (i.e. user documentation) for all service calls."""
|
|
|
|
if SERVICE_DESCRIPTION_CACHE not in hass.data:
|
|
|
|
hass.data[SERVICE_DESCRIPTION_CACHE] = {}
|
|
|
|
description_cache = hass.data[SERVICE_DESCRIPTION_CACHE]
|
|
|
|
|
|
|
|
format_cache_key = '{}.{}'.format
|
|
|
|
|
|
|
|
def domain_yaml_file(domain):
|
|
|
|
"""Return the services.yaml location for a domain."""
|
|
|
|
if domain == ha.DOMAIN:
|
|
|
|
import homeassistant.components as components
|
|
|
|
component_path = path.dirname(components.__file__)
|
|
|
|
else:
|
|
|
|
component_path = path.dirname(get_component(domain).__file__)
|
|
|
|
return path.join(component_path, 'services.yaml')
|
|
|
|
|
2018-01-19 05:59:03 +00:00
|
|
|
def load_services_files(yaml_files):
|
|
|
|
"""Load and parse services.yaml files."""
|
|
|
|
loaded = {}
|
|
|
|
for yaml_file in yaml_files:
|
|
|
|
try:
|
|
|
|
loaded[yaml_file] = load_yaml(yaml_file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
loaded[yaml_file] = {}
|
|
|
|
|
|
|
|
return loaded
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
services = hass.services.async_services()
|
|
|
|
|
|
|
|
# Load missing files
|
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]:
|
|
|
|
if format_cache_key(domain, service) not in description_cache:
|
2018-01-19 05:59:03 +00:00
|
|
|
missing.add(domain_yaml_file(domain))
|
|
|
|
break
|
2018-01-07 22:54:16 +00:00
|
|
|
|
2018-01-19 05:59:03 +00:00
|
|
|
if missing:
|
2018-02-25 11:38:46 +00:00
|
|
|
loaded = await hass.async_add_job(load_services_files, missing)
|
2018-01-07 22:54:16 +00:00
|
|
|
|
|
|
|
# Build response
|
|
|
|
catch_all_yaml_file = domain_yaml_file(ha.DOMAIN)
|
|
|
|
descriptions = {}
|
|
|
|
for domain in services:
|
|
|
|
descriptions[domain] = {}
|
|
|
|
yaml_file = domain_yaml_file(domain)
|
|
|
|
|
|
|
|
for service in services[domain]:
|
|
|
|
cache_key = format_cache_key(domain, service)
|
|
|
|
description = description_cache.get(cache_key)
|
|
|
|
|
|
|
|
# Cache missing descriptions
|
|
|
|
if description is None:
|
|
|
|
if yaml_file == catch_all_yaml_file:
|
2018-01-19 05:59:03 +00:00
|
|
|
yaml_services = loaded[yaml_file].get(domain, {})
|
2018-01-07 22:54:16 +00:00
|
|
|
else:
|
2018-01-19 05:59:03 +00:00
|
|
|
yaml_services = loaded[yaml_file]
|
2018-01-07 22:54:16 +00:00
|
|
|
yaml_description = yaml_services.get(service, {})
|
|
|
|
|
|
|
|
description = description_cache[cache_key] = {
|
|
|
|
'description': yaml_description.get('description', ''),
|
|
|
|
'fields': yaml_description.get('fields', {})
|
|
|
|
}
|
|
|
|
|
|
|
|
descriptions[domain][service] = description
|
|
|
|
|
|
|
|
return descriptions
|