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-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
import importlib
|
|
|
|
|
|
|
|
import homeassistant as ha
|
|
|
|
import homeassistant.util as util
|
|
|
|
|
|
|
|
ATTR_ENTITY_ID = 'entity_id'
|
|
|
|
|
|
|
|
STATE_ON = 'on'
|
|
|
|
STATE_OFF = 'off'
|
|
|
|
STATE_NOT_HOME = 'not_home'
|
|
|
|
STATE_HOME = 'home'
|
|
|
|
|
|
|
|
SERVICE_TURN_ON = 'turn_on'
|
|
|
|
SERVICE_TURN_OFF = 'turn_off'
|
|
|
|
|
2014-03-12 05:45:05 +00:00
|
|
|
SERVICE_VOLUME_UP = "volume_up"
|
|
|
|
SERVICE_VOLUME_DOWN = "volume_down"
|
|
|
|
SERVICE_VOLUME_MUTE = "volume_mute"
|
|
|
|
SERVICE_MEDIA_PLAY_PAUSE = "media_play_pause"
|
|
|
|
SERVICE_MEDIA_NEXT_TRACK = "media_next_track"
|
|
|
|
SERVICE_MEDIA_PREV_TRACK = "media_prev_track"
|
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
_LOADED_MOD = {}
|
|
|
|
|
|
|
|
|
|
|
|
def is_on(statemachine, entity_id=None):
|
|
|
|
""" Loads up the module to call the is_on method.
|
|
|
|
If there is no entity id given we will check all. """
|
|
|
|
entity_ids = [entity_id] if entity_id else statemachine.entity_ids
|
|
|
|
|
2014-01-24 07:53:18 +00:00
|
|
|
# Save reference locally because those lookups are way faster
|
|
|
|
modules = _LOADED_MOD
|
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
for entity_id in entity_ids:
|
|
|
|
domain = util.split_entity_id(entity_id)[0]
|
|
|
|
|
2014-01-24 07:53:18 +00:00
|
|
|
# See if we have the module locally cached, else import it
|
|
|
|
# Does this code look chaotic? Yes!
|
|
|
|
try:
|
|
|
|
module = modules[domain]
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
# If modules[domain] does not exist, import module
|
|
|
|
try:
|
|
|
|
module = modules[domain] = importlib.import_module(
|
|
|
|
'homeassistant.components.'+domain)
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
# If we got a bogus domain the input will fail
|
|
|
|
module = modules[domain] = None
|
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
try:
|
2014-01-24 07:53:18 +00:00
|
|
|
if module.is_on(statemachine, 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-01-24 07:26:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def turn_on(bus, entity_id=None):
|
|
|
|
""" Turns specified entity on if possible. """
|
|
|
|
# If there is no entity_id we do not know which domain to call.
|
|
|
|
if not entity_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
domain = util.split_entity_id(entity_id)[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
bus.call_service(domain, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id})
|
|
|
|
except ha.ServiceDoesNotExistError:
|
|
|
|
# turn_on service does not exist
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def turn_off(bus, entity_id=None):
|
|
|
|
""" Turns specified entity off. """
|
|
|
|
# If there is no entity_id we do not know which domain to call.
|
|
|
|
if not entity_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
domain = util.split_entity_id(entity_id)[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
bus.call_service(domain, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
|
|
|
|
except ha.ServiceDoesNotExistError:
|
|
|
|
# turn_off service does not exist
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bus):
|
|
|
|
""" Setup general services related to homeassistant. """
|
|
|
|
|
|
|
|
bus.register_service(ha.DOMAIN, SERVICE_TURN_OFF,
|
|
|
|
lambda service:
|
|
|
|
turn_off(bus, service.data.get(ATTR_ENTITY_ID)))
|
|
|
|
|
|
|
|
bus.register_service(ha.DOMAIN, SERVICE_TURN_ON,
|
|
|
|
lambda service:
|
|
|
|
turn_on(bus, service.data.get(ATTR_ENTITY_ID)))
|
|
|
|
|
|
|
|
return True
|