core/homeassistant/components/light/rfxtrx.py

191 lines
6.6 KiB
Python
Raw Normal View History

2015-09-27 09:13:49 +00:00
"""
2015-10-08 09:08:17 +00:00
Support for RFXtrx lights.
2015-09-27 09:13:49 +00:00
2015-10-08 09:08:17 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/light.rfxtrx/
2015-09-27 09:13:49 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
import homeassistant.components.rfxtrx as rfxtrx
from homeassistant.components.light import ATTR_BRIGHTNESS, Light
2016-01-13 05:53:27 +00:00
from homeassistant.components.rfxtrx import (
2016-02-19 05:27:50 +00:00
ATTR_FIREEVENT, ATTR_NAME, ATTR_PACKETID, ATTR_STATE, EVENT_BUTTON_PRESSED)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.util import slugify
DEPENDENCIES = ['rfxtrx']
2016-02-15 18:41:40 +00:00
SIGNAL_REPETITIONS = 1
2015-09-27 09:13:49 +00:00
_LOGGER = logging.getLogger(__name__)
2015-09-27 09:13:49 +00:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-07 21:08:21 +00:00
"""Setup the RFXtrx platform."""
2015-11-26 07:27:31 +00:00
import RFXtrx as rfxtrxmod
2015-09-27 21:51:19 +00:00
lights = []
2016-02-15 18:41:40 +00:00
signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
2016-02-23 17:01:53 +00:00
for device_id, entity_info in config.get('devices', {}).items():
2016-02-25 16:40:24 +00:00
if device_id in rfxtrx.RFX_DEVICES:
continue
_LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME])
# Check if i must fire event
fire_event = entity_info.get(ATTR_FIREEVENT, False)
datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}
rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
new_light = RfxtrxLight(
entity_info[ATTR_NAME], rfxobject, datas,
signal_repetitions)
rfxtrx.RFX_DEVICES[device_id] = new_light
lights.append(new_light)
2015-09-27 09:13:49 +00:00
2015-09-27 21:51:19 +00:00
add_devices_callback(lights)
2015-09-27 09:13:49 +00:00
def light_update(event):
2016-03-07 21:08:21 +00:00
"""Callback for light updates from the RFXtrx gateway."""
2016-02-11 13:15:51 +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
# Add entity if not exist and the automatic_add is True
2016-02-23 17:01:53 +00:00
device_id = slugify(event.device.id_string.lower())
if device_id not in rfxtrx.RFX_DEVICES:
2015-10-07 17:15:50 +00:00
automatic_add = config.get('automatic_add', False)
2015-10-07 17:57:40 +00:00
if not automatic_add:
return
_LOGGER.info(
"Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
2016-02-23 17:01:53 +00:00
device_id,
2015-10-07 17:57:40 +00:00
event.device.__class__.__name__,
event.device.subtype
)
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
2016-02-23 17:01:53 +00:00
entity_name = "%s : %s" % (device_id, pkt_id)
datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
2016-02-15 18:45:33 +00:00
signal_repetitions = config.get('signal_repetitions',
SIGNAL_REPETITIONS)
new_light = RfxtrxLight(entity_name, event, datas,
signal_repetitions)
2016-02-23 17:01:53 +00:00
rfxtrx.RFX_DEVICES[device_id] = new_light
2015-10-07 17:57:40 +00:00
add_devices_callback([new_light])
# Check if entity exists or previously added automatically
2016-02-23 17:01:53 +00:00
if device_id in rfxtrx.RFX_DEVICES:
2015-11-08 18:02:51 +00:00
_LOGGER.debug(
2015-11-08 10:04:29 +00:00
"EntityID: %s light_update. Command: %s",
2016-02-23 17:01:53 +00:00
device_id,
2015-11-08 10:04:29 +00:00
event.values['Command']
)
2015-10-07 17:15:50 +00:00
if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off':
# Update the rfxtrx device state
is_on = event.values['Command'] == 'On'
# pylint: disable=protected-access
2016-02-23 17:01:53 +00:00
rfxtrx.RFX_DEVICES[device_id]._state = is_on
rfxtrx.RFX_DEVICES[device_id].update_ha_state()
elif event.values['Command'] == 'Set level':
# pylint: disable=protected-access
2016-02-23 17:01:53 +00:00
rfxtrx.RFX_DEVICES[device_id]._brightness = \
(event.values['Dim level'] * 255 // 100)
# Update the rfxtrx device state
2016-02-23 17:01:53 +00:00
is_on = rfxtrx.RFX_DEVICES[device_id]._brightness > 0
rfxtrx.RFX_DEVICES[device_id]._state = is_on
rfxtrx.RFX_DEVICES[device_id].update_ha_state()
else:
return
# Fire event
2016-02-23 17:01:53 +00:00
if rfxtrx.RFX_DEVICES[device_id].should_fire_event:
rfxtrx.RFX_DEVICES[device_id].hass.bus.fire(
EVENT_BUTTON_PRESSED, {
ATTR_ENTITY_ID:
rfxtrx.RFX_DEVICES[device_id].entity_id,
ATTR_STATE: event.values['Command'].lower()
}
)
2015-10-07 17:15:50 +00:00
# Subscribe to main rfxtrx events
2015-09-27 09:13:49 +00:00
if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update)
class RfxtrxLight(Light):
2016-03-07 21:08:21 +00:00
"""Represenation of a RFXtrx light."""
2016-02-15 18:41:40 +00:00
def __init__(self, name, event, datas, signal_repetitions):
2016-03-07 21:08:21 +00:00
"""Initialize the light."""
2015-09-27 09:13:49 +00:00
self._name = name
2015-09-29 20:47:22 +00:00
self._event = event
self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIREEVENT]
2016-02-15 18:41:40 +00:00
self.signal_repetitions = signal_repetitions
self._brightness = 0
2015-09-27 09:13:49 +00:00
@property
def should_poll(self):
2016-03-07 21:08:21 +00:00
"""No polling needed for a light."""
2015-09-27 09:13:49 +00:00
return False
@property
def name(self):
2016-03-07 21:08:21 +00:00
"""Return the name of the light if any."""
2015-09-27 09:13:49 +00:00
return self._name
@property
def should_fire_event(self):
2016-03-07 21:08:21 +00:00
"""Return true if the device must fire event."""
return self._should_fire_event
2015-09-27 09:13:49 +00:00
@property
def is_on(self):
2016-03-07 21:08:21 +00:00
"""Return true if light is on."""
2015-09-27 09:13:49 +00:00
return self._state
2016-01-23 10:50:09 +00:00
@property
def brightness(self):
2016-03-07 21:08:21 +00:00
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def assumed_state(self):
"""Return True if unable to access real state of entity."""
return True
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."""
brightness = kwargs.get(ATTR_BRIGHTNESS)
2016-02-15 09:05:47 +00:00
if not self._event:
return
if brightness is None:
self._brightness = 255
2016-02-15 18:41:40 +00:00
for _ in range(self.signal_repetitions):
2016-02-15 18:52:39 +00:00
self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
else:
self._brightness = brightness
_brightness = (brightness * 100 // 255)
2016-02-15 18:41:40 +00:00
for _ in range(self.signal_repetitions):
self._event.device.send_dim(rfxtrx.RFXOBJECT.transport,
_brightness)
2015-09-27 09:13:49 +00:00
self._state = True
self.update_ha_state()
def turn_off(self, **kwargs):
2016-03-07 21:08:21 +00:00
"""Turn the light off."""
2016-02-15 18:41:40 +00:00
if not self._event:
return
for _ in range(self.signal_repetitions):
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
2016-01-23 11:08:21 +00:00
self._brightness = 0
2015-09-27 09:13:49 +00:00
self._state = False
2015-11-26 07:12:04 +00:00
self.update_ha_state()