2019-02-13 20:21:14 +00:00
|
|
|
"""Support for RFXtrx covers."""
|
2020-07-10 12:52:07 +00:00
|
|
|
import logging
|
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
from homeassistant.components.cover import CoverEntity
|
2020-07-13 00:57:19 +00:00
|
|
|
from homeassistant.const import CONF_DEVICES, STATE_OPEN
|
2020-07-13 21:24:28 +00:00
|
|
|
from homeassistant.core import callback
|
2020-01-02 06:59:13 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
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
|
|
|
CONF_AUTOMATIC_ADD,
|
|
|
|
CONF_SIGNAL_REPETITIONS,
|
|
|
|
DEFAULT_SIGNAL_REPETITIONS,
|
2020-07-05 22:10:26 +00:00
|
|
|
SIGNAL_EVENT,
|
2019-12-11 14:58:49 +00:00
|
|
|
RfxtrxDevice,
|
2020-07-10 12:52:07 +00:00
|
|
|
get_device_id,
|
|
|
|
get_rfx_object,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-07-13 21:24:28 +00:00
|
|
|
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DATA_RFXTRX_CONFIG
|
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
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass, config_entry, async_add_entities,
|
|
|
|
):
|
|
|
|
"""Set up config entry."""
|
|
|
|
discovery_info = hass.data[DATA_RFXTRX_CONFIG]
|
2020-07-10 12:52:07 +00:00
|
|
|
device_ids = set()
|
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
def supported(event):
|
|
|
|
return event.device.known_to_be_rollershutter
|
|
|
|
|
2020-07-10 12:52:07 +00:00
|
|
|
entities = []
|
2020-07-12 20:03:22 +00:00
|
|
|
for packet_id, entity_info in discovery_info[CONF_DEVICES].items():
|
2020-07-10 12:52:07 +00:00
|
|
|
event = get_rfx_object(packet_id)
|
|
|
|
if event is None:
|
|
|
|
_LOGGER.error("Invalid device: %s", packet_id)
|
|
|
|
continue
|
2020-07-12 20:03:22 +00:00
|
|
|
if not supported(event):
|
|
|
|
continue
|
2020-07-10 12:52:07 +00:00
|
|
|
|
|
|
|
device_id = get_device_id(event.device)
|
|
|
|
if device_id in device_ids:
|
|
|
|
continue
|
|
|
|
device_ids.add(device_id)
|
|
|
|
|
2020-07-13 00:57:19 +00:00
|
|
|
entity = RfxtrxCover(
|
|
|
|
event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS]
|
|
|
|
)
|
2020-07-10 12:52:07 +00:00
|
|
|
entities.append(entity)
|
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
async_add_entities(entities)
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
@callback
|
2020-07-13 00:57:19 +00:00
|
|
|
def cover_update(event, device_id):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Handle cover updates from the RFXtrx gateway."""
|
2020-07-12 20:03:22 +00:00
|
|
|
if not supported(event):
|
2016-08-24 01:23:18 +00:00
|
|
|
return
|
|
|
|
|
2020-07-10 12:52:07 +00:00
|
|
|
if device_id in device_ids:
|
|
|
|
return
|
|
|
|
device_ids.add(device_id)
|
|
|
|
|
|
|
|
_LOGGER.info(
|
2020-07-12 20:03:22 +00:00
|
|
|
"Added cover (Device ID: %s Class: %s Sub: %s, Event: %s)",
|
2020-07-10 12:52:07 +00:00
|
|
|
event.device.id_string.lower(),
|
|
|
|
event.device.__class__.__name__,
|
|
|
|
event.device.subtype,
|
2020-07-12 20:03:22 +00:00
|
|
|
"".join(f"{x:02x}" for x in event.data),
|
2020-07-10 12:52:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
entity = RfxtrxCover(
|
2020-07-13 00:57:19 +00:00
|
|
|
event.device, device_id, DEFAULT_SIGNAL_REPETITIONS, event=event
|
2020-07-10 12:52:07 +00:00
|
|
|
)
|
2020-07-13 21:24:28 +00:00
|
|
|
async_add_entities([entity])
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
# Subscribe to main RFXtrx events
|
2020-07-12 20:03:22 +00:00
|
|
|
if discovery_info[CONF_AUTOMATIC_ADD]:
|
2020-07-13 21:24:28 +00:00
|
|
|
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, cover_update)
|
2016-08-24 01:23:18 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:07:15 +00:00
|
|
|
class RfxtrxCover(RfxtrxDevice, CoverEntity, RestoreEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a RFXtrx cover."""
|
2016-08-24 01:23:18 +00:00
|
|
|
|
2020-01-02 06:59:13 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Restore RFXtrx cover device state (OPEN/CLOSE)."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
old_state = await self.async_get_last_state()
|
|
|
|
if old_state is not None:
|
|
|
|
self._state = old_state.state == STATE_OPEN
|
|
|
|
|
2020-07-05 22:10:26 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
2020-07-09 09:40:37 +00:00
|
|
|
SIGNAL_EVENT, self._handle_event
|
2020-07-05 22:10:26 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-08-24 01:23:18 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return the polling state. No polling available in RFXtrx cover."""
|
2016-08-24 01:23:18 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
def open_cover(self, **kwargs):
|
|
|
|
"""Move the cover up."""
|
|
|
|
self._send_command("roll_up")
|
|
|
|
|
|
|
|
def close_cover(self, **kwargs):
|
|
|
|
"""Move the cover down."""
|
|
|
|
self._send_command("roll_down")
|
|
|
|
|
|
|
|
def stop_cover(self, **kwargs):
|
|
|
|
"""Stop the cover."""
|
|
|
|
self._send_command("stop_roll")
|
2020-07-05 22:10:26 +00:00
|
|
|
|
2020-07-09 09:40:37 +00:00
|
|
|
def _apply_event(self, event):
|
2020-07-05 22:10:26 +00:00
|
|
|
"""Apply command from rfxtrx."""
|
|
|
|
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
|
2020-07-13 00:57:19 +00:00
|
|
|
def _handle_event(self, event, device_id):
|
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()
|