2016-01-09 00:58:44 +00:00
|
|
|
"""Service calling related helpers."""
|
2016-10-01 05:34:45 +00:00
|
|
|
import asyncio
|
2016-01-09 00:58:44 +00:00
|
|
|
import logging
|
2016-08-07 23:26:35 +00:00
|
|
|
# pylint: disable=unused-import
|
|
|
|
from typing import Optional # NOQA
|
|
|
|
|
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
|
2016-08-09 03:42:25 +00:00
|
|
|
from homeassistant.core import HomeAssistant # NOQA
|
2016-04-21 19:22:19 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant.loader import get_component
|
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__)
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_call_from_config(hass, config, blocking=False, variables=None,
|
|
|
|
validate_config=True):
|
|
|
|
"""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:
|
2016-10-01 06:26:15 +00:00
|
|
|
def _data_template_creator(value):
|
|
|
|
"""Recursive template creator helper function."""
|
|
|
|
if isinstance(value, list):
|
|
|
|
return [_data_template_creator(item) for item in value]
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
return {key: _data_template_creator(item)
|
|
|
|
for key, item in value.items()}
|
|
|
|
value.hass = hass
|
2016-10-01 05:34:45 +00:00
|
|
|
return value.async_render(variables)
|
2016-10-01 06:26:15 +00:00
|
|
|
service_data.update(_data_template_creator(
|
|
|
|
config[CONF_SERVICE_DATA_TEMPLATE]))
|
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
|
|
|
|
2016-10-01 05:34:45 +00:00
|
|
|
yield from hass.services.async_call(
|
|
|
|
domain, service_name, service_data, blocking)
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
|
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
|