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

129 lines
3.8 KiB
Python
Raw Normal View History

2015-09-27 11:13:49 +02:00
"""
homeassistant.components.light.rfxtrx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Rfxtrx lights.
Configuration:
To use Rfxtrx lights you will need to add the following to your
configuration.yaml file.
light:
platform: rfxtrx
devices:
ac09c4f1: Bedroom Light
ac09c4f2: Kitchen Light
ac09c4f3: Bathroom Light
*Optional*
# Automatic add new light
automatic_add: True
"""
import logging
import homeassistant.components.rfxtrx as rfxtrx
import RFXtrx as rfxtrxmod
2015-09-27 11:13:49 +02:00
from homeassistant.components.light import Light
from homeassistant.util import slugify
DEPENDENCIES = ['rfxtrx']
2015-09-27 11:13:49 +02:00
DOMAIN = "rfxtrx"
_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. """
# Add light from config file
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['name'])
2015-10-06 08:44:15 +02:00
rfxobject = rfxtrx.get_rfx_object(entity_info['packetid'])
new_light = RfxtrxLight(entity_info['name'], rfxobject, False)
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):
""" Callback for sensor 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)
if automatic_add:
_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 (previous automatic added)
if entity_id in rfxtrx.RFX_DEVICES:
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 11:13:49 +02:00
if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update)
class RfxtrxLight(Light):
""" Provides a demo switch. """
2015-09-29 22:47:22 +02:00
def __init__(self, name, event, state):
2015-09-27 11:13:49 +02:00
self._name = name
2015-09-29 22:47:22 +02:00
self._event = event
2015-09-27 11:13:49 +02:00
self._state = state
@property
def should_poll(self):
""" No polling needed for a demo light. """
return False
@property
def name(self):
""" Returns the name of the device if any. """
return self._name
@property
def is_on(self):
""" True if device is on. """
return self._state
def turn_on(self, **kwargs):
""" Turn the device on. """
2015-09-29 22:47:22 +02:00
if self._event:
self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
2015-09-29 22:47:22 +02:00
2015-09-27 11:13:49 +02:00
self._state = True
self.update_ha_state()
def turn_off(self, **kwargs):
""" Turn the device off. """
2015-09-29 22:47:22 +02:00
if self._event:
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
2015-09-29 22:47:22 +02:00
2015-09-27 11:13:49 +02:00
self._state = False
self.update_ha_state()