2019-02-13 20:21:14 +00:00
|
|
|
"""Support for RFXtrx covers."""
|
2021-12-09 21:35:53 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-07-10 12:52:07 +00:00
|
|
|
import logging
|
|
|
|
|
2021-12-09 21:35:53 +00:00
|
|
|
import RFXtrx as rfxtrxmod
|
|
|
|
|
2021-01-03 11:59:22 +00:00
|
|
|
from homeassistant.components.cover import (
|
|
|
|
SUPPORT_CLOSE,
|
|
|
|
SUPPORT_CLOSE_TILT,
|
|
|
|
SUPPORT_OPEN,
|
|
|
|
SUPPORT_OPEN_TILT,
|
|
|
|
SUPPORT_STOP,
|
|
|
|
SUPPORT_STOP_TILT,
|
|
|
|
CoverEntity,
|
|
|
|
)
|
2021-12-22 21:38:55 +00:00
|
|
|
from homeassistant.const import STATE_OPEN
|
2020-07-13 21:24:28 +00:00
|
|
|
from homeassistant.core import callback
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_SIGNAL_REPETITIONS,
|
2021-12-09 21:35:53 +00:00
|
|
|
DeviceTuple,
|
2020-07-18 11:43:38 +00:00
|
|
|
RfxtrxCommandEntity,
|
2021-12-22 21:38:55 +00:00
|
|
|
async_setup_platform_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-01-03 11:59:22 +00:00
|
|
|
from .const import (
|
|
|
|
COMMAND_OFF_LIST,
|
|
|
|
COMMAND_ON_LIST,
|
2021-09-09 05:17:02 +00:00
|
|
|
CONF_SIGNAL_REPETITIONS,
|
2021-01-03 11:59:22 +00:00
|
|
|
CONF_VENETIAN_BLIND_MODE,
|
|
|
|
CONST_VENETIAN_BLIND_MODE_EU,
|
|
|
|
CONST_VENETIAN_BLIND_MODE_US,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-07-10 12:52:07 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2021-12-09 21:35:53 +00:00
|
|
|
def supported(event: rfxtrxmod.RFXtrxEvent):
|
2020-10-01 06:55:57 +00:00
|
|
|
"""Return whether an event supports cover."""
|
|
|
|
return event.device.known_to_be_rollershutter
|
|
|
|
|
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
async def async_setup_entry(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
2020-07-13 21:24:28 +00:00
|
|
|
):
|
|
|
|
"""Set up config entry."""
|
2020-07-10 12:52:07 +00:00
|
|
|
|
2021-12-22 21:38:55 +00:00
|
|
|
def _constructor(
|
|
|
|
event: rfxtrxmod.RFXtrxEvent,
|
|
|
|
auto: bool,
|
|
|
|
device_id: DeviceTuple,
|
|
|
|
entity_info: dict,
|
|
|
|
):
|
|
|
|
return [
|
|
|
|
RfxtrxCover(
|
|
|
|
event.device,
|
|
|
|
device_id,
|
|
|
|
entity_info.get(CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS),
|
|
|
|
venetian_blind_mode=entity_info.get(CONF_VENETIAN_BLIND_MODE),
|
|
|
|
event=event if auto else None,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
await async_setup_platform_entry(
|
|
|
|
hass, config_entry, async_add_entities, supported, _constructor
|
|
|
|
)
|
2016-08-24 01:23:18 +00:00
|
|
|
|
|
|
|
|
2020-07-18 11:43:38 +00:00
|
|
|
class RfxtrxCover(RfxtrxCommandEntity, CoverEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a RFXtrx cover."""
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2021-12-09 21:35:53 +00:00
|
|
|
_device: rfxtrxmod.RollerTrolDevice | rfxtrxmod.RfyDevice | rfxtrxmod.LightingDevice
|
|
|
|
|
2021-01-03 11:59:22 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-12-09 21:35:53 +00:00
|
|
|
device: rfxtrxmod.RFXtrxDevice,
|
|
|
|
device_id: DeviceTuple,
|
|
|
|
signal_repetitions: int,
|
|
|
|
event: rfxtrxmod.RFXtrxEvent = None,
|
|
|
|
venetian_blind_mode: bool | None = None,
|
|
|
|
) -> None:
|
2021-01-03 11:59:22 +00:00
|
|
|
"""Initialize the RFXtrx cover device."""
|
|
|
|
super().__init__(device, device_id, signal_repetitions, event)
|
|
|
|
self._venetian_blind_mode = venetian_blind_mode
|
|
|
|
|
2020-07-21 22:01:31 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Restore device state."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
if self._event is None:
|
|
|
|
old_state = await self.async_get_last_state()
|
|
|
|
if old_state is not None:
|
|
|
|
self._state = old_state.state == STATE_OPEN
|
|
|
|
|
2021-01-03 11:59:22 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
|
|
|
|
|
|
|
|
if self._venetian_blind_mode in (
|
|
|
|
CONST_VENETIAN_BLIND_MODE_US,
|
|
|
|
CONST_VENETIAN_BLIND_MODE_EU,
|
|
|
|
):
|
|
|
|
supported_features |= (
|
|
|
|
SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT | SUPPORT_STOP_TILT
|
|
|
|
)
|
|
|
|
|
|
|
|
return supported_features
|
|
|
|
|
2016-08-24 01:23:18 +00:00
|
|
|
@property
|
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
2020-04-16 13:07:55 +00:00
|
|
|
return not self._state
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2020-07-27 22:44:30 +00:00
|
|
|
async def async_open_cover(self, **kwargs):
|
2016-08-24 01:23:18 +00:00
|
|
|
"""Move the cover up."""
|
2021-01-03 11:59:22 +00:00
|
|
|
if self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
|
|
|
|
await self._async_send(self._device.send_up05sec)
|
|
|
|
elif self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
|
|
|
|
await self._async_send(self._device.send_up2sec)
|
|
|
|
else:
|
|
|
|
await self._async_send(self._device.send_open)
|
2020-07-27 22:44:30 +00:00
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2020-07-27 22:44:30 +00:00
|
|
|
async def async_close_cover(self, **kwargs):
|
2016-08-24 01:23:18 +00:00
|
|
|
"""Move the cover down."""
|
2021-01-03 11:59:22 +00:00
|
|
|
if self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
|
|
|
|
await self._async_send(self._device.send_down05sec)
|
|
|
|
elif self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
|
|
|
|
await self._async_send(self._device.send_down2sec)
|
|
|
|
else:
|
|
|
|
await self._async_send(self._device.send_close)
|
2020-07-27 22:44:30 +00:00
|
|
|
self._state = False
|
|
|
|
self.async_write_ha_state()
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2020-07-27 22:44:30 +00:00
|
|
|
async def async_stop_cover(self, **kwargs):
|
2016-08-24 01:23:18 +00:00
|
|
|
"""Stop the cover."""
|
2020-07-27 22:44:30 +00:00
|
|
|
await self._async_send(self._device.send_stop)
|
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
2020-07-05 22:10:26 +00:00
|
|
|
|
2021-01-03 11:59:22 +00:00
|
|
|
async def async_open_cover_tilt(self, **kwargs):
|
|
|
|
"""Tilt the cover up."""
|
|
|
|
if self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
|
|
|
|
await self._async_send(self._device.send_up2sec)
|
|
|
|
elif self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
|
|
|
|
await self._async_send(self._device.send_up05sec)
|
|
|
|
|
|
|
|
async def async_close_cover_tilt(self, **kwargs):
|
|
|
|
"""Tilt the cover down."""
|
|
|
|
if self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
|
|
|
|
await self._async_send(self._device.send_down2sec)
|
|
|
|
elif self._venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
|
|
|
|
await self._async_send(self._device.send_down05sec)
|
|
|
|
|
|
|
|
async def async_stop_cover_tilt(self, **kwargs):
|
|
|
|
"""Stop the cover tilt."""
|
|
|
|
await self._async_send(self._device.send_stop)
|
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-12-09 21:35:53 +00:00
|
|
|
def _apply_event(self, event: rfxtrxmod.RFXtrxEvent):
|
2020-07-05 22:10:26 +00:00
|
|
|
"""Apply command from rfxtrx."""
|
2021-12-09 21:35:53 +00:00
|
|
|
assert isinstance(event, rfxtrxmod.ControlEvent)
|
2020-07-17 08:27:07 +00:00
|
|
|
super()._apply_event(event)
|
2020-07-05 22:10:26 +00:00
|
|
|
if event.values["Command"] in COMMAND_ON_LIST:
|
|
|
|
self._state = True
|
|
|
|
elif event.values["Command"] in COMMAND_OFF_LIST:
|
|
|
|
self._state = False
|
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
@callback
|
2021-12-09 21:35:53 +00:00
|
|
|
def _handle_event(self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple):
|
2020-07-09 09:40:37 +00:00
|
|
|
"""Check if event applies to me and update."""
|
2020-07-13 00:57:19 +00:00
|
|
|
if device_id != self._device_id:
|
2020-07-09 09:40:37 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self._apply_event(event)
|
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
self.async_write_ha_state()
|