2016-01-09 00:58:44 +00:00
|
|
|
"""Service calling related helpers."""
|
2016-01-25 03:46:30 +00:00
|
|
|
import functools
|
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-01-09 00:58:44 +00:00
|
|
|
|
2016-08-09 03:42:25 +00:00
|
|
|
HASS = None # type: Optional[HomeAssistant]
|
2016-01-25 03:46:30 +00:00
|
|
|
|
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-01-25 03:46:30 +00:00
|
|
|
def service(domain, service_name):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Decorator factory to register a service."""
|
2016-01-25 03:46:30 +00:00
|
|
|
def register_service_decorator(action):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Decorator to register a service."""
|
2016-01-25 03:46:30 +00:00
|
|
|
HASS.services.register(domain, service_name,
|
2016-01-25 05:14:16 +00:00
|
|
|
functools.partial(action, HASS))
|
2016-01-25 03:46:30 +00:00
|
|
|
return action
|
|
|
|
|
|
|
|
return register_service_decorator
|
|
|
|
|
|
|
|
|
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-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
|
|
|
|
domain_service = config[CONF_SERVICE_TEMPLATE].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
|
|
|
|
return value.render(variables)
|
|
|
|
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-01-25 03:46:30 +00:00
|
|
|
hass.services.call(domain, service_name, service_data, blocking)
|
2016-01-24 06:57:14 +00:00
|
|
|
|
|
|
|
|
2016-01-25 04:00:43 +00:00
|
|
|
def extract_entity_ids(hass, service_call):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Helper method to extract a list of entity ids from a service call.
|
|
|
|
|
2016-01-24 06:57:14 +00:00
|
|
|
Will convert group entity ids to the entity ids it represents.
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
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)]
|