2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ scenes."""
|
2018-11-05 15:21:44 +00:00
|
|
|
from homeassistant.components.deconz import DOMAIN as DECONZ_DOMAIN
|
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
|
|
|
|
|
|
|
DEPENDENCIES = ['deconz']
|
|
|
|
|
|
|
|
|
2019-02-13 20:21:14 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2018-04-23 16:00:16 +00:00
|
|
|
"""Old way of setting up deCONZ scenes."""
|
|
|
|
pass
|
|
|
|
|
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."""
|
2018-11-05 15:21:44 +00:00
|
|
|
gateway = hass.data[DECONZ_DOMAIN]
|
|
|
|
|
2018-09-04 07:24:42 +00:00
|
|
|
@callback
|
|
|
|
def async_add_scene(scenes):
|
|
|
|
"""Add scene from deCONZ."""
|
|
|
|
entities = []
|
|
|
|
for scene in scenes:
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzScene(scene, gateway))
|
2018-09-04 07:24:42 +00:00
|
|
|
async_add_entities(entities)
|
2018-11-05 15:21:44 +00:00
|
|
|
gateway.listeners.append(
|
2018-09-04 07:24:42 +00:00
|
|
|
async_dispatcher_connect(hass, 'deconz_new_scene', async_add_scene))
|
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_scene(gateway.api.scenes.values())
|
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."""
|
|
|
|
self._scene = None
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_activate(self):
|
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
|