hass-core/homeassistant/components/light/rfxtrx.py

170 lines
5.9 KiB
Python
Raw Normal View History

2015-09-27 11:13:49 +02:00
"""
homeassistant.components.light.rfxtrx
2015-10-08 11:08:17 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for RFXtrx lights.
2015-09-27 11:13:49 +02:00
2015-10-08 11:08:17 +02:00
For more details about this platform, please refer to the documentation at
2015-11-09 13:12:18 +01:00
https://home-assistant.io/components/light.rfxtrx/
2015-09-27 11:13:49 +02:00
"""
import logging
import homeassistant.components.rfxtrx as rfxtrx
from homeassistant.components.light import Light, ATTR_BRIGHTNESS
2015-09-27 11:13:49 +02:00
from homeassistant.util import slugify
from homeassistant.const import ATTR_ENTITY_ID
2016-01-12 21:53:27 -08:00
from homeassistant.components.rfxtrx import (
ATTR_STATE, ATTR_FIREEVENT, ATTR_PACKETID,
ATTR_NAME, EVENT_BUTTON_PRESSED)
DEPENDENCIES = ['rfxtrx']
2015-09-27 11:13:49 +02:00
_LOGGER = logging.getLogger(__name__)
2015-09-27 11:13:49 +02:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Setup the RFXtrx platform. """
2015-11-26 08:27:31 +01:00
import RFXtrx as rfxtrxmod
2015-09-27 23:51:19 +02:00
lights = []
devices = config.get('devices', None)
if devices:
for entity_id, entity_info in devices.items():
if entity_id not in rfxtrx.RFX_DEVICES:
_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
)
rfxtrx.RFX_DEVICES[entity_id] = new_light
lights.append(new_light)
2015-09-27 11:13:49 +02:00
2015-09-27 23:51:19 +02:00
add_devices_callback(lights)
2015-09-27 11:13:49 +02:00
def light_update(event):
2015-10-08 11:08:17 +02:00
""" Callback for light updates from the RFXtrx gateway. """
2015-10-07 19:15:50 +02:00
if not isinstance(event.device, rfxtrxmod.LightingDevice):
return
# Add entity if not exist and the automatic_add is True
entity_id = slugify(event.device.id_string.lower())
if entity_id not in rfxtrx.RFX_DEVICES:
automatic_add = config.get('automatic_add', False)
2015-10-07 19:57:40 +02:00
if not automatic_add:
return
_LOGGER.info(
"Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
entity_id,
event.device.__class__.__name__,
event.device.subtype
)
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id)
datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
new_light = RfxtrxLight(entity_name, event, datas)
2015-10-07 19:57:40 +02:00
rfxtrx.RFX_DEVICES[entity_id] = new_light
add_devices_callback([new_light])
# Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES \
and isinstance(rfxtrx.RFX_DEVICES[entity_id], RfxtrxLight):
2015-11-08 19:02:51 +01:00
_LOGGER.debug(
2015-11-08 11:04:29 +01:00
"EntityID: %s light_update. Command: %s",
entity_id,
event.values['Command']
)
2015-10-07 19:15:50 +02: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
rfxtrx.RFX_DEVICES[entity_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
# Fire event
if rfxtrx.RFX_DEVICES[entity_id].should_fire_event:
rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire(
EVENT_BUTTON_PRESSED, {
ATTR_ENTITY_ID:
rfxtrx.RFX_DEVICES[entity_id].entity_id,
ATTR_STATE: event.values['Command'].lower()
}
)
2015-10-07 19:15:50 +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)
class RfxtrxLight(Light):
2015-10-08 11:08:17 +02:00
""" Provides a RFXtrx light. """
def __init__(self, name, event, datas):
2015-09-27 11:13:49 +02:00
self._name = name
2015-09-29 22:47:22 +02:00
self._event = event
self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIREEVENT]
self._brightness = 0
2015-09-27 11:13:49 +02:00
@property
def should_poll(self):
2015-10-08 11:08:17 +02:00
""" No polling needed for a light. """
2015-09-27 11:13:49 +02:00
return False
@property
def name(self):
2015-10-08 11:08:17 +02:00
""" Returns the name of the light if any. """
2015-09-27 11:13:49 +02:00
return self._name
@property
def should_fire_event(self):
""" Returns is the device must fire event"""
return self._should_fire_event
2015-09-27 11:13:49 +02:00
@property
def is_on(self):
2015-10-08 11:08:17 +02:00
""" True if light is on. """
2015-09-27 11:13:49 +02:00
return self._state
@property
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
2015-09-27 11:13:49 +02:00
def turn_on(self, **kwargs):
2015-10-08 11:08:17 +02:00
""" Turn the light on. """
brightness = kwargs.get(ATTR_BRIGHTNESS)
if brightness is None:
""" Brightness in rfxtrx is defined as level and values supported are 0-100 """
self._brightness = 100
else:
""" Brightness in rfxtrx is defined as level and values supported are 0-100 so we need to scale the set value (0-255)"""
self._brightness = ((brightness + 4) * 100 // 255 -1)
2015-09-29 22:47:22 +02:00
2015-10-07 19:57:40 +02:00
if hasattr(self, '_event') and self._event:
self._event.device.send_on(rfxtrx.RFXOBJECT.transport, self._brightness)
2015-09-29 22:47:22 +02:00
""" Reverse earlier calculation to make dimmer slider stay at correct point in HA frontend """
self._brightness = (self._brightness * 255 // 100)
2015-09-27 11:13:49 +02:00
self._state = True
self.update_ha_state()
def turn_off(self, **kwargs):
2015-10-08 11:08:17 +02:00
""" Turn the light off. """
2015-09-29 22:47:22 +02:00
2015-10-07 19:57:40 +02:00
if hasattr(self, '_event') and self._event:
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
self._brightness = 0
2015-09-27 11:13:49 +02:00
self._state = False
2015-11-26 08:12:04 +01:00
self.update_ha_state()