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
|
|
|
|
|
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
2016-03-10 20:36:05 +00:00
|
|
|
from homeassistant.helpers import template
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant.loader import get_component
|
2016-01-09 00:58:44 +00:00
|
|
|
|
2016-01-25 03:46:30 +00:00
|
|
|
HASS = None
|
|
|
|
|
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-01-09 00:58:44 +00:00
|
|
|
def call_from_config(hass, config, blocking=False):
|
|
|
|
"""Call a service based on a config hash."""
|
2016-03-10 20:36:05 +00:00
|
|
|
validation_error = validate_service_call(config)
|
|
|
|
if validation_error:
|
|
|
|
_LOGGER.error(validation_error)
|
2016-01-09 00:58:44 +00:00
|
|
|
return
|
|
|
|
|
2016-03-10 20:36:05 +00:00
|
|
|
domain_service = (
|
|
|
|
config[CONF_SERVICE]
|
|
|
|
if CONF_SERVICE in config
|
|
|
|
else template.render(hass, config[CONF_SERVICE_TEMPLATE]))
|
|
|
|
|
2016-01-10 00:01:27 +00:00
|
|
|
try:
|
2016-03-10 20:36:05 +00:00
|
|
|
domain, service_name = domain_service.split('.', 1)
|
2016-01-10 00:01:27 +00:00
|
|
|
except ValueError:
|
2016-03-10 20:36:05 +00:00
|
|
|
_LOGGER.error('Invalid service specified: %s', domain_service)
|
2016-01-10 00:01:27 +00:00
|
|
|
return
|
|
|
|
|
2016-01-09 00:58:44 +00:00
|
|
|
service_data = config.get(CONF_SERVICE_DATA)
|
|
|
|
|
|
|
|
if service_data is None:
|
|
|
|
service_data = {}
|
|
|
|
elif isinstance(service_data, dict):
|
|
|
|
service_data = dict(service_data)
|
|
|
|
else:
|
|
|
|
_LOGGER.error("%s should be a dictionary", CONF_SERVICE_DATA)
|
|
|
|
service_data = {}
|
|
|
|
|
2016-03-10 20:36:05 +00:00
|
|
|
service_data_template = config.get(CONF_SERVICE_DATA_TEMPLATE)
|
|
|
|
if service_data_template and isinstance(service_data_template, dict):
|
|
|
|
for key, value in service_data_template.items():
|
|
|
|
service_data[key] = template.render(hass, value)
|
|
|
|
elif service_data_template:
|
|
|
|
_LOGGER.error("%s should be a dictionary", CONF_SERVICE_DATA)
|
|
|
|
|
2016-01-09 00:58:44 +00:00
|
|
|
entity_id = config.get(CONF_SERVICE_ENTITY_ID)
|
|
|
|
if isinstance(entity_id, str):
|
2016-01-09 23:51:51 +00:00
|
|
|
service_data[ATTR_ENTITY_ID] = [ent.strip() for ent in
|
|
|
|
entity_id.split(",")]
|
2016-01-09 00:58:44 +00:00
|
|
|
elif entity_id is not None:
|
|
|
|
service_data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
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)]
|
2016-03-10 20:36:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_service_call(config):
|
|
|
|
"""Validate service call configuration.
|
|
|
|
|
|
|
|
Helper method to validate that a configuration is a valid service call.
|
|
|
|
Returns None if validation succeeds, else an error description
|
|
|
|
"""
|
|
|
|
if not isinstance(config, dict):
|
|
|
|
return 'Invalid configuration {}'.format(config)
|
|
|
|
if CONF_SERVICE not in config and CONF_SERVICE_TEMPLATE not in config:
|
|
|
|
return 'Missing key {} or {}: {}'.format(
|
|
|
|
CONF_SERVICE,
|
|
|
|
CONF_SERVICE_TEMPLATE,
|
|
|
|
config)
|
|
|
|
return None
|