2015-09-27 09:13:49 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.light.rfxtrx
|
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
|
|
|
|
import homeassistant.components.rfxtrx as rfxtrx
|
|
|
|
|
|
|
|
from homeassistant.components.light import Light
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
2015-10-07 17:04:03 +00:00
|
|
|
DEPENDENCIES = ['rfxtrx']
|
2015-09-27 09:13:49 +00:00
|
|
|
|
2015-09-29 06:20:25 +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):
|
|
|
|
""" Setup the RFXtrx platform. """
|
2015-11-17 08:18:42 +00:00
|
|
|
import RFXtrx as rfxtrxmod
|
|
|
|
|
2015-09-27 21:51:19 +00:00
|
|
|
lights = []
|
2015-10-02 20:39:30 +00:00
|
|
|
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['name'])
|
2015-10-06 06:44:15 +00:00
|
|
|
rfxobject = rfxtrx.get_rfx_object(entity_info['packetid'])
|
2015-10-02 20:39:30 +00:00
|
|
|
new_light = RfxtrxLight(entity_info['name'], rfxobject, False)
|
|
|
|
rfxtrx.RFX_DEVICES[entity_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):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" Callback for light updates from the RFXtrx gateway. """
|
2015-10-07 17:15:50 +00: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 17:57:40 +00: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)
|
|
|
|
new_light = RfxtrxLight(entity_name, event, False)
|
|
|
|
rfxtrx.RFX_DEVICES[entity_id] = new_light
|
|
|
|
add_devices_callback([new_light])
|
|
|
|
|
|
|
|
# Check if entity exists or previously added automatically
|
2015-10-07 17:15:50 +00:00
|
|
|
if entity_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",
|
|
|
|
entity_id,
|
|
|
|
event.values['Command']
|
|
|
|
)
|
2015-10-07 17:15:50 +00:00
|
|
|
if event.values['Command'] == 'On'\
|
|
|
|
or event.values['Command'] == 'Off':
|
|
|
|
if event.values['Command'] == 'On':
|
|
|
|
rfxtrx.RFX_DEVICES[entity_id].turn_on()
|
|
|
|
else:
|
|
|
|
rfxtrx.RFX_DEVICES[entity_id].turn_off()
|
|
|
|
|
|
|
|
# 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):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" Provides a RFXtrx light. """
|
2015-09-29 20:47:22 +00:00
|
|
|
def __init__(self, name, event, state):
|
2015-09-27 09:13:49 +00:00
|
|
|
self._name = name
|
2015-09-29 20:47:22 +00:00
|
|
|
self._event = event
|
2015-09-27 09:13:49 +00:00
|
|
|
self._state = state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" No polling needed for a light. """
|
2015-09-27 09:13:49 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" Returns the name of the light if any. """
|
2015-09-27 09:13:49 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" True if light is on. """
|
2015-09-27 09:13:49 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" Turn the light on. """
|
2015-09-29 20:47:22 +00:00
|
|
|
|
2015-10-07 17:57:40 +00:00
|
|
|
if hasattr(self, '_event') and self._event:
|
2015-10-02 20:39:30 +00:00
|
|
|
self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
|
2015-09-29 20:47:22 +00:00
|
|
|
|
2015-09-27 09:13:49 +00:00
|
|
|
self._state = True
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2015-10-08 09:08:17 +00:00
|
|
|
""" Turn the light off. """
|
2015-09-29 20:47:22 +00:00
|
|
|
|
2015-10-07 17:57:40 +00:00
|
|
|
if hasattr(self, '_event') and self._event:
|
2015-10-02 20:39:30 +00:00
|
|
|
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
|
2015-09-29 20:47:22 +00:00
|
|
|
|
2015-09-27 09:13:49 +00:00
|
|
|
self._state = False
|
2015-09-29 06:20:25 +00:00
|
|
|
self.update_ha_state()
|