core/homeassistant/components/switch/rfxtrx.py

120 lines
3.7 KiB
Python
Raw Normal View History

2015-09-27 09:13:49 +00:00
"""
homeassistant.components.switch.rfxtrx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Rfxtrx switch.
Configuration:
To use Rfxtrx switchs you will need to add the following to your
configuration.yaml file.
switch:
platform: rfxtrx
devices:
ac09c4f1: Bedroom Door
ac09c4f2: Kitchen Door
ac09c4f3: Bathroom Door
*Optional*
# Automatic add new switch
automatic_add: True
"""
import logging
import homeassistant.components.rfxtrx as rfxtrx
from RFXtrx import LightingDevice
from homeassistant.components.switch import SwitchDevice
from homeassistant.util import slugify
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/' +
'ec7a1aaddf8270db6e5da1c13d58c1547effd7cf.zip#RFXtrx==0.15']
DOMAIN = "rfxtrx"
_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-09-27 21:51:19 +00:00
2015-09-27 09:13:49 +00:00
# Add switch from config file
2015-09-27 21:51:19 +00:00
switchs = []
2015-09-27 09:13:49 +00:00
devices = config.get('devices')
2015-09-29 20:47:22 +00:00
for entity_id, entity_info in devices.items():
2015-09-27 09:13:49 +00:00
if entity_id not in rfxtrx.RFX_DEVICES:
2015-09-29 20:47:22 +00:00
_LOGGER.info("Add %s rfxtrx.switch", entity_info['name'])
new_switch = RfxtrxSwitch(entity_info['name'], None, False)
2015-09-27 09:13:49 +00:00
rfxtrx.RFX_DEVICES[entity_id] = new_switch
2015-09-27 21:51:19 +00:00
switchs.append(new_switch)
2015-09-27 09:13:49 +00:00
2015-09-27 21:51:19 +00:00
add_devices_callback(switchs)
2015-09-27 09:13:49 +00:00
def switch_update(event):
""" Callback for sensor updates from the RFXtrx gateway. """
if isinstance(event.device, LightingDevice):
2015-09-27 21:51:19 +00:00
entity_id = slugify(event.device.id_string.lower())
2015-09-27 09:13:49 +00:00
# Add entity if not exist and the automatic_add is True
if entity_id not in rfxtrx.RFX_DEVICES:
automatic_add = config.get('automatic_add', False)
if automatic_add:
2015-09-29 20:47:22 +00:00
_LOGGER.info("Automatic add %s rfxtrx.switch (class: %s subtype: %s)",
entity_id,
event.device.__class__.__name__,
event.device.subtype
)
new_switch = RfxtrxSwitch(entity_id, event, False)
2015-09-27 09:13:49 +00:00
rfxtrx.RFX_DEVICES[entity_id] = new_switch
add_devices_callback([new_switch])
# Check if entity exists (previous automatic added)
if entity_id in rfxtrx.RFX_DEVICES:
if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off':
2015-09-27 09:13:49 +00:00
if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on()
else:
rfxtrx.RFX_DEVICES[entity_id].turn_off()
if switch_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(switch_update)
class RfxtrxSwitch(SwitchDevice):
""" Provides a demo switch. """
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):
""" No polling needed for a demo switch. """
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 20:47:22 +00:00
self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
2015-09-27 09:13:49 +00:00
self._state = True
self.update_ha_state()
def turn_off(self, **kwargs):
""" Turn the device off. """
2015-09-29 20:47:22 +00:00
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
2015-09-27 09:13:49 +00:00
self._state = False
self.update_ha_state()