core/homeassistant/components/rfxtrx.py

96 lines
2.6 KiB
Python
Raw Normal View History

"""
2016-03-07 17:49:31 +00:00
Support for RFXtrx components.
2015-10-23 20:29:22 +00:00
For more details about this component, please refer to the documentation at
2015-11-26 07:27:31 +00:00
https://home-assistant.io/components/rfxtrx/
"""
2015-09-27 09:13:49 +00:00
import logging
2016-02-19 05:27:50 +00:00
2015-10-03 09:26:18 +00:00
from homeassistant.util import slugify
2015-09-27 09:13:49 +00:00
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/' +
2016-03-03 16:47:50 +00:00
'archive/0.5.zip#pyRFXtrx==0.5']
2015-09-27 09:13:49 +00:00
DOMAIN = "rfxtrx"
ATTR_DEVICE = 'device'
ATTR_DEBUG = 'debug'
ATTR_STATE = 'state'
ATTR_NAME = 'name'
ATTR_PACKETID = 'packetid'
ATTR_FIREEVENT = 'fire_event'
2016-02-23 17:01:53 +00:00
ATTR_DATA_TYPE = 'data_type'
ATTR_DUMMY = "dummy"
EVENT_BUTTON_PRESSED = 'button_pressed'
2015-09-27 09:13:49 +00:00
RECEIVED_EVT_SUBSCRIBERS = []
RFX_DEVICES = {}
_LOGGER = logging.getLogger(__name__)
2015-09-29 20:47:22 +00:00
RFXOBJECT = None
2015-09-27 09:13:49 +00:00
2015-10-06 06:44:15 +00:00
2015-09-27 09:13:49 +00:00
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Setup the RFXtrx component."""
2015-09-27 09:13:49 +00:00
# Declare the Handle event
def handle_receive(event):
2016-03-07 17:49:31 +00:00
"""Callback all subscribers for RFXtrx gateway."""
2015-10-03 09:26:18 +00:00
# Log RFXCOM event
if not event.device.id_string:
return
2015-10-03 09:26:18 +00:00
entity_id = slugify(event.device.id_string.lower())
packet_id = "".join("{0:02x}".format(x) for x in event.data)
2015-10-06 06:44:15 +00:00
entity_name = "%s : %s" % (entity_id, packet_id)
_LOGGER.info("Receive RFXCOM event from %s => %s",
event.device, entity_name)
2015-10-03 09:26:18 +00:00
2016-03-07 17:49:31 +00:00
# Callback to HA registered components.
2015-09-27 09:13:49 +00:00
for subscriber in RECEIVED_EVT_SUBSCRIBERS:
subscriber(event)
2016-03-07 17:49:31 +00:00
# Try to load the RFXtrx module.
2016-01-29 05:37:08 +00:00
import RFXtrx as rfxtrxmod
2015-09-27 09:13:49 +00:00
2016-03-07 17:49:31 +00:00
# Init the rfxtrx module.
2015-09-29 20:47:22 +00:00
global RFXOBJECT
if ATTR_DEVICE not in config[DOMAIN]:
_LOGGER.error(
"can not find device parameter in %s YAML configuration section",
2015-10-07 17:57:40 +00:00
DOMAIN
)
return False
device = config[DOMAIN][ATTR_DEVICE]
debug = config[DOMAIN].get(ATTR_DEBUG, False)
dummy_connection = config[DOMAIN].get(ATTR_DUMMY, False)
if dummy_connection:
RFXOBJECT =\
rfxtrxmod.Core(device, handle_receive, debug=debug,
transport_protocol=rfxtrxmod.DummyTransport2)
else:
RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug)
2015-09-27 09:13:49 +00:00
return True
2015-10-06 06:44:15 +00:00
def get_rfx_object(packetid):
2016-03-07 17:49:31 +00:00
"""Return the RFXObject with the packetid."""
2016-01-29 05:37:08 +00:00
import RFXtrx as rfxtrxmod
2015-10-03 09:26:18 +00:00
binarypacket = bytearray.fromhex(packetid)
pkt = rfxtrxmod.lowlevel.parse(binarypacket)
if pkt is not None:
if isinstance(pkt, rfxtrxmod.lowlevel.SensorPacket):
obj = rfxtrxmod.SensorEvent(pkt)
elif isinstance(pkt, rfxtrxmod.lowlevel.Status):
obj = rfxtrxmod.StatusEvent(pkt)
else:
obj = rfxtrxmod.ControlEvent(pkt)
return obj
2015-11-26 07:12:04 +00:00
return None