core/homeassistant/components/rfxtrx/switch.py

71 lines
2.1 KiB
Python
Raw Normal View History

"""Support for RFXtrx switches."""
2015-09-27 09:13:49 +00:00
import logging
import RFXtrx as rfxtrxmod
import voluptuous as vol
from homeassistant.components import rfxtrx
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
2018-05-14 11:05:52 +00:00
from homeassistant.const import CONF_NAME
from homeassistant.helpers import config_validation as cv
from . import (
2019-07-31 19:25:30 +00:00
CONF_AUTOMATIC_ADD,
CONF_DEVICES,
CONF_FIRE_EVENT,
CONF_SIGNAL_REPETITIONS,
DEFAULT_SIGNAL_REPETITIONS,
)
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_DEVICES, default={}): {
cv.string: vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean,
}
)
},
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
vol.Optional(
CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS
): vol.Coerce(int),
}
)
2015-09-27 09:13:49 +00:00
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
"""Set up the RFXtrx platform."""
2015-09-27 09:13:49 +00:00
# Add switch from config file
switches = rfxtrx.get_devices_from_config(config, RfxtrxSwitch)
add_entities_callback(switches)
2015-09-27 09:13:49 +00:00
def switch_update(event):
"""Handle sensor updates from the RFXtrx gateway."""
2019-07-31 19:25:30 +00:00
if (
not isinstance(event.device, rfxtrxmod.LightingDevice)
or event.device.known_to_be_dimmable
or event.device.known_to_be_rollershutter
):
2015-10-07 17:15:50 +00:00
return
new_device = rfxtrx.get_new_device(event, config, RfxtrxSwitch)
2016-04-06 17:34:51 +00:00
if new_device:
add_entities_callback([new_device])
2016-04-06 17:34:51 +00:00
rfxtrx.apply_received_command(event)
2015-10-07 17:15:50 +00:00
# Subscribe to main RFXtrx events
2015-09-27 09:13:49 +00:00
if switch_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(switch_update)
2016-04-06 17:34:51 +00:00
class RfxtrxSwitch(rfxtrx.RfxtrxDevice, SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a RFXtrx switch."""
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")