2013-12-11 08:07:30 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
This package contains components that can be plugged into Home Assistant.
|
2014-01-05 02:24:30 +00:00
|
|
|
|
|
|
|
Component design guidelines:
|
|
|
|
|
|
|
|
Each component defines a constant DOMAIN that is equal to its filename.
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
Each component that tracks states should create state entity names in the
|
2014-01-05 02:24:30 +00:00
|
|
|
format "<DOMAIN>.<OBJECT_ID>".
|
|
|
|
|
|
|
|
Each component should publish services only under its own domain.
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
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
|
2014-01-24 07:26:00 +00:00
|
|
|
import homeassistant.util as util
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.helpers 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 (
|
|
|
|
ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
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
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def is_on(hass, entity_id=None):
|
2014-01-24 07:26:00 +00:00
|
|
|
""" Loads 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:
|
|
|
|
domain = util.split_entity_id(entity_id)[0]
|
|
|
|
|
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):
|
2014-01-24 07:26:00 +00:00
|
|
|
""" Turns 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):
|
2014-01-24 07:26:00 +00:00
|
|
|
""" Turns 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
|
|
|
|
|
|
|
|
2014-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
2014-04-13 19:59:45 +00:00
|
|
|
""" Setup general services related to homeassistant. """
|
2014-03-25 03:34:35 +00:00
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
def handle_turn_service(service):
|
|
|
|
""" Method to 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),
|
|
|
|
lambda item: util.split_entity_id(item)[0])
|
|
|
|
|
|
|
|
for domain, ent_ids in by_domain:
|
|
|
|
# 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)
|
|
|
|
|
2014-12-14 08:35:16 +00:00
|
|
|
hass.services.call(domain, service.service, data, True)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
hass.services.register(ha.DOMAIN, SERVICE_TURN_OFF, handle_turn_service)
|
|
|
|
hass.services.register(ha.DOMAIN, SERVICE_TURN_ON, handle_turn_service)
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
return True
|