2019-02-13 21:21:14 +01:00
|
|
|
"""Allow users to set and activate scenes."""
|
2021-03-18 14:31:38 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
import functools as ft
|
2018-05-01 20:57:30 +02:00
|
|
|
import importlib
|
2016-02-22 19:53:55 +01:00
|
|
|
import logging
|
2021-03-18 14:31:38 +01:00
|
|
|
from typing import Any
|
2016-02-22 19:53:55 +01:00
|
|
|
|
2016-04-12 08:01:22 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
from homeassistant.components.light import ATTR_TRANSITION
|
2019-07-23 21:38:08 +02:00
|
|
|
from homeassistant.const import CONF_PLATFORM, SERVICE_TURN_ON
|
2019-12-09 13:12:43 +01:00
|
|
|
from homeassistant.core import DOMAIN as HA_DOMAIN
|
2016-02-22 19:53:55 +01:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
|
|
|
|
2019-08-12 05:38:18 +02:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
DOMAIN = "scene"
|
|
|
|
STATE = "scening"
|
|
|
|
STATES = "states"
|
2016-02-22 19:53:55 +01:00
|
|
|
|
2017-03-29 08:39:53 +02:00
|
|
|
|
|
|
|
def _hass_domain_validator(config):
|
|
|
|
"""Validate platform in config for homeassistant domain."""
|
|
|
|
if CONF_PLATFORM not in config:
|
2019-11-04 11:12:04 +01:00
|
|
|
config = {CONF_PLATFORM: HA_DOMAIN, STATES: config}
|
2017-03-29 08:39:53 +02:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def _platform_validator(config):
|
|
|
|
"""Validate it is a valid platform."""
|
2018-05-01 20:57:30 +02:00
|
|
|
try:
|
2021-04-09 18:58:27 +02:00
|
|
|
platform = importlib.import_module(f".{config[CONF_PLATFORM]}", __name__)
|
2018-05-01 20:57:30 +02:00
|
|
|
except ImportError:
|
2019-02-23 18:26:27 +01:00
|
|
|
try:
|
|
|
|
platform = importlib.import_module(
|
2021-04-09 18:58:27 +02:00
|
|
|
f"homeassistant.components.{config[CONF_PLATFORM]}.scene"
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2019-02-23 18:26:27 +01:00
|
|
|
except ImportError:
|
2019-07-31 21:25:30 +02:00
|
|
|
raise vol.Invalid("Invalid platform specified") from None
|
2017-03-29 08:39:53 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
if not hasattr(platform, "PLATFORM_SCHEMA"):
|
2017-03-29 08:39:53 +02:00
|
|
|
return config
|
|
|
|
|
2018-05-01 20:57:30 +02:00
|
|
|
return platform.PLATFORM_SCHEMA(config)
|
2017-03-29 08:39:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema(
|
|
|
|
vol.All(
|
|
|
|
_hass_domain_validator,
|
2019-07-31 21:25:30 +02:00
|
|
|
vol.Schema({vol.Required(CONF_PLATFORM): str}, extra=vol.ALLOW_EXTRA),
|
|
|
|
_platform_validator,
|
|
|
|
),
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-02-22 19:53:55 +01:00
|
|
|
|
|
|
|
|
2018-02-24 19:24:33 +01:00
|
|
|
async def async_setup(hass, config):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the scenes."""
|
2020-10-17 04:24:08 +02:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(
|
|
|
|
logging.getLogger(__name__), DOMAIN, hass
|
|
|
|
)
|
2016-02-22 19:53:55 +01:00
|
|
|
|
2018-02-24 19:24:33 +01:00
|
|
|
await component.async_setup(config)
|
2019-08-05 23:04:20 +02:00
|
|
|
# Ensure Home Assistant platform always loaded.
|
2020-01-27 08:01:35 +01:00
|
|
|
await component.async_setup_platform(HA_DOMAIN, {"platform": HA_DOMAIN, STATES: []})
|
2020-04-21 03:07:50 +02:00
|
|
|
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 19:53:55 +01:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-04-23 18:00:16 +02:00
|
|
|
async def async_setup_entry(hass, entry):
|
2018-08-19 22:29:08 +02:00
|
|
|
"""Set up a config entry."""
|
2018-04-23 18:00:16 +02:00
|
|
|
return await hass.data[DOMAIN].async_setup_entry(entry)
|
|
|
|
|
|
|
|
|
2018-04-29 16:16:20 +02:00
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_unload_entry(entry)
|
|
|
|
|
|
|
|
|
2016-02-22 19:53:55 +01:00
|
|
|
class Scene(Entity):
|
2016-03-07 22:50:56 +01:00
|
|
|
"""A scene is a group of entities and the states we want them to be."""
|
2016-02-22 19:53:55 +01:00
|
|
|
|
|
|
|
@property
|
2020-04-21 03:07:50 +02:00
|
|
|
def should_poll(self) -> bool:
|
2016-03-07 22:50:56 +01:00
|
|
|
"""No polling needed."""
|
2016-02-22 19:53:55 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2021-03-18 14:31:38 +01:00
|
|
|
def state(self) -> str | None:
|
2016-03-06 04:32:28 +01:00
|
|
|
"""Return the state of the scene."""
|
2016-02-22 19:53:55 +01:00
|
|
|
return STATE
|
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
def activate(self, **kwargs: Any) -> None:
|
2016-03-06 04:32:28 +01:00
|
|
|
"""Activate scene. Try to get entities into requested state."""
|
2016-12-02 06:38:12 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
async def async_activate(self, **kwargs: Any) -> None:
|
2020-01-29 22:59:45 +01:00
|
|
|
"""Activate scene. Try to get entities into requested state."""
|
2020-04-21 03:07:50 +02:00
|
|
|
task = self.hass.async_add_job(ft.partial(self.activate, **kwargs))
|
|
|
|
if task:
|
|
|
|
await task
|