core/homeassistant/components/rfxtrx.py

97 lines
2.5 KiB
Python
Raw Normal View History

"""
homeassistant.components.rfxtrx
2015-10-08 09:08:32 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-10-09 12:40:48 +00:00
Provides 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
2015-10-03 09:26:18 +00:00
from homeassistant.util import slugify
2015-09-27 09:13:49 +00:00
2015-10-07 17:57:40 +00:00
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip' +
'#RFXtrx==0.2']
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'
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):
2015-10-08 09:08:32 +00:00
""" Setup the RFXtrx component. """
2015-09-27 09:13:49 +00:00
# Declare the Handle event
def handle_receive(event):
""" Callback all subscribers for RFXtrx gateway. """
2015-10-03 09:26:18 +00:00
# Log RFXCOM event
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
# Callback to HA registered components
2015-09-27 09:13:49 +00:00
for subscriber in RECEIVED_EVT_SUBSCRIBERS:
subscriber(event)
# Try to load the RFXtrx module
try:
import RFXtrx as rfxtrxmod
except ImportError:
_LOGGER.exception("Failed to import rfxtrx")
2015-09-27 09:13:49 +00:00
return False
# Init the rfxtrx module
2015-09-29 20:47:22 +00:00
global RFXOBJECT
if ATTR_DEVICE not in config[DOMAIN]:
2015-10-07 17:57:40 +00:00
_LOGGER.exception(
"can found device parameter in %s YAML configuration section",
DOMAIN
)
return False
device = config[DOMAIN][ATTR_DEVICE]
debug = config[DOMAIN].get(ATTR_DEBUG, False)
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):
2015-10-08 09:08:32 +00:00
""" Return the RFXObject with the packetid. """
2015-10-03 09:26:18 +00:00
try:
import RFXtrx as rfxtrxmod
except ImportError:
_LOGGER.exception("Failed to import rfxtrx")
return False
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