2019-02-13 20:21:14 +00:00
|
|
|
"""Allow users to set and activate scenes."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
import functools as ft
|
2018-05-01 18:57:30 +00:00
|
|
|
import importlib
|
2016-02-22 18:53:55 +00:00
|
|
|
import logging
|
2022-09-14 09:18:23 +00:00
|
|
|
from typing import Any, Final, final
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2016-04-12 06:01:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
from homeassistant.components.light import ATTR_TRANSITION
|
2021-06-17 08:10:26 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-03-08 04:43:19 +00:00
|
|
|
from homeassistant.const import CONF_PLATFORM, SERVICE_TURN_ON, STATE_UNAVAILABLE
|
2021-06-17 08:10:26 +00:00
|
|
|
from homeassistant.core import DOMAIN as HA_DOMAIN, HomeAssistant
|
2016-02-22 18:53:55 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2022-01-07 18:02:32 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2022-01-02 15:29:52 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2022-01-07 18:02:32 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2022-09-14 09:18:23 +00:00
|
|
|
DOMAIN: Final = "scene"
|
|
|
|
STATES: Final = "states"
|
2019-08-12 03:38:18 +00:00
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2022-09-14 09:18:23 +00:00
|
|
|
def _hass_domain_validator(config: dict[str, Any]) -> dict[str, Any]:
|
2017-03-29 06:39:53 +00:00
|
|
|
"""Validate platform in config for homeassistant domain."""
|
|
|
|
if CONF_PLATFORM not in config:
|
2019-11-04 10:12:04 +00:00
|
|
|
config = {CONF_PLATFORM: HA_DOMAIN, STATES: config}
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2022-09-14 09:18:23 +00:00
|
|
|
def _platform_validator(config: dict[str, Any]) -> dict[str, Any]:
|
2017-03-29 06:39:53 +00:00
|
|
|
"""Validate it is a valid platform."""
|
2018-05-01 18:57:30 +00:00
|
|
|
try:
|
2021-04-09 16:58:27 +00:00
|
|
|
platform = importlib.import_module(f".{config[CONF_PLATFORM]}", __name__)
|
2018-05-01 18:57:30 +00:00
|
|
|
except ImportError:
|
2019-02-23 17:26:27 +00:00
|
|
|
try:
|
|
|
|
platform = importlib.import_module(
|
2021-04-09 16:58:27 +00:00
|
|
|
f"homeassistant.components.{config[CONF_PLATFORM]}.scene"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
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
|
2017-03-29 06:39:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if not hasattr(platform, "PLATFORM_SCHEMA"):
|
2017-03-29 06:39:53 +00:00
|
|
|
return config
|
|
|
|
|
2022-09-14 09:18:23 +00:00
|
|
|
return platform.PLATFORM_SCHEMA(config) # type: ignore[no-any-return]
|
2017-03-29 06:39:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-09-17 16:18:53 +00:00
|
|
|
# mypy: disallow-any-generics
|
|
|
|
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2022-01-02 15:29:52 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the scenes."""
|
2022-09-17 16:18:53 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent[Scene](
|
2020-10-17 02:24:08 +00:00
|
|
|
logging.getLogger(__name__), DOMAIN, hass
|
|
|
|
)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
await component.async_setup(config)
|
2019-08-05 21:04:20 +00:00
|
|
|
# Ensure Home Assistant platform always loaded.
|
2020-01-27 07:01:35 +00:00
|
|
|
await component.async_setup_platform(HA_DOMAIN, {"platform": HA_DOMAIN, STATES: []})
|
2020-04-21 01:07:50 +00: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 18:02:32 +00:00
|
|
|
"_async_activate",
|
2020-04-21 01:07:50 +00:00
|
|
|
)
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-06-17 08:10:26 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up a config entry."""
|
2022-09-17 16:18:53 +00:00
|
|
|
component: EntityComponent[Scene] = hass.data[DOMAIN]
|
2021-06-17 08:10:26 +00:00
|
|
|
return await component.async_setup_entry(entry)
|
2018-04-23 16:00:16 +00:00
|
|
|
|
|
|
|
|
2021-06-17 08:10:26 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-04-29 14:16:20 +00:00
|
|
|
"""Unload a config entry."""
|
2022-09-17 16:18:53 +00:00
|
|
|
component: EntityComponent[Scene] = hass.data[DOMAIN]
|
2021-06-17 08:10:26 +00:00
|
|
|
return await component.async_unload_entry(entry)
|
2018-04-29 14:16:20 +00:00
|
|
|
|
|
|
|
|
2022-01-07 18:02:32 +00:00
|
|
|
class Scene(RestoreEntity):
|
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
|
|
|
|
2022-01-07 18:02:32 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
__last_activated: str | None = None
|
2016-02-22 18:53:55 +00:00
|
|
|
|
|
|
|
@property
|
2022-01-07 18:02:32 +00:00
|
|
|
@final
|
2021-03-18 13:31:38 +00:00
|
|
|
def state(self) -> str | None:
|
2016-03-06 03:32:28 +00:00
|
|
|
"""Return the state of the scene."""
|
2022-01-07 18:02:32 +00: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 18:43:06 +00: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 18:02:32 +00:00
|
|
|
state = await self.async_get_last_state()
|
2022-03-08 04:43:19 +00:00
|
|
|
if (
|
|
|
|
state is not None
|
|
|
|
and state.state is not None
|
|
|
|
and state.state != STATE_UNAVAILABLE
|
|
|
|
):
|
2022-01-07 18:02:32 +00:00
|
|
|
self.__last_activated = state.state
|
2016-02-22 18:53:55 +00:00
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
def activate(self, **kwargs: Any) -> None:
|
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()
|
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
async def async_activate(self, **kwargs: Any) -> None:
|
2020-01-29 21:59:45 +00:00
|
|
|
"""Activate scene. Try to get entities into requested state."""
|
2020-04-21 01:07:50 +00:00
|
|
|
task = self.hass.async_add_job(ft.partial(self.activate, **kwargs))
|
|
|
|
if task:
|
|
|
|
await task
|