2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ scenes."""
|
2021-11-14 18:47:15 +00:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import ValuesView
|
2020-04-21 01:07:50 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-11-14 18:47:15 +00:00
|
|
|
from pydeconz.group import DeconzScene as PydeconzScene
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.components.scene import Scene
|
2021-11-14 18:47:15 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2018-09-04 07:24:42 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-11-14 18:47:15 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2021-11-14 18:47:15 +00:00
|
|
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
2019-02-18 16:43:22 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2021-11-14 18:47:15 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-04-23 16:00:16 +00:00
|
|
|
"""Set up scenes for deCONZ component."""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-09-04 07:24:42 +00:00
|
|
|
@callback
|
2021-11-14 18:47:15 +00:00
|
|
|
def async_add_scene(
|
|
|
|
scenes: list[PydeconzScene]
|
|
|
|
| ValuesView[PydeconzScene] = gateway.api.scenes.values(),
|
|
|
|
) -> None:
|
2018-09-04 07:24:42 +00:00
|
|
|
"""Add scene from deCONZ."""
|
2020-04-21 01:07:50 +00:00
|
|
|
entities = [DeconzScene(scene, gateway) for scene in scenes]
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
if entities:
|
|
|
|
async_add_entities(entities)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2021-04-20 18:20:57 +00:00
|
|
|
config_entry.async_on_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect(
|
2021-10-07 10:48:27 +00:00
|
|
|
hass,
|
|
|
|
gateway.signal_new_scene,
|
|
|
|
async_add_scene,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-09-04 07:24:42 +00:00
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
async_add_scene()
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzScene(Scene):
|
|
|
|
"""Representation of a deCONZ scene."""
|
|
|
|
|
2021-11-14 18:47:15 +00:00
|
|
|
def __init__(self, scene: PydeconzScene, gateway: DeconzGateway) -> None:
|
2018-01-19 06:36:29 +00:00
|
|
|
"""Set up a scene."""
|
2018-01-01 16:08:13 +00:00
|
|
|
self._scene = scene
|
2018-11-05 15:21:44 +00:00
|
|
|
self.gateway = gateway
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2021-06-23 19:40:34 +00:00
|
|
|
self._attr_name = scene.full_name
|
|
|
|
|
2021-11-14 18:47:15 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2018-02-14 00:23:04 +00:00
|
|
|
"""Subscribe to sensors events."""
|
2018-11-05 15:21:44 +00:00
|
|
|
self.gateway.deconz_ids[self.entity_id] = self._scene.deconz_id
|
2018-02-14 00:23:04 +00:00
|
|
|
|
2018-08-29 21:18:20 +00:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect scene object when removed."""
|
2019-09-05 23:38:00 +00:00
|
|
|
del self.gateway.deconz_ids[self.entity_id]
|
2018-08-29 21:18:20 +00:00
|
|
|
self._scene = None
|
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
async def async_activate(self, **kwargs: Any) -> None:
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Activate the scene."""
|
2021-09-18 07:05:08 +00:00
|
|
|
await self._scene.recall()
|