core/homeassistant/components/scene.py

131 lines
3.5 KiB
Python
Raw Normal View History

2015-03-16 06:36:42 +00:00
"""
homeassistant.components.scene
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-10-25 14:13:38 +00:00
Allows users to set and activate scenes.
2015-03-16 06:36:42 +00:00
2015-10-25 14:13:38 +00:00
For more details about this component, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/scene/
2015-03-16 06:36:42 +00:00
"""
import logging
from collections import namedtuple
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_OFF, STATE_ON)
2015-08-17 03:44:46 +00:00
from homeassistant.core import State
2015-10-12 06:48:55 +00:00
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
2015-03-16 06:36:42 +00:00
from homeassistant.helpers.state import reproduce_state
DOMAIN = 'scene'
DEPENDENCIES = ['group']
2015-10-12 06:48:55 +00:00
STATE = 'scening'
2015-03-16 06:36:42 +00:00
CONF_ENTITIES = "entities"
2015-10-12 06:48:55 +00:00
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
2015-03-16 06:36:42 +00:00
2015-10-12 06:48:17 +00:00
def activate(hass, entity_id=None):
""" Activate a scene. """
data = {}
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
2015-03-16 06:36:42 +00:00
def setup(hass, config):
""" Sets up scenes. """
logger = logging.getLogger(__name__)
scene_configs = config.get(DOMAIN)
2015-10-12 06:48:17 +00:00
if not isinstance(scene_configs, list) or \
any(not isinstance(item, dict) for item in scene_configs):
logger.error('Scene config should be a list of dictionaries')
2015-03-16 06:36:42 +00:00
return False
component = EntityComponent(logger, DOMAIN, hass)
2015-03-16 06:36:42 +00:00
component.add_entities(Scene(hass, _process_config(scene_config))
for scene_config in scene_configs)
2015-03-16 06:36:42 +00:00
def handle_scene_service(service):
""" Handles calls to the switch services. """
target_scenes = component.extract_from_service(service)
for scene in target_scenes:
2015-10-12 06:48:55 +00:00
scene.activate()
2015-03-16 06:36:42 +00:00
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_scene_service)
return True
def _process_config(scene_config):
""" Process passed in config into a format to work with. """
name = scene_config.get('name')
2015-03-16 06:36:42 +00:00
states = {}
c_entities = dict(scene_config.get(CONF_ENTITIES, {}))
for entity_id in c_entities:
if isinstance(c_entities[entity_id], dict):
entity_attrs = c_entities[entity_id].copy()
state = entity_attrs.pop('state', None)
attributes = entity_attrs
2015-03-16 06:36:42 +00:00
else:
state = c_entities[entity_id]
attributes = {}
# YAML translates 'on' to a boolean
# http://yaml.org/type/bool.html
if isinstance(state, bool):
state = STATE_ON if state else STATE_OFF
else:
state = str(state)
states[entity_id.lower()] = State(entity_id, state, attributes)
2015-10-12 06:48:55 +00:00
return SceneConfig(name, states)
2015-03-16 06:36:42 +00:00
2015-10-12 06:48:55 +00:00
class Scene(Entity):
2015-03-16 06:36:42 +00:00
""" A scene is a group of entities and the states we want them to be. """
def __init__(self, hass, scene_config):
self.hass = hass
self.scene_config = scene_config
self.update()
@property
def should_poll(self):
return False
@property
def name(self):
return self.scene_config.name
@property
2015-10-12 06:48:55 +00:00
def state(self):
return STATE
2015-03-16 06:36:42 +00:00
@property
def entity_ids(self):
""" Entity IDs part of this scene. """
return self.scene_config.states.keys()
@property
def state_attributes(self):
""" Scene state attributes. """
return {
ATTR_ENTITY_ID: list(self.entity_ids),
}
2015-10-12 06:48:55 +00:00
def activate(self):
2015-03-16 06:36:42 +00:00
""" Activates scene. Tries to get entities into requested state. """
2015-10-12 06:48:55 +00:00
reproduce_state(self.hass, self.scene_config.states.values(), True)