2013-12-11 08:07:30 +00:00
|
|
|
"""
|
|
|
|
This package contains components that can be plugged into Home Assistant.
|
2014-01-05 02:24:30 +00:00
|
|
|
|
|
|
|
Component design guidelines:
|
2016-03-08 16:55:57 +00:00
|
|
|
- Each component defines a constant DOMAIN that is equal to its filename.
|
|
|
|
- Each component that tracks states should create state entity names in the
|
|
|
|
format "<DOMAIN>.<OBJECT_ID>".
|
|
|
|
- Each component should publish services only under its own domain.
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2016-10-27 07:16:23 +00:00
|
|
|
import asyncio
|
2014-04-13 19:59:45 +00:00
|
|
|
import itertools as it
|
2014-08-13 12:28:45 +00:00
|
|
|
import logging
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2017-02-08 17:17:52 +00:00
|
|
|
import homeassistant.config as conf_util
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant.helpers.service import extract_entity_ids
|
2014-11-05 07:34:19 +00:00
|
|
|
from homeassistant.loader import get_component
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2017-02-08 17:17:52 +00:00
|
|
|
ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
|
|
|
SERVICE_HOMEASSISTANT_STOP, SERVICE_HOMEASSISTANT_RESTART,
|
|
|
|
RESTART_EXIT_CODE)
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2014-11-08 21:57:08 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2014-08-13 12:28:45 +00:00
|
|
|
|
2016-06-22 16:13:18 +00:00
|
|
|
SERVICE_RELOAD_CORE_CONFIG = 'reload_core_config'
|
2017-02-08 17:17:52 +00:00
|
|
|
SERVICE_CHECK_CONFIG = 'check_config'
|
2016-06-22 16:13:18 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def is_on(hass, entity_id=None):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Load up the module to call the is_on method.
|
|
|
|
|
|
|
|
If there is no entity id given we will check all.
|
|
|
|
"""
|
2014-04-13 19:59:45 +00:00
|
|
|
if entity_id:
|
2014-11-05 07:34:19 +00:00
|
|
|
group = get_component('group')
|
2014-04-13 19:59:45 +00:00
|
|
|
|
2014-11-23 08:31:09 +00:00
|
|
|
entity_ids = group.expand_entity_ids(hass, [entity_id])
|
2014-04-13 19:59:45 +00:00
|
|
|
else:
|
2014-11-29 07:19:59 +00:00
|
|
|
entity_ids = hass.states.entity_ids()
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
for entity_id in entity_ids:
|
2016-08-09 03:21:40 +00:00
|
|
|
domain = ha.split_entity_id(entity_id)[0]
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2014-11-05 07:34:19 +00:00
|
|
|
module = get_component(domain)
|
2014-01-24 07:53:18 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
try:
|
2014-04-24 07:40:45 +00:00
|
|
|
if module.is_on(hass, entity_id):
|
2014-01-24 07:26:00 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
except AttributeError:
|
2014-01-24 07:53:18 +00:00
|
|
|
# module is None or method is_on does not exist
|
2014-11-08 21:57:08 +00:00
|
|
|
_LOGGER.exception("Failed to call %s.is_on for %s",
|
|
|
|
module, entity_id)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-11-23 08:31:09 +00:00
|
|
|
def turn_on(hass, entity_id=None, **service_data):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Turn specified entity on if possible."""
|
2014-11-23 08:31:09 +00:00
|
|
|
if entity_id is not None:
|
|
|
|
service_data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
2014-12-01 02:42:52 +00:00
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_TURN_ON, service_data)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
|
2014-11-23 08:31:09 +00:00
|
|
|
def turn_off(hass, entity_id=None, **service_data):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Turn specified entity off."""
|
2014-11-23 08:31:09 +00:00
|
|
|
if entity_id is not None:
|
|
|
|
service_data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
2014-12-01 02:42:52 +00:00
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_TURN_OFF, service_data)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
|
2016-01-16 15:45:05 +00:00
|
|
|
def toggle(hass, entity_id=None, **service_data):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Toggle specified entity."""
|
2016-01-16 15:45:05 +00:00
|
|
|
if entity_id is not None:
|
|
|
|
service_data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_TOGGLE, service_data)
|
|
|
|
|
|
|
|
|
2017-02-08 17:17:52 +00:00
|
|
|
def stop(hass):
|
|
|
|
"""Stop Home Assistant."""
|
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP)
|
|
|
|
|
|
|
|
|
|
|
|
def restart(hass):
|
|
|
|
"""Stop Home Assistant."""
|
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART)
|
|
|
|
|
|
|
|
|
|
|
|
def check_config(hass):
|
|
|
|
"""Check the config files."""
|
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_CHECK_CONFIG)
|
|
|
|
|
|
|
|
|
2016-06-22 16:13:18 +00:00
|
|
|
def reload_core_config(hass):
|
|
|
|
"""Reload the core config."""
|
|
|
|
hass.services.call(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG)
|
|
|
|
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up general services related to Home Assistant."""
|
2016-10-27 07:16:23 +00:00
|
|
|
@asyncio.coroutine
|
2017-02-08 17:17:52 +00:00
|
|
|
def async_handle_turn_service(service):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle calls to homeassistant.turn_on/off."""
|
2014-04-24 07:40:45 +00:00
|
|
|
entity_ids = extract_entity_ids(hass, service)
|
2014-03-25 03:34:35 +00:00
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
# Generic turn on/off method requires entity id
|
|
|
|
if not entity_ids:
|
2014-11-08 21:57:08 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"homeassistant/%s cannot be called without entity_id",
|
|
|
|
service.service)
|
2014-04-13 19:59:45 +00:00
|
|
|
return
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
# Group entity_ids by domain. groupby requires sorted data.
|
|
|
|
by_domain = it.groupby(sorted(entity_ids),
|
2016-08-09 03:21:40 +00:00
|
|
|
lambda item: ha.split_entity_id(item)[0])
|
2014-04-13 19:59:45 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
tasks = []
|
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
for domain, ent_ids in by_domain:
|
2016-01-04 05:25:15 +00:00
|
|
|
# We want to block for all calls and only return when all calls
|
|
|
|
# have been processed. If a service does not exist it causes a 10
|
|
|
|
# second delay while we're blocking waiting for a response.
|
|
|
|
# But services can be registered on other HA instances that are
|
|
|
|
# listening to the bus too. So as a in between solution, we'll
|
|
|
|
# block only if the service is defined in the current HA instance.
|
|
|
|
blocking = hass.services.has_service(domain, service.service)
|
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
# Create a new dict for this call
|
|
|
|
data = dict(service.data)
|
|
|
|
|
|
|
|
# ent_ids is a generator, convert it to a list.
|
|
|
|
data[ATTR_ENTITY_ID] = list(ent_ids)
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
tasks.append(hass.services.async_call(
|
|
|
|
domain, service.service, data, blocking))
|
|
|
|
|
2016-11-06 17:26:40 +00:00
|
|
|
yield from asyncio.wait(tasks, loop=hass.loop)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
hass.services.async_register(
|
2017-02-08 17:17:52 +00:00
|
|
|
ha.DOMAIN, SERVICE_TURN_OFF, async_handle_turn_service)
|
2016-10-27 07:16:23 +00:00
|
|
|
hass.services.async_register(
|
2017-02-08 17:17:52 +00:00
|
|
|
ha.DOMAIN, SERVICE_TURN_ON, async_handle_turn_service)
|
2016-10-27 07:16:23 +00:00
|
|
|
hass.services.async_register(
|
2017-02-08 17:17:52 +00:00
|
|
|
ha.DOMAIN, SERVICE_TOGGLE, async_handle_turn_service)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
@asyncio.coroutine
|
2017-02-08 17:17:52 +00:00
|
|
|
def async_handle_core_service(call):
|
|
|
|
"""Service handler for handling core services."""
|
|
|
|
if call.service == SERVICE_HOMEASSISTANT_STOP:
|
|
|
|
hass.async_add_job(hass.async_stop())
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2017-02-12 19:31:46 +00:00
|
|
|
errors = yield from conf_util.async_check_ha_config_file(hass)
|
2017-02-08 17:17:52 +00:00
|
|
|
except HomeAssistantError:
|
|
|
|
return
|
|
|
|
|
2017-02-12 19:31:46 +00:00
|
|
|
if errors:
|
|
|
|
notif = get_component('persistent_notification')
|
|
|
|
_LOGGER.error(errors)
|
|
|
|
notif.async_create(
|
|
|
|
hass, "Config error. See dev-info panel for details.",
|
|
|
|
"Config validating", "{0}.check_config".format(ha.DOMAIN))
|
|
|
|
return
|
|
|
|
|
2017-02-08 17:17:52 +00:00
|
|
|
if call.service == SERVICE_HOMEASSISTANT_RESTART:
|
|
|
|
hass.async_add_job(hass.async_stop(RESTART_EXIT_CODE))
|
2016-06-22 16:13:18 +00:00
|
|
|
|
2017-02-08 17:17:52 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service)
|
|
|
|
hass.services.async_register(
|
|
|
|
ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART, async_handle_core_service)
|
|
|
|
hass.services.async_register(
|
|
|
|
ha.DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_handle_reload_config(call):
|
|
|
|
"""Service handler for reloading core config."""
|
2016-06-22 16:13:18 +00:00
|
|
|
try:
|
2016-10-27 07:16:23 +00:00
|
|
|
conf = yield from conf_util.async_hass_config_yaml(hass)
|
2016-06-22 16:13:18 +00:00
|
|
|
except HomeAssistantError as err:
|
|
|
|
_LOGGER.error(err)
|
|
|
|
return
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
yield from conf_util.async_process_ha_core_config(
|
|
|
|
hass, conf.get(ha.DOMAIN) or {})
|
2016-06-22 16:13:18 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
hass.services.async_register(
|
2017-02-08 17:17:52 +00:00
|
|
|
ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config)
|
2016-06-22 16:13:18 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
return True
|