2015-07-23 17:36:05 +00:00
|
|
|
"""
|
2016-02-23 05:21:49 +00:00
|
|
|
Support for 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-11-01 11:51:09 +00:00
|
|
|
import homeassistant.components.rfxtrx as rfxtrx
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import TEMP_CELCIUS
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-09-27 09:13:49 +00:00
|
|
|
from homeassistant.util import slugify
|
2016-02-23 17:01:53 +00:00
|
|
|
from homeassistant.components.rfxtrx import (
|
|
|
|
ATTR_PACKETID, ATTR_NAME, ATTR_DATA_TYPE)
|
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', ''),
|
2016-02-11 14:35:05 +00:00
|
|
|
('Rain rate', ''),
|
|
|
|
('Energy usage', 'W'),
|
|
|
|
('Total usage', 'W')])
|
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):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Setup the RFXtrx platform."""
|
2015-11-17 08:18:42 +00:00
|
|
|
from RFXtrx import SensorEvent
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2016-02-23 17:01:53 +00:00
|
|
|
sensors = []
|
|
|
|
for device_id, entity_info in config.get('devices', {}).items():
|
|
|
|
if device_id not in rfxtrx.RFX_DEVICES:
|
|
|
|
_LOGGER.info("Add %s rfxtrx.sensor", entity_info[ATTR_NAME])
|
|
|
|
event = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
|
|
|
|
new_sensor = RfxtrxSensor(event, entity_info[ATTR_NAME],
|
|
|
|
entity_info.get(ATTR_DATA_TYPE, None))
|
|
|
|
rfxtrx.RFX_DEVICES[device_id] = new_sensor
|
|
|
|
sensors.append(new_sensor)
|
|
|
|
|
|
|
|
add_devices_callback(sensors)
|
|
|
|
|
2015-07-23 17:36:05 +00:00
|
|
|
def sensor_update(event):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Callback for sensor updates from the RFXtrx gateway."""
|
2016-02-23 17:01:53 +00:00
|
|
|
if not isinstance(event, SensorEvent):
|
|
|
|
return
|
|
|
|
|
|
|
|
device_id = "sensor_" + slugify(event.device.id_string.lower())
|
|
|
|
|
|
|
|
if device_id in rfxtrx.RFX_DEVICES:
|
|
|
|
rfxtrx.RFX_DEVICES[device_id].event = event
|
|
|
|
return
|
|
|
|
|
|
|
|
# Add entity if not exist and the automatic_add is True
|
|
|
|
if config.get('automatic_add', True):
|
|
|
|
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
|
|
|
|
entity_name = "%s : %s" % (device_id, pkt_id)
|
|
|
|
_LOGGER.info(
|
|
|
|
"Automatic add rfxtrx.sensor: (%s : %s)",
|
|
|
|
device_id,
|
|
|
|
pkt_id)
|
|
|
|
|
|
|
|
new_sensor = RfxtrxSensor(event, entity_name)
|
|
|
|
rfxtrx.RFX_DEVICES[device_id] = new_sensor
|
|
|
|
add_devices_callback([new_sensor])
|
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):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Represents a RFXtrx sensor."""
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2016-02-23 17:01:53 +00:00
|
|
|
def __init__(self, event, name, data_type=None):
|
2015-07-23 17:36:05 +00:00
|
|
|
self.event = event
|
|
|
|
self._unit_of_measurement = None
|
|
|
|
self._data_type = None
|
2016-02-23 17:01:53 +00:00
|
|
|
self._name = name
|
|
|
|
if data_type:
|
|
|
|
self._data_type = data_type
|
|
|
|
self._unit_of_measurement = DATA_TYPES[data_type]
|
|
|
|
return
|
2015-07-23 17:36:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Returns the name."""
|
2015-07-23 17:36:05 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Returns the state of the sensor."""
|
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):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Get the name of the sensor."""
|
2015-07-23 17:36:05 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2016-02-07 06:28:29 +00:00
|
|
|
def device_state_attributes(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Returns the state attributes."""
|
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):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Unit this state is expressed in."""
|
2015-07-23 17:36:05 +00:00
|
|
|
return self._unit_of_measurement
|