2019-02-13 21:21:14 +01:00
|
|
|
"""Support for deCONZ scenes."""
|
2021-11-14 19:47:15 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2022-04-26 11:42:56 +02:00
|
|
|
from pydeconz.models.event import EventType
|
2021-11-14 19:47:15 +01:00
|
|
|
|
2022-01-30 00:11:28 +01:00
|
|
|
from homeassistant.components.scene import DOMAIN, Scene
|
2021-11-14 19:47:15 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2022-02-16 17:55:30 +01:00
|
|
|
from .deconz_device import DeconzSceneMixin
|
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-02-18 17:43:22 +01:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-11-14 19:47:15 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-04-26 11:42:56 +02:00
|
|
|
"""Set up scenes for deCONZ integration."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2022-01-30 00:11:28 +01:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 16:21:44 +01:00
|
|
|
|
2018-09-04 09:24:42 +02:00
|
|
|
@callback
|
2022-04-26 11:42:56 +02:00
|
|
|
def async_add_scene(_: EventType, scene_id: str) -> None:
|
2018-09-04 09:24:42 +02:00
|
|
|
"""Add scene from deCONZ."""
|
2022-04-26 11:42:56 +02:00
|
|
|
scene = gateway.api.scenes[scene_id]
|
|
|
|
async_add_entities([DeconzScene(scene, gateway)])
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2021-04-20 20:20:57 +02:00
|
|
|
config_entry.async_on_unload(
|
2022-04-26 11:42:56 +02:00
|
|
|
gateway.api.scenes.subscribe(
|
2021-10-07 12:48:27 +02:00
|
|
|
async_add_scene,
|
2022-04-26 11:42:56 +02:00
|
|
|
EventType.ADDED,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2018-09-04 09:24:42 +02:00
|
|
|
|
2022-04-26 11:42:56 +02:00
|
|
|
for scene_id in gateway.api.scenes:
|
|
|
|
async_add_scene(EventType.ADDED, scene_id)
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
|
2022-02-16 17:55:30 +01:00
|
|
|
class DeconzScene(DeconzSceneMixin, Scene):
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Representation of a deCONZ scene."""
|
|
|
|
|
2022-02-16 17:55:30 +01:00
|
|
|
TYPE = DOMAIN
|
2018-08-29 23:18:20 +02:00
|
|
|
|
2020-04-21 03:07:50 +02:00
|
|
|
async def async_activate(self, **kwargs: Any) -> None:
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Activate the scene."""
|
2022-02-16 17:55:30 +01:00
|
|
|
await self._device.recall()
|