2016-02-22 18:53:55 +00:00
|
|
|
"""
|
2016-03-07 21:50:56 +00:00
|
|
|
Allow users to set and activate scenes.
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/scene/
|
|
|
|
"""
|
2016-12-02 05:38:12 +00:00
|
|
|
import asyncio
|
2016-02-22 18:53:55 +00:00
|
|
|
import logging
|
|
|
|
from collections import namedtuple
|
|
|
|
|
2016-04-12 06:01:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID, SERVICE_TURN_ON, CONF_PLATFORM)
|
2016-02-28 09:24:02 +00:00
|
|
|
from homeassistant.helpers import extract_domain_configs
|
2016-04-12 06:01:22 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-22 18:53:55 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
|
|
|
|
|
|
|
DOMAIN = 'scene'
|
|
|
|
DEPENDENCIES = ['group']
|
|
|
|
STATE = 'scening'
|
|
|
|
|
|
|
|
CONF_ENTITIES = "entities"
|
|
|
|
|
2016-04-12 06:01:22 +00:00
|
|
|
SCENE_SERVICE_SCHEMA = vol.Schema({
|
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
|
|
|
})
|
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
|
|
|
|
|
|
|
|
|
|
|
|
def activate(hass, entity_id=None):
|
2016-03-07 21:50:56 +00:00
|
|
|
"""Activate a scene."""
|
2016-02-22 18:53:55 +00:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
if entity_id:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
|
|
|
|
|
|
|
|
|
2016-12-02 05:38:12 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2016-03-06 03:32:28 +00:00
|
|
|
"""Setup scenes."""
|
2016-02-22 18:53:55 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-02-28 09:24:02 +00:00
|
|
|
# You are not allowed to mutate the original config so make a copy
|
|
|
|
config = dict(config)
|
|
|
|
|
|
|
|
for config_key in extract_domain_configs(config, DOMAIN):
|
|
|
|
platform_config = config[config_key]
|
|
|
|
if not isinstance(platform_config, list):
|
|
|
|
platform_config = [platform_config]
|
|
|
|
|
|
|
|
if not any(CONF_PLATFORM in entry for entry in platform_config):
|
2016-02-28 20:00:51 +00:00
|
|
|
platform_config = [{'platform': 'homeassistant', 'states': entry}
|
2016-02-28 09:24:02 +00:00
|
|
|
for entry in platform_config]
|
|
|
|
|
|
|
|
config[config_key] = platform_config
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
component = EntityComponent(logger, DOMAIN, hass)
|
|
|
|
|
2016-12-02 05:38:12 +00:00
|
|
|
yield from component.async_setup(config)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2016-12-02 05:38:12 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_handle_scene_service(service):
|
2016-03-07 21:50:56 +00:00
|
|
|
"""Handle calls to the switch services."""
|
2016-12-02 05:38:12 +00:00
|
|
|
target_scenes = component.async_extract_from_service(service)
|
|
|
|
print(target_scenes)
|
|
|
|
print(component.entities)
|
|
|
|
tasks = [scene.async_activate() for scene in target_scenes]
|
|
|
|
if tasks:
|
|
|
|
yield from asyncio.wait(tasks, loop=hass.loop)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2016-12-02 05:38:12 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_TURN_ON, async_handle_scene_service,
|
|
|
|
schema=SCENE_SERVICE_SCHEMA)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Scene(Entity):
|
2016-03-07 21:50:56 +00:00
|
|
|
"""A scene is a group of entities and the states we want them to be."""
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-07 21:50:56 +00:00
|
|
|
"""No polling needed."""
|
2016-02-22 18:53:55 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-06 03:32:28 +00:00
|
|
|
"""Return the state of the scene."""
|
2016-02-22 18:53:55 +00:00
|
|
|
return STATE
|
|
|
|
|
|
|
|
def activate(self):
|
2016-03-06 03:32:28 +00:00
|
|
|
"""Activate scene. Try to get entities into requested state."""
|
2016-12-02 05:38:12 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_activate(self):
|
|
|
|
"""Activate scene. Try to get entities into requested state.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
yield from self.hass.loop.run_in_executor(None, self.activate)
|