2019-02-13 21:21:14 +01:00
|
|
|
"""Support for RFXtrx lights."""
|
2015-09-27 11:13:49 +02:00
|
|
|
import logging
|
|
|
|
|
2018-01-12 20:52:53 +01:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-07-18 12:54:27 +03:00
|
|
|
from homeassistant.components import rfxtrx
|
2018-01-12 20:52:53 +01:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
Light,
|
|
|
|
)
|
2018-01-12 20:52:53 +01:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
2015-11-26 07:52:37 +01:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from . import (
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_AUTOMATIC_ADD,
|
|
|
|
CONF_DEVICES,
|
|
|
|
CONF_FIRE_EVENT,
|
|
|
|
CONF_SIGNAL_REPETITIONS,
|
|
|
|
DEFAULT_SIGNAL_REPETITIONS,
|
|
|
|
)
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2015-09-29 08:20:25 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 12:25:30 -07: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),
|
|
|
|
}
|
|
|
|
)
|
2016-04-09 05:55:31 +02:00
|
|
|
|
2016-08-16 09:07:07 +03:00
|
|
|
SUPPORT_RFXTRX = SUPPORT_BRIGHTNESS
|
|
|
|
|
2015-09-27 11:13:49 +02:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 23:10:08 -04:00
|
|
|
"""Set up the RFXtrx platform."""
|
2015-11-26 08:27:31 +01:00
|
|
|
import RFXtrx as rfxtrxmod
|
|
|
|
|
2017-08-29 16:22:28 +02:00
|
|
|
lights = rfxtrx.get_devices_from_config(config, RfxtrxLight)
|
2018-08-24 16:37:30 +02:00
|
|
|
add_entities(lights)
|
2015-09-27 11:13:49 +02:00
|
|
|
|
|
|
|
def light_update(event):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Handle light updates from the RFXtrx gateway."""
|
2019-07-31 12:25:30 -07:00
|
|
|
if (
|
|
|
|
not isinstance(event.device, rfxtrxmod.LightingDevice)
|
|
|
|
or not event.device.known_to_be_dimmable
|
|
|
|
):
|
2015-10-07 19:15:50 +02:00
|
|
|
return
|
|
|
|
|
2017-08-29 16:22:28 +02:00
|
|
|
new_device = rfxtrx.get_new_device(event, config, RfxtrxLight)
|
2016-04-06 19:34:51 +02:00
|
|
|
if new_device:
|
2018-08-24 16:37:30 +02:00
|
|
|
add_entities([new_device])
|
2015-11-26 07:52:37 +01:00
|
|
|
|
2016-04-06 19:34:51 +02:00
|
|
|
rfxtrx.apply_received_command(event)
|
2015-10-07 19:15:50 +02:00
|
|
|
|
2017-05-02 18:18:47 +02:00
|
|
|
# Subscribe to main RFXtrx events
|
2015-09-27 11:13:49 +02:00
|
|
|
if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
|
|
|
|
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update)
|
|
|
|
|
|
|
|
|
2016-04-06 19:34:51 +02:00
|
|
|
class RfxtrxLight(rfxtrx.RfxtrxDevice, Light):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Representation of a RFXtrx light."""
|
2016-03-07 22:08:21 +01:00
|
|
|
|
2016-01-23 11:50:09 +01:00
|
|
|
@property
|
2016-01-23 11:18:11 +01:00
|
|
|
def brightness(self):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2016-01-23 11:18:11 +01:00
|
|
|
return self._brightness
|
|
|
|
|
2016-08-16 09:07:07 +03:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_RFXTRX
|
|
|
|
|
2015-09-27 11:13:49 +02:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the light on."""
|
2016-01-23 11:18:11 +01:00
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
|
|
|
if brightness is None:
|
2016-03-02 20:36:41 +01:00
|
|
|
self._brightness = 255
|
2019-07-31 12:25:30 -07:00
|
|
|
self._send_command("turn_on")
|
2016-01-23 11:18:11 +01:00
|
|
|
else:
|
2016-03-02 20:36:41 +01:00
|
|
|
self._brightness = brightness
|
2019-07-31 12:25:30 -07:00
|
|
|
_brightness = brightness * 100 // 255
|
|
|
|
self._send_command("dim", _brightness)
|