2019-02-13 21:21:14 +01:00
|
|
|
"""Support for RFXtrx switches."""
|
2015-09-27 11:13:49 +02:00
|
|
|
import logging
|
2019-10-14 17:38:34 +02:00
|
|
|
|
2019-10-12 21:37:59 +02:00
|
|
|
import RFXtrx as rfxtrxmod
|
2018-01-12 20:52:53 +01:00
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
|
|
|
from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_ON
|
2020-01-02 07:59:13 +01:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2019-03-20 22:56:46 -07:00
|
|
|
|
|
|
|
from . import (
|
2020-07-10 14:52:07 +02:00
|
|
|
ATTR_FIRE_EVENT,
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_AUTOMATIC_ADD,
|
|
|
|
CONF_FIRE_EVENT,
|
|
|
|
CONF_SIGNAL_REPETITIONS,
|
|
|
|
DEFAULT_SIGNAL_REPETITIONS,
|
2020-07-12 22:03:22 +02:00
|
|
|
DOMAIN,
|
2020-07-06 00:10:26 +02:00
|
|
|
SIGNAL_EVENT,
|
2019-12-11 14:58:49 +00:00
|
|
|
RfxtrxDevice,
|
2020-07-06 00:10:26 +02:00
|
|
|
fire_command_event,
|
2020-07-10 14:52:07 +02:00
|
|
|
get_device_id,
|
|
|
|
get_rfx_object,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2020-07-06 00:10:26 +02:00
|
|
|
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
|
2015-11-26 07:52:37 +01:00
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
DATA_SWITCH = f"{DOMAIN}_switch"
|
2015-09-29 08:20:25 +02:00
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-04-09 05:55:31 +02:00
|
|
|
|
2015-09-27 11:13:49 +02:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the RFXtrx platform."""
|
2020-07-12 22:03:22 +02:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2020-07-10 14:52:07 +02:00
|
|
|
device_ids = set()
|
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
def supported(event):
|
|
|
|
return (
|
|
|
|
isinstance(event.device, rfxtrxmod.LightingDevice)
|
|
|
|
and not event.device.known_to_be_dimmable
|
|
|
|
and not event.device.known_to_be_rollershutter
|
|
|
|
)
|
|
|
|
|
2015-09-27 11:13:49 +02:00
|
|
|
# Add switch from config file
|
2020-07-10 14:52:07 +02:00
|
|
|
entities = []
|
2020-07-12 22:03:22 +02:00
|
|
|
for packet_id, entity_info in discovery_info[CONF_DEVICES].items():
|
2020-07-10 14:52:07 +02:00
|
|
|
event = get_rfx_object(packet_id)
|
|
|
|
if event is None:
|
|
|
|
_LOGGER.error("Invalid device: %s", packet_id)
|
|
|
|
continue
|
2020-07-12 22:03:22 +02:00
|
|
|
if not supported(event):
|
|
|
|
continue
|
2020-07-10 14:52:07 +02:00
|
|
|
|
|
|
|
device_id = get_device_id(event.device)
|
|
|
|
if device_id in device_ids:
|
|
|
|
continue
|
|
|
|
device_ids.add(device_id)
|
|
|
|
|
|
|
|
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]}
|
2020-07-12 22:03:22 +02:00
|
|
|
entity = RfxtrxSwitch(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS])
|
2020-07-10 14:52:07 +02:00
|
|
|
entities.append(entity)
|
|
|
|
|
|
|
|
add_entities_callback(entities)
|
2015-09-27 11:13:49 +02:00
|
|
|
|
|
|
|
def switch_update(event):
|
2017-05-02 22:47:20 +02:00
|
|
|
"""Handle sensor updates from the RFXtrx gateway."""
|
2020-07-12 22:03:22 +02:00
|
|
|
if not supported(event):
|
2015-10-07 19:15:50 +02:00
|
|
|
return
|
|
|
|
|
2020-07-10 14:52:07 +02:00
|
|
|
device_id = get_device_id(event.device)
|
|
|
|
if device_id in device_ids:
|
|
|
|
return
|
|
|
|
device_ids.add(device_id)
|
|
|
|
|
|
|
|
_LOGGER.info(
|
2020-07-12 22:03:22 +02:00
|
|
|
"Added switch (Device ID: %s Class: %s Sub: %s, Event: %s)",
|
2020-07-10 14:52:07 +02:00
|
|
|
event.device.id_string.lower(),
|
|
|
|
event.device.__class__.__name__,
|
|
|
|
event.device.subtype,
|
2020-07-12 22:03:22 +02:00
|
|
|
"".join(f"{x:02x}" for x in event.data),
|
2020-07-10 14:52:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: False}
|
|
|
|
entity = RfxtrxSwitch(
|
2020-07-12 22:03:22 +02:00
|
|
|
event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event
|
2020-07-10 14:52:07 +02:00
|
|
|
)
|
|
|
|
add_entities_callback([entity])
|
2015-11-26 07:52:37 +01:00
|
|
|
|
2017-05-02 22:47:20 +02:00
|
|
|
# Subscribe to main RFXtrx events
|
2020-07-12 22:03:22 +02:00
|
|
|
if discovery_info[CONF_AUTOMATIC_ADD]:
|
2020-07-10 14:52:07 +02:00
|
|
|
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, switch_update)
|
2015-09-27 11:13:49 +02:00
|
|
|
|
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
class RfxtrxSwitch(RfxtrxDevice, SwitchEntity, RestoreEntity):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a RFXtrx switch."""
|
|
|
|
|
2020-01-02 07:59:13 +01:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Restore RFXtrx switch device state (ON/OFF)."""
|
|
|
|
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_ON
|
|
|
|
|
2020-07-06 00:10:26 +02:00
|
|
|
self.async_on_remove(
|
|
|
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
2020-07-09 11:40:37 +02:00
|
|
|
SIGNAL_EVENT, self._handle_event
|
2020-07-06 00:10:26 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-07-09 11:40:37 +02:00
|
|
|
def _apply_event(self, event):
|
2020-07-06 00:10:26 +02: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-09 11:40:37 +02:00
|
|
|
def _handle_event(self, event):
|
|
|
|
"""Check if event applies to me and update."""
|
|
|
|
if event.device.id_string != self._device.id_string:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._apply_event(event)
|
|
|
|
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
if self.should_fire_event:
|
|
|
|
fire_command_event(self.hass, self.entity_id, event.values["Command"])
|
2020-07-06 00:10:26 +02:00
|
|
|
|
2015-09-27 11:13:49 +02:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device on."""
|
2016-04-06 19:34:51 +02:00
|
|
|
self._send_command("turn_on")
|
2020-07-09 11:40:37 +02:00
|
|
|
self.schedule_update_ha_state()
|
2020-07-06 00:10:26 +02:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the device off."""
|
|
|
|
self._send_command("turn_off")
|
2020-07-09 11:40:37 +02:00
|
|
|
self.schedule_update_ha_state()
|