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