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
|
2018-05-01 18:57:30 +00:00
|
|
|
import importlib
|
2016-02-22 18:53:55 +00:00
|
|
|
import logging
|
|
|
|
|
2016-04-12 06:01:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
from homeassistant.const import (
|
2017-03-29 06:39:53 +00:00
|
|
|
ATTR_ENTITY_ID, CONF_PLATFORM, SERVICE_TURN_ON)
|
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
|
2017-03-29 06:39:53 +00:00
|
|
|
from homeassistant.helpers.state import HASS_DOMAIN
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
DOMAIN = 'scene'
|
|
|
|
STATE = 'scening'
|
2017-03-29 06:39:53 +00:00
|
|
|
STATES = 'states'
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
def _hass_domain_validator(config):
|
|
|
|
"""Validate platform in config for homeassistant domain."""
|
|
|
|
if CONF_PLATFORM not in config:
|
|
|
|
config = {
|
|
|
|
CONF_PLATFORM: HASS_DOMAIN, STATES: config}
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def _platform_validator(config):
|
|
|
|
"""Validate it is a valid platform."""
|
2018-05-01 18:57:30 +00:00
|
|
|
try:
|
|
|
|
platform = importlib.import_module(
|
|
|
|
'homeassistant.components.scene.{}'.format(
|
|
|
|
config[CONF_PLATFORM]))
|
|
|
|
except ImportError:
|
|
|
|
raise vol.Invalid('Invalid platform specified') from None
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
if not hasattr(platform, 'PLATFORM_SCHEMA'):
|
|
|
|
return config
|
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
return platform.PLATFORM_SCHEMA(config)
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema(
|
|
|
|
vol.All(
|
|
|
|
_hass_domain_validator,
|
|
|
|
vol.Schema({
|
2018-05-01 18:57:30 +00:00
|
|
|
vol.Required(CONF_PLATFORM): str
|
2017-03-29 06:39:53 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA),
|
|
|
|
_platform_validator
|
|
|
|
), extra=vol.ALLOW_EXTRA)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2016-04-12 06:01:22 +00:00
|
|
|
SCENE_SERVICE_SCHEMA = vol.Schema({
|
2018-12-13 09:07:59 +00:00
|
|
|
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
|
2016-04-12 06:01:22 +00:00
|
|
|
})
|
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the scenes."""
|
2016-02-22 18:53:55 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-04-23 16:00:16 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(logger, DOMAIN, hass)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
await component.async_setup(config)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async 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)
|
2016-12-02 17:13:39 +00:00
|
|
|
|
2016-12-02 05:38:12 +00:00
|
|
|
tasks = [scene.async_activate() for scene in target_scenes]
|
|
|
|
if tasks:
|
2018-02-24 18:24:33 +00:00
|
|
|
await 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
|
|
|
|
|
|
|
|
|
2018-04-23 16:00:16 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up a config entry."""
|
2018-04-23 16:00:16 +00:00
|
|
|
return await hass.data[DOMAIN].async_setup_entry(entry)
|
|
|
|
|
|
|
|
|
2018-04-29 14:16:20 +00:00
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_unload_entry(entry)
|
|
|
|
|
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
def async_activate(self):
|
|
|
|
"""Activate scene. Try to get entities into requested state.
|
|
|
|
|
2016-12-26 13:10:23 +00:00
|
|
|
This method must be run in the event loop and returns a coroutine.
|
2016-12-02 05:38:12 +00:00
|
|
|
"""
|
2017-05-26 15:28:07 +00:00
|
|
|
return self.hass.async_add_job(self.activate)
|