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 14:57:30 -04:00
|
|
|
import importlib
|
2016-02-22 19:53:55 +01:00
|
|
|
import logging
|
2022-01-07 19:02:32 +01:00
|
|
|
from typing import Any, final
|
2016-02-22 19:53:55 +01:00
|
|
|
|
2016-04-12 02:01:22 -04:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
from homeassistant.components.light import ATTR_TRANSITION
|
2021-06-17 10:10:26 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-03-08 05:43:19 +01:00
|
|
|
from homeassistant.const import CONF_PLATFORM, SERVICE_TURN_ON, STATE_UNAVAILABLE
|
2021-06-17 10:10:26 +02:00
|
|
|
from homeassistant.core import DOMAIN as HA_DOMAIN, HomeAssistant
|
2016-02-22 19:53:55 +01:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2022-01-07 19:02:32 +01:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2022-01-02 16:29:52 +01:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2022-01-07 19:02:32 +01:00
|
|
|
from homeassistant.util import dt as dt_util
|
2016-02-22 19:53:55 +01:00
|
|
|
|
2019-08-12 06:38:18 +03:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DOMAIN = "scene"
|
|
|
|
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 02:12:04 -08: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 14:57:30 -04:00
|
|
|
try:
|
2021-04-09 18:58:27 +02:00
|
|
|
platform = importlib.import_module(f".{config[CONF_PLATFORM]}", __name__)
|
2018-05-01 14:57:30 -04:00
|
|
|
except ImportError:
|
2019-02-23 19:26:27 +02:00
|
|
|
try:
|
|
|
|
platform = importlib.import_module(
|
2021-04-09 18:58:27 +02:00
|
|
|
f"homeassistant.components.{config[CONF_PLATFORM]}.scene"
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2019-02-23 19:26:27 +02:00
|
|
|
except ImportError:
|
2019-07-31 12:25:30 -07:00
|
|
|
raise vol.Invalid("Invalid platform specified") from None
|
2017-03-29 08:39:53 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if not hasattr(platform, "PLATFORM_SCHEMA"):
|
2017-03-29 08:39:53 +02:00
|
|
|
return config
|
|
|
|
|
2018-05-01 14:57:30 -04: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 12:25:30 -07: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
|
|
|
|
|
|
|
|
2022-01-02 16:29:52 +01:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
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 14:04:20 -07:00
|
|
|
# Ensure Home Assistant platform always loaded.
|
2020-01-26 23:01:35 -08: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))},
|
2022-01-07 19:02:32 +01:00
|
|
|
"_async_activate",
|
2020-04-21 03:07:50 +02:00
|
|
|
)
|
2016-02-22 19:53:55 +01:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-06-17 10:10:26 +02:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-08-19 22:29:08 +02:00
|
|
|
"""Set up a config entry."""
|
2021-06-17 10:10:26 +02:00
|
|
|
component: EntityComponent = hass.data[DOMAIN]
|
|
|
|
return await component.async_setup_entry(entry)
|
2018-04-23 18:00:16 +02:00
|
|
|
|
|
|
|
|
2021-06-17 10:10:26 +02:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-04-29 16:16:20 +02:00
|
|
|
"""Unload a config entry."""
|
2021-06-17 10:10:26 +02:00
|
|
|
component: EntityComponent = hass.data[DOMAIN]
|
|
|
|
return await component.async_unload_entry(entry)
|
2018-04-29 16:16:20 +02:00
|
|
|
|
|
|
|
|
2022-01-07 19:02:32 +01:00
|
|
|
class Scene(RestoreEntity):
|
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
|
|
|
|
2022-01-07 19:02:32 +01:00
|
|
|
_attr_should_poll = False
|
|
|
|
__last_activated: str | None = None
|
2016-02-22 19:53:55 +01:00
|
|
|
|
|
|
|
@property
|
2022-01-07 19:02:32 +01:00
|
|
|
@final
|
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."""
|
2022-01-07 19:02:32 +01:00
|
|
|
if self.__last_activated is None:
|
|
|
|
return None
|
|
|
|
return self.__last_activated
|
|
|
|
|
|
|
|
@final
|
|
|
|
async def _async_activate(self, **kwargs: Any) -> None:
|
|
|
|
"""Activate scene.
|
|
|
|
|
|
|
|
Should not be overridden, handle setting last press timestamp.
|
|
|
|
"""
|
|
|
|
self.__last_activated = dt_util.utcnow().isoformat()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
await self.async_activate(**kwargs)
|
|
|
|
|
2022-02-04 10:43:06 -08:00
|
|
|
async def async_internal_added_to_hass(self) -> None:
|
|
|
|
"""Call when the scene is added to hass."""
|
|
|
|
await super().async_internal_added_to_hass()
|
2022-01-07 19:02:32 +01:00
|
|
|
state = await self.async_get_last_state()
|
2022-03-08 05:43:19 +01:00
|
|
|
if (
|
|
|
|
state is not None
|
|
|
|
and state.state is not None
|
|
|
|
and state.state != STATE_UNAVAILABLE
|
|
|
|
):
|
2022-01-07 19:02:32 +01:00
|
|
|
self.__last_activated = state.state
|
2016-02-22 19:53:55 +01:00
|
|
|
|
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 13:59:45 -08: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
|