mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
commit
37ec18b363
98
homeassistant/components/sensor/rfxtrx.py
Normal file
98
homeassistant/components/sensor/rfxtrx.py
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
"""
|
||||||
|
homeassistant.components.sensor.rfxtrx
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
Shows sensor values from rfxtrx sensors.
|
||||||
|
|
||||||
|
Possible config keys:
|
||||||
|
device="path to rfxtrx device"
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
sensor 2:
|
||||||
|
platform: rfxtrx
|
||||||
|
device : /dev/serial/by-id/usb-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0
|
||||||
|
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
from homeassistant.const import (TEMP_CELCIUS)
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/master.zip'
|
||||||
|
'#RFXtrx>=0.15']
|
||||||
|
|
||||||
|
DATA_TYPES = OrderedDict([
|
||||||
|
('Temperature', TEMP_CELCIUS),
|
||||||
|
('Humidity', '%'),
|
||||||
|
('Barometer', ''),
|
||||||
|
('Wind direction', ''),
|
||||||
|
('Rain rate', '')])
|
||||||
|
|
||||||
|
|
||||||
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
""" Setup the rfxtrx platform. """
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
sensors = {} # keep track of sensors added to HA
|
||||||
|
|
||||||
|
def sensor_update(event):
|
||||||
|
""" Callback for sensor updates from the RFXtrx gateway. """
|
||||||
|
if event.device.id_string in sensors:
|
||||||
|
sensors[event.device.id_string].event = event
|
||||||
|
else:
|
||||||
|
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])
|
||||||
|
try:
|
||||||
|
import RFXtrx as rfxtrx
|
||||||
|
except ImportError:
|
||||||
|
logger.exception(
|
||||||
|
"Failed to import rfxtrx")
|
||||||
|
return False
|
||||||
|
|
||||||
|
device = config.get("device", "")
|
||||||
|
rfxtrx.Core(device, sensor_update)
|
||||||
|
|
||||||
|
|
||||||
|
class RfxtrxSensor(Entity):
|
||||||
|
""" Represents a Rfxtrx Sensor. """
|
||||||
|
|
||||||
|
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):
|
||||||
|
return self.event.values
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self):
|
||||||
|
return self._unit_of_measurement
|
@ -79,3 +79,6 @@ PyMata==2.07a
|
|||||||
|
|
||||||
# Mysensors serial gateway
|
# Mysensors serial gateway
|
||||||
pyserial>=2.7
|
pyserial>=2.7
|
||||||
|
|
||||||
|
#Rfxtrx sensor
|
||||||
|
https://github.com/Danielhiversen/pyRFXtrx/archive/master.zip
|
||||||
|
Loading…
x
Reference in New Issue
Block a user