core/homeassistant/components/scene/homeassistant.py

92 lines
2.6 KiB
Python
Raw Normal View History

2015-03-16 06:36:42 +00:00
"""
Allow 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 asyncio
2015-03-16 06:36:42 +00:00
from collections import namedtuple
2016-02-22 18:53:55 +00:00
from homeassistant.components.scene import Scene
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
2016-02-22 18:53:55 +00:00
ATTR_ENTITY_ID, STATE_OFF, STATE_ON)
2015-08-17 03:44:46 +00:00
from homeassistant.core import State
from homeassistant.helpers.state import async_reproduce_state
2015-03-16 06:36:42 +00:00
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
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
2016-03-07 21:50:56 +00:00
"""Setup home assistant scene entries."""
scene_config = config.get("states")
2015-03-16 06:36:42 +00:00
if not isinstance(scene_config, list):
scene_config = [scene_config]
async_add_devices(HomeAssistantScene(
hass, _process_config(scene)) for scene in scene_config)
2015-03-16 06:36:42 +00:00
return True
def _process_config(scene_config):
"""Process passed in config into a format to work with.
Async friendly.
"""
2015-03-16 06:36:42 +00:00
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
2016-02-22 18:53:55 +00:00
class HomeAssistantScene(Scene):
2016-03-07 21:50:56 +00:00
"""A scene is a group of entities and the states we want them to be."""
2015-03-16 06:36:42 +00:00
def __init__(self, hass, scene_config):
2016-03-07 21:50:56 +00:00
"""Initialize the scene."""
2015-03-16 06:36:42 +00:00
self.hass = hass
self.scene_config = scene_config
@property
def name(self):
2016-03-07 21:50:56 +00:00
"""Return the name of the scene."""
2015-03-16 06:36:42 +00:00
return self.scene_config.name
@property
2016-02-22 18:53:55 +00:00
def device_state_attributes(self):
2016-03-07 21:50:56 +00:00
"""Return the scene state attributes."""
2015-03-16 06:36:42 +00:00
return {
2016-02-22 18:53:55 +00:00
ATTR_ENTITY_ID: list(self.scene_config.states.keys()),
2015-03-16 06:36:42 +00:00
}
@asyncio.coroutine
def async_activate(self):
"""Activate scene. Try to get entities into requested state."""
yield from async_reproduce_state(
self.hass, self.scene_config.states.values(), True)