2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ scenes."""
|
2020-04-21 01:07:50 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.components.scene import Scene
|
2018-09-04 07:24:42 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .const import NEW_SCENE
|
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-02-18 16:43:22 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
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
|
2020-12-02 15:21:27 +00:00
|
|
|
def async_add_scene(scenes=gateway.api.scenes.values()):
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(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."""
|
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
def __init__(self, scene, gateway):
|
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
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_added_to_hass(self):
|
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."""
|
2018-03-13 07:47:45 +00:00
|
|
|
await self._scene.async_set_state({})
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the scene."""
|
|
|
|
return self._scene.full_name
|