core/homeassistant/components/scene/__init__.py

110 lines
3.1 KiB
Python
Raw Normal View History

"""Allow users to set and activate scenes."""
import functools as ft
import importlib
2016-02-22 18:53:55 +00:00
import logging
from typing import Any, Optional
2016-02-22 18:53:55 +00:00
import voluptuous as vol
from homeassistant.components.light import ATTR_TRANSITION
from homeassistant.const import CONF_PLATFORM, SERVICE_TURN_ON
from homeassistant.core import DOMAIN as HA_DOMAIN
2016-02-22 18:53:55 +00:00
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
# mypy: allow-untyped-defs, no-check-untyped-defs
2019-07-31 19:25:30 +00:00
DOMAIN = "scene"
STATE = "scening"
STATES = "states"
2016-02-22 18:53:55 +00:00
def _hass_domain_validator(config):
"""Validate platform in config for homeassistant domain."""
if CONF_PLATFORM not in config:
config = {CONF_PLATFORM: HA_DOMAIN, STATES: config}
return config
def _platform_validator(config):
"""Validate it is a valid platform."""
try:
2019-07-31 19:25:30 +00:00
platform = importlib.import_module(
".{}".format(config[CONF_PLATFORM]), __name__
)
except ImportError:
2019-02-23 17:26:27 +00:00
try:
platform = importlib.import_module(
2019-07-31 19:25:30 +00:00
"homeassistant.components.{}.scene".format(config[CONF_PLATFORM])
)
2019-02-23 17:26:27 +00:00
except ImportError:
2019-07-31 19:25:30 +00:00
raise vol.Invalid("Invalid platform specified") from None
2019-07-31 19:25:30 +00:00
if not hasattr(platform, "PLATFORM_SCHEMA"):
return config
return platform.PLATFORM_SCHEMA(config)
PLATFORM_SCHEMA = vol.Schema(
vol.All(
_hass_domain_validator,
2019-07-31 19:25:30 +00:00
vol.Schema({vol.Required(CONF_PLATFORM): str}, extra=vol.ALLOW_EXTRA),
_platform_validator,
),
extra=vol.ALLOW_EXTRA,
)
2016-02-22 18:53:55 +00:00
async def async_setup(hass, config):
"""Set up the scenes."""
2016-02-22 18:53:55 +00:00
logger = logging.getLogger(__name__)
component = hass.data[DOMAIN] = EntityComponent(logger, DOMAIN, hass)
2016-02-22 18:53:55 +00:00
await component.async_setup(config)
# Ensure Home Assistant platform always loaded.
await component.async_setup_platform(HA_DOMAIN, {"platform": HA_DOMAIN, STATES: []})
component.async_register_entity_service(
SERVICE_TURN_ON,
{ATTR_TRANSITION: vol.All(vol.Coerce(float), vol.Clamp(min=0, max=6553))},
"async_activate",
)
2016-02-22 18:53:55 +00:00
return True
async def async_setup_entry(hass, entry):
2018-08-19 20:29:08 +00:00
"""Set up a config entry."""
return await hass.data[DOMAIN].async_setup_entry(entry)
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) -> bool:
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) -> Optional[str]:
"""Return the state of the scene."""
2016-02-22 18:53:55 +00:00
return STATE
def activate(self, **kwargs: Any) -> None:
"""Activate scene. Try to get entities into requested state."""
raise NotImplementedError()
async def async_activate(self, **kwargs: Any) -> None:
"""Activate scene. Try to get entities into requested state."""
assert self.hass
task = self.hass.async_add_job(ft.partial(self.activate, **kwargs))
if task:
await task