2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ covers."""
|
2018-09-21 17:59:20 +00:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_POSITION,
|
|
|
|
SUPPORT_CLOSE,
|
|
|
|
SUPPORT_OPEN,
|
|
|
|
SUPPORT_SET_POSITION,
|
2019-12-05 05:17:18 +00:00
|
|
|
SUPPORT_STOP,
|
|
|
|
CoverDevice,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-21 17:59:20 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .const import COVER_TYPES, DAMPERS, NEW_LIGHT, WINDOW_COVERS
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-04-05 00:48:24 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-27 04:56:00 +00:00
|
|
|
"""Old way of setting up deCONZ platforms."""
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up covers for deCONZ component.
|
|
|
|
|
|
|
|
Covers are based on same device class as lights in deCONZ.
|
|
|
|
"""
|
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-21 17:59:20 +00:00
|
|
|
@callback
|
|
|
|
def async_add_cover(lights):
|
|
|
|
"""Add cover from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-09-21 17:59:20 +00:00
|
|
|
for light in lights:
|
|
|
|
if light.type in COVER_TYPES:
|
2019-08-27 19:06:14 +00:00
|
|
|
entities.append(DeconzCover(light, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-09-21 17:59:20 +00:00
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
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_LIGHT), async_add_cover
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_cover(gateway.api.lights.values())
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzCover(DeconzDevice, CoverDevice):
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Representation of a deCONZ cover."""
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
def __init__(self, device, gateway):
|
2019-02-18 16:43:22 +00:00
|
|
|
"""Set up cover device."""
|
2019-01-16 07:33:04 +00:00
|
|
|
super().__init__(device, gateway)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-10-20 13:13:23 +00:00
|
|
|
self._features = SUPPORT_OPEN
|
|
|
|
self._features |= SUPPORT_CLOSE
|
|
|
|
self._features |= SUPPORT_STOP
|
|
|
|
self._features |= SUPPORT_SET_POSITION
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_cover_position(self):
|
|
|
|
"""Return the current position of the cover."""
|
2019-08-27 19:06:14 +00:00
|
|
|
return 100 - int(self._device.brightness / 255 * 100)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
2019-08-27 19:06:14 +00:00
|
|
|
return self._device.state
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of the cover."""
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.type in DAMPERS:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "damper"
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.type in WINDOW_COVERS:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "window"
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._features
|
|
|
|
|
|
|
|
async def async_set_cover_position(self, **kwargs):
|
|
|
|
"""Move the cover to a specific position."""
|
|
|
|
position = kwargs[ATTR_POSITION]
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": False}
|
2019-05-03 15:55:42 +00:00
|
|
|
|
2019-08-27 19:06:14 +00:00
|
|
|
if position < 100:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["on"] = True
|
2019-08-27 19:06:14 +00:00
|
|
|
data["bri"] = 255 - int(position / 100 * 255)
|
2019-05-03 15:55:42 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-09-21 17:59:20 +00:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs):
|
|
|
|
"""Open cover."""
|
|
|
|
data = {ATTR_POSITION: 100}
|
|
|
|
await self.async_set_cover_position(**data)
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs):
|
|
|
|
"""Close cover."""
|
|
|
|
data = {ATTR_POSITION: 0}
|
|
|
|
await self.async_set_cover_position(**data)
|
|
|
|
|
2018-10-20 13:13:23 +00:00
|
|
|
async def async_stop_cover(self, **kwargs):
|
|
|
|
"""Stop cover."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"bri_inc": 0}
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|