2016-01-08 16:58:44 -08:00
|
|
|
"""Service calling related helpers."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
2016-01-23 22:49:49 -08:00
|
|
|
from homeassistant.helpers.entity import split_entity_id
|
2016-01-08 16:58:44 -08:00
|
|
|
|
|
|
|
CONF_SERVICE = 'service'
|
|
|
|
CONF_SERVICE_ENTITY_ID = 'entity_id'
|
|
|
|
CONF_SERVICE_DATA = 'data'
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def call_from_config(hass, config, blocking=False):
|
|
|
|
"""Call a service based on a config hash."""
|
2016-01-09 16:01:27 -08:00
|
|
|
if not isinstance(config, dict) or CONF_SERVICE not in config:
|
2016-01-08 16:58:44 -08:00
|
|
|
_LOGGER.error('Missing key %s: %s', CONF_SERVICE, config)
|
|
|
|
return
|
|
|
|
|
2016-01-09 16:01:27 -08:00
|
|
|
try:
|
|
|
|
domain, service = split_entity_id(config[CONF_SERVICE])
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.error('Invalid service specified: %s', config[CONF_SERVICE])
|
|
|
|
return
|
|
|
|
|
2016-01-08 16:58:44 -08: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 = {}
|
|
|
|
|
|
|
|
entity_id = config.get(CONF_SERVICE_ENTITY_ID)
|
|
|
|
if isinstance(entity_id, str):
|
2016-01-09 15:51:51 -08:00
|
|
|
service_data[ATTR_ENTITY_ID] = [ent.strip() for ent in
|
|
|
|
entity_id.split(",")]
|
2016-01-08 16:58:44 -08:00
|
|
|
elif entity_id is not None:
|
|
|
|
service_data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(domain, service, service_data, blocking)
|