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-10-08 09:08:47 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/sensor.rfxtrx/
|
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
|
2015-09-27 09:13:49 +00:00
|
|
|
from RFXtrx import SensorEvent
|
2015-11-01 11:51:09 +00:00
|
|
|
import homeassistant.components.rfxtrx as rfxtrx
|
2015-09-27 09:13:49 +00:00
|
|
|
from homeassistant.util import slugify
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2015-10-07 17:04:03 +00:00
|
|
|
DEPENDENCIES = ['rfxtrx']
|
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-09-29 06:20:25 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-07-23 17:36:05 +00:00
|
|
|
|
|
|
|
|
2015-09-27 09:13:49 +00:00
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2015-08-06 15:54:05 +00:00
|
|
|
""" Setup the RFXtrx platform. """
|
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. """
|
2015-11-01 13:20:11 +00:00
|
|
|
if isinstance(event, SensorEvent):
|
2015-09-29 06:20:25 +00:00
|
|
|
entity_id = slugify(event.device.id_string.lower())
|
|
|
|
|
|
|
|
# Add entity if not exist and the automatic_add is True
|
|
|
|
if entity_id not in rfxtrx.RFX_DEVICES:
|
2015-10-02 20:39:30 +00:00
|
|
|
automatic_add = config.get('automatic_add', True)
|
2015-09-27 09:13:49 +00:00
|
|
|
if automatic_add:
|
2015-10-08 09:10:05 +00:00
|
|
|
_LOGGER.info("Automatic add %s rfxtrx.sensor", entity_id)
|
2015-09-29 06:20:25 +00:00
|
|
|
new_sensor = RfxtrxSensor(event)
|
|
|
|
rfxtrx.RFX_DEVICES[entity_id] = new_sensor
|
|
|
|
add_devices_callback([new_sensor])
|
|
|
|
else:
|
2015-11-08 18:02:51 +00:00
|
|
|
_LOGGER.debug(
|
2015-11-08 10:04:29 +00:00
|
|
|
"EntityID: %s sensor_update",
|
|
|
|
entity_id,
|
|
|
|
)
|
2015-09-29 06:20:25 +00:00
|
|
|
rfxtrx.RFX_DEVICES[entity_id].event = event
|
2015-09-27 09:13:49 +00:00
|
|
|
|
|
|
|
if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
|
|
|
|
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update)
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2015-09-29 06:20:25 +00:00
|
|
|
|
2015-07-23 17:36:05 +00:00
|
|
|
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):
|
2015-10-08 09:08:47 +00:00
|
|
|
""" Returns the state of the device. """
|
2015-07-23 17:36:05 +00:00
|
|
|
if self._data_type:
|
|
|
|
return self.event.values[self._data_type]
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2015-10-08 09:08:47 +00:00
|
|
|
""" Get the name of the sensor. """
|
2015-07-23 17:36:05 +00:00
|
|
|
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):
|
2015-09-07 17:40:09 +00:00
|
|
|
""" Unit this state is expressed in. """
|
2015-07-23 17:36:05 +00:00
|
|
|
return self._unit_of_measurement
|