core/homeassistant/components/sensor/rfxtrx.py

106 lines
2.9 KiB
Python
Raw Normal View History

2015-07-23 17:36:05 +00:00
"""
homeassistant.components.sensor.rfxtrx
2015-08-06 15:54:05 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Shows sensor values from RFXtrx sensors.
2015-07-23 17:36:05 +00:00
2015-08-06 15:54:05 +00:00
Configuration:
2015-07-23 17:36:05 +00:00
To use the rfxtrx sensors you will need to add something like the following to
your configuration.yaml file.
2015-07-23 17:36:05 +00:00
2015-08-06 15:54:05 +00:00
sensor:
platform: rfxtrx
device: PATH_TO_DEVICE
2015-07-23 17:36:05 +00:00
2015-08-06 15:54:05 +00:00
Variables:
device
*Required
Path to your RFXtrx device.
E.g. /dev/serial/by-id/usb-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0
2015-07-23 17:36:05 +00:00
"""
import logging
from collections import OrderedDict
2015-07-24 10:35:03 +00:00
from homeassistant.const import (TEMP_CELCIUS)
2015-07-23 17:36:05 +00:00
from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/' +
'ec7a1aaddf8270db6e5da1c13d58c1547effd7cf.zip']
2015-07-23 17:36:05 +00:00
DATA_TYPES = OrderedDict([
('Temperature', TEMP_CELCIUS),
('Humidity', '%'),
('Barometer', ''),
('Wind direction', ''),
2015-07-24 11:06:15 +00:00
('Rain rate', '')])
2015-07-23 17:36:05 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2015-08-06 15:54:05 +00:00
""" Setup the RFXtrx platform. """
2015-07-23 17:36:05 +00:00
logger = logging.getLogger(__name__)
2015-07-24 11:06:15 +00:00
sensors = {} # keep track of sensors added to HA
2015-07-23 17:36:05 +00:00
def sensor_update(event):
2015-07-24 11:06:15 +00:00
""" Callback for sensor updates from the RFXtrx gateway. """
if event.device.id_string in sensors:
sensors[event.device.id_string].event = event
2015-07-23 17:36:05 +00:00
else:
2015-07-24 11:06:15 +00:00
logger.info("adding new sensor: %s", event.device.type_string)
new_sensor = RfxtrxSensor(event)
sensors[event.device.id_string] = new_sensor
add_devices([new_sensor])
2015-07-23 17:36:05 +00:00
try:
import RFXtrx as rfxtrx
except ImportError:
logger.exception(
"Failed to import rfxtrx")
return False
2015-07-24 11:06:15 +00:00
device = config.get("device", "")
2015-07-23 17:36:05 +00:00
rfxtrx.Core(device, sensor_update)
class RfxtrxSensor(Entity):
2015-08-06 15:54:05 +00:00
""" Represents a RFXtrx sensor. """
2015-07-23 17:36:05 +00:00
def __init__(self, event):
self.event = event
self._unit_of_measurement = None
self._data_type = None
for data_type in DATA_TYPES:
if data_type in self.event.values:
self._unit_of_measurement = DATA_TYPES[data_type]
self._data_type = data_type
break
id_string = int(event.device.id_string.replace(":", ""), 16)
self._name = "{} {} ({})".format(self._data_type,
self.event.device.type_string,
id_string)
def __str__(self):
return self._name
@property
def state(self):
if self._data_type:
return self.event.values[self._data_type]
return None
@property
def name(self):
""" Get the mame of the sensor. """
return self._name
@property
def state_attributes(self):
2015-07-24 10:35:03 +00:00
return self.event.values
2015-07-23 17:36:05 +00:00
@property
def unit_of_measurement(self):
""" Unit this state is expressed in. """
2015-07-23 17:36:05 +00:00
return self._unit_of_measurement