From 44ce756cbae80b5f672a3ded64aae7e9eb4c91c8 Mon Sep 17 00:00:00 2001 From: Daniel Hoyer Iversen Date: Thu, 23 Jul 2015 19:36:05 +0200 Subject: [PATCH 1/6] Support for rfxtrx sensors --- homeassistant/components/sensor/rfxtrx.py | 119 ++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 homeassistant/components/sensor/rfxtrx.py diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py new file mode 100644 index 00000000000..879e3326eb8 --- /dev/null +++ b/homeassistant/components/sensor/rfxtrx.py @@ -0,0 +1,119 @@ +""" +homeassistant.components.sensor.rfxtrx +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Shows sensor values from rfxtrx sensors. + +Possible config keys: + +path to rfxtrx device +device=/dev/serial/by-id/usb-rfxtrx_RFXtrx433_A1Y0NJGR-if00-port0 + +id of the sensor: Name the sensor with ID +135=Outside + +only_named: Only show the named sensors +only_named=1 + +datatype_mask: mask to determine which sensor values to show based on + +datatype_mask=1 # only show temperature +datatype_mask=127 # show all sensor values +""" +import logging +from collections import OrderedDict + +from homeassistant.const import ( + ATTR_BATTERY_LEVEL, 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', '%'), + ('Forecast', ''), + ('Barometer', ''), + ('Wind direction', ''), + ('Humidity status', ''), + ('Humidity status numeric', ''), + ('Forecast numeric', ''), + ('Rain rate', ''), + ('Rain total', ''), + ('Wind average speed', ''), + ('Wind gust', ''), + ('Chill', ''), + ('Battery numeric', ATTR_BATTERY_LEVEL), + ('Rssi numeric', '')]) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Setup the rfxtrx platform. """ + logger = logging.getLogger(__name__) + + devices = {} # keep track of devices added to HA + + def sensor_update(event): + """ Callback for sensor updates from the MySensors gateway. """ + if event.device.id_string in devices: + devices[event.device.id_string].event = event + else: + logger.info("adding new devices: %s", event.device.type_string) + new_device = RfxtrxSensor(event) + devices[event.device.id_string] = new_device + add_devices([new_device]) + try: + import RFXtrx as rfxtrx + except ImportError: + logger.exception( + "Failed to import rfxtrx") + return False + + device = config.get("device", True) + rfxtrx.Core(device, sensor_update) + + +class RfxtrxSensor(Entity): + """ Represents a Vera 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): + attr = super().state_attributes + for data_type in DATA_TYPES: + if data_type in self.event.values: + attr[data_type] = self.event.values[data_type] + return attr + + @property + def unit_of_measurement(self): + return self._unit_of_measurement From f44acc9b0e695e96507c21aefa84325c7d792a67 Mon Sep 17 00:00:00 2001 From: Daniel Hoyer Iversen Date: Thu, 23 Jul 2015 19:42:20 +0200 Subject: [PATCH 2/6] requirements file --- requirements.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/requirements.txt b/requirements.txt index c7a569fad33..06aea3fa518 100644 --- a/requirements.txt +++ b/requirements.txt @@ -79,3 +79,6 @@ PyMata==2.07a # Mysensors serial gateway pyserial>=2.7 + +#Rfxtrx sensor +https://github.com/Danielhiversen/pyRFXtrx/archive/master.zip From 8f99ebf27e676390c2ee919c8b8c5b8d20462934 Mon Sep 17 00:00:00 2001 From: Daniel Hoyer Iversen Date: Thu, 23 Jul 2015 19:47:45 +0200 Subject: [PATCH 3/6] Documentation of rfxtrx sensor --- homeassistant/components/sensor/rfxtrx.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index 879e3326eb8..c2950c0d9c2 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -4,20 +4,14 @@ homeassistant.components.sensor.rfxtrx Shows sensor values from rfxtrx sensors. Possible config keys: +device="path to rfxtrx device" -path to rfxtrx device -device=/dev/serial/by-id/usb-rfxtrx_RFXtrx433_A1Y0NJGR-if00-port0 +Example: -id of the sensor: Name the sensor with ID -135=Outside +sensor 2: + platform: rfxtrx + device : /dev/serial/by-id/usb-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0 -only_named: Only show the named sensors -only_named=1 - -datatype_mask: mask to determine which sensor values to show based on - -datatype_mask=1 # only show temperature -datatype_mask=127 # show all sensor values """ import logging from collections import OrderedDict From b54c58235f49f93fc3245a351a29ba636b79ea91 Mon Sep 17 00:00:00 2001 From: Daniel Hoyer Iversen Date: Thu, 23 Jul 2015 19:50:26 +0200 Subject: [PATCH 4/6] Documentation of rfxtrx sensor --- homeassistant/components/sensor/rfxtrx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index c2950c0d9c2..12e6afde3d1 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -8,7 +8,7 @@ device="path to rfxtrx device" Example: -sensor 2: +sensor 2: platform: rfxtrx device : /dev/serial/by-id/usb-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0 From 1489af0ecaa5b16ca5951482a50d5136ebad5faf Mon Sep 17 00:00:00 2001 From: Daniel Hoyer Iversen Date: Fri, 24 Jul 2015 12:35:03 +0200 Subject: [PATCH 5/6] updated rfxcom sensor --- homeassistant/components/sensor/rfxtrx.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index 12e6afde3d1..1b2a4643449 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -16,8 +16,7 @@ sensor 2: import logging from collections import OrderedDict -from homeassistant.const import ( - ATTR_BATTERY_LEVEL, TEMP_CELCIUS) +from homeassistant.const import (TEMP_CELCIUS) from homeassistant.helpers.entity import Entity REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/master.zip' @@ -37,7 +36,7 @@ DATA_TYPES = OrderedDict([ ('Wind average speed', ''), ('Wind gust', ''), ('Chill', ''), - ('Battery numeric', ATTR_BATTERY_LEVEL), + ('Battery numeric', '%'), ('Rssi numeric', '')]) @@ -102,11 +101,7 @@ class RfxtrxSensor(Entity): @property def state_attributes(self): - attr = super().state_attributes - for data_type in DATA_TYPES: - if data_type in self.event.values: - attr[data_type] = self.event.values[data_type] - return attr + return self.event.values @property def unit_of_measurement(self): From 3658c579129151980765886464054cf23aa7d214 Mon Sep 17 00:00:00 2001 From: Daniel Hoyer Iversen Date: Fri, 24 Jul 2015 13:06:15 +0200 Subject: [PATCH 6/6] updated rfxcom sensor --- homeassistant/components/sensor/rfxtrx.py | 32 ++++++++--------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index 1b2a4643449..b9762995ea5 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -25,36 +25,26 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/master.zip' DATA_TYPES = OrderedDict([ ('Temperature', TEMP_CELCIUS), ('Humidity', '%'), - ('Forecast', ''), ('Barometer', ''), ('Wind direction', ''), - ('Humidity status', ''), - ('Humidity status numeric', ''), - ('Forecast numeric', ''), - ('Rain rate', ''), - ('Rain total', ''), - ('Wind average speed', ''), - ('Wind gust', ''), - ('Chill', ''), - ('Battery numeric', '%'), - ('Rssi numeric', '')]) + ('Rain rate', '')]) def setup_platform(hass, config, add_devices, discovery_info=None): """ Setup the rfxtrx platform. """ logger = logging.getLogger(__name__) - devices = {} # keep track of devices added to HA + sensors = {} # keep track of sensors added to HA def sensor_update(event): - """ Callback for sensor updates from the MySensors gateway. """ - if event.device.id_string in devices: - devices[event.device.id_string].event = 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 devices: %s", event.device.type_string) - new_device = RfxtrxSensor(event) - devices[event.device.id_string] = new_device - add_devices([new_device]) + 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: @@ -62,12 +52,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): "Failed to import rfxtrx") return False - device = config.get("device", True) + device = config.get("device", "") rfxtrx.Core(device, sensor_update) class RfxtrxSensor(Entity): - """ Represents a Vera Sensor. """ + """ Represents a Rfxtrx Sensor. """ def __init__(self, event): self.event = event