core/homeassistant/components/rfxtrx/switch.py

135 lines
3.8 KiB
Python
Raw Normal View History

"""Support for RFXtrx switches."""
2015-09-27 09:13:49 +00:00
import logging
import RFXtrx as rfxtrxmod
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import CONF_DEVICES, STATE_ON
from homeassistant.core import callback
from homeassistant.helpers.restore_state import RestoreEntity
from . import (
2019-07-31 19:25:30 +00:00
CONF_AUTOMATIC_ADD,
CONF_SIGNAL_REPETITIONS,
DEFAULT_SIGNAL_REPETITIONS,
DOMAIN,
SIGNAL_EVENT,
RfxtrxDevice,
get_device_id,
get_rfx_object,
2019-07-31 19:25:30 +00:00
)
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DATA_RFXTRX_CONFIG
DATA_SWITCH = f"{DOMAIN}_switch"
_LOGGER = logging.getLogger(__name__)
2015-09-27 09:13:49 +00:00
async def async_setup_entry(
hass, config_entry, async_add_entities,
):
"""Set up config entry."""
discovery_info = hass.data[DATA_RFXTRX_CONFIG]
device_ids = set()
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 09:13:49 +00:00
# Add switch from config file
entities = []
for packet_id, entity_info in discovery_info[CONF_DEVICES].items():
event = get_rfx_object(packet_id)
if event is None:
_LOGGER.error("Invalid device: %s", packet_id)
continue
if not supported(event):
continue
device_id = get_device_id(event.device)
if device_id in device_ids:
continue
device_ids.add(device_id)
entity = RfxtrxSwitch(
event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS]
)
entities.append(entity)
async_add_entities(entities)
2015-09-27 09:13:49 +00:00
@callback
def switch_update(event, device_id):
"""Handle sensor updates from the RFXtrx gateway."""
if not supported(event):
2015-10-07 17:15:50 +00:00
return
if device_id in device_ids:
return
device_ids.add(device_id)
_LOGGER.info(
"Added switch (Device ID: %s Class: %s Sub: %s, Event: %s)",
event.device.id_string.lower(),
event.device.__class__.__name__,
event.device.subtype,
"".join(f"{x:02x}" for x in event.data),
)
entity = RfxtrxSwitch(
event.device, device_id, DEFAULT_SIGNAL_REPETITIONS, event=event
)
async_add_entities([entity])
# Subscribe to main RFXtrx events
if discovery_info[CONF_AUTOMATIC_ADD]:
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, switch_update)
2015-09-27 09:13:49 +00:00
class RfxtrxSwitch(RfxtrxDevice, SwitchEntity, RestoreEntity):
2016-03-08 12:35:39 +00:00
"""Representation of a RFXtrx switch."""
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
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_EVENT, self._handle_event
)
)
def _apply_event(self, event):
"""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
@callback
def _handle_event(self, event, device_id):
"""Check if event applies to me and update."""
if device_id != self._device_id:
return
self._apply_event(event)
self.async_write_ha_state()
2015-09-27 09:13:49 +00:00
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
2016-04-06 17:34:51 +00:00
self._send_command("turn_on")
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn the device off."""
self._send_command("turn_off")
self.schedule_update_ha_state()