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
|
2017-04-24 03:41:09 +00:00
|
|
|
|
2016-04-10 23:05:32 +00:00
|
|
|
import voluptuous as vol
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2015-11-01 11:51:09 +00:00
|
|
|
import homeassistant.components.rfxtrx as rfxtrx
|
2016-04-10 23:05:32 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-01-12 19:52:53 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, CONF_NAME
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-09-27 09:13:49 +00:00
|
|
|
from homeassistant.util import slugify
|
2018-01-12 19:52:53 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2016-02-23 17:01:53 +00:00
|
|
|
from homeassistant.components.rfxtrx import (
|
2018-01-12 19:52:53 +00:00
|
|
|
ATTR_NAME, ATTR_FIRE_EVENT, ATTR_DATA_TYPE, CONF_AUTOMATIC_ADD,
|
|
|
|
CONF_FIRE_EVENT, CONF_DEVICES, DATA_TYPES, CONF_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
|
|
|
|
2015-09-29 06:20:25 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2018-01-12 19:52:53 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_DEVICES, default={}): {
|
|
|
|
cv.string: vol.Schema({
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_DATA_TYPE, default=[]):
|
|
|
|
vol.All(cv.ensure_list, [vol.In(DATA_TYPES.keys())]),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
|
2016-04-10 23:05:32 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
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):
|
2017-04-24 03:41:09 +00:00
|
|
|
"""Set up the RFXtrx platform."""
|
2015-11-17 08:18:42 +00:00
|
|
|
from RFXtrx import SensorEvent
|
2016-02-23 17:01:53 +00:00
|
|
|
sensors = []
|
2016-09-01 16:58:32 +00:00
|
|
|
for packet_id, entity_info in config[CONF_DEVICES].items():
|
2016-04-23 17:55:05 +00:00
|
|
|
event = rfxtrx.get_rfx_object(packet_id)
|
|
|
|
device_id = "sensor_" + slugify(event.device.id_string.lower())
|
2016-02-25 16:40:24 +00:00
|
|
|
if device_id in rfxtrx.RFX_DEVICES:
|
|
|
|
continue
|
|
|
|
_LOGGER.info("Add %s rfxtrx.sensor", entity_info[ATTR_NAME])
|
2016-04-24 09:48:01 +00:00
|
|
|
|
2016-04-23 17:55:05 +00:00
|
|
|
sub_sensors = {}
|
2016-04-24 09:48:01 +00:00
|
|
|
data_types = entity_info[ATTR_DATA_TYPE]
|
2017-04-24 03:41:09 +00:00
|
|
|
if not data_types:
|
2016-09-01 16:58:32 +00:00
|
|
|
data_types = ['']
|
2016-04-24 09:48:01 +00:00
|
|
|
for data_type in DATA_TYPES:
|
|
|
|
if data_type in event.values:
|
|
|
|
data_types = [data_type]
|
|
|
|
break
|
|
|
|
for _data_type in data_types:
|
2016-07-13 14:46:11 +00:00
|
|
|
new_sensor = RfxtrxSensor(None, entity_info[ATTR_NAME],
|
2018-01-12 19:52:53 +00:00
|
|
|
_data_type, entity_info[ATTR_FIRE_EVENT])
|
2016-04-23 17:55:05 +00:00
|
|
|
sensors.append(new_sensor)
|
|
|
|
sub_sensors[_data_type] = new_sensor
|
2016-04-24 09:48:01 +00:00
|
|
|
rfxtrx.RFX_DEVICES[device_id] = sub_sensors
|
2016-02-23 17:01:53 +00:00
|
|
|
add_devices_callback(sensors)
|
|
|
|
|
2015-07-23 17:36:05 +00:00
|
|
|
def sensor_update(event):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle 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:
|
2016-04-23 17:55:05 +00:00
|
|
|
sensors = rfxtrx.RFX_DEVICES[device_id]
|
|
|
|
for key in sensors:
|
2016-07-20 02:15:50 +00:00
|
|
|
sensor = sensors[key]
|
|
|
|
sensor.event = event
|
|
|
|
# Fire event
|
|
|
|
if sensors[key].should_fire_event:
|
|
|
|
sensor.hass.bus.fire(
|
|
|
|
"signal_received", {
|
|
|
|
ATTR_ENTITY_ID:
|
|
|
|
sensors[key].entity_id,
|
|
|
|
}
|
|
|
|
)
|
2016-02-23 17:01:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Add entity if not exist and the automatic_add is True
|
2018-01-12 19:52:53 +00:00
|
|
|
if not config[CONF_AUTOMATIC_ADD]:
|
2016-04-24 09:48:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.info("Automatic add rfxtrx.sensor: %s", pkt_id)
|
2016-04-24 09:48:01 +00:00
|
|
|
|
2016-09-01 16:58:32 +00:00
|
|
|
data_type = ''
|
2016-05-07 01:24:43 +00:00
|
|
|
for _data_type in DATA_TYPES:
|
|
|
|
if _data_type in event.values:
|
|
|
|
data_type = _data_type
|
2016-04-24 09:48:01 +00:00
|
|
|
break
|
2016-05-07 01:24:43 +00:00
|
|
|
new_sensor = RfxtrxSensor(event, pkt_id, data_type)
|
2016-04-24 09:48:01 +00:00
|
|
|
sub_sensors = {}
|
|
|
|
sub_sensors[new_sensor.data_type] = new_sensor
|
|
|
|
rfxtrx.RFX_DEVICES[device_id] = sub_sensors
|
|
|
|
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-03-08 15:46:34 +00:00
|
|
|
"""Representation of a RFXtrx sensor."""
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2016-07-20 02:15:50 +00:00
|
|
|
def __init__(self, event, name, data_type, should_fire_event=False):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-07-23 17:36:05 +00:00
|
|
|
self.event = event
|
2016-02-23 17:01:53 +00:00
|
|
|
self._name = name
|
2016-07-20 02:15:50 +00:00
|
|
|
self.should_fire_event = should_fire_event
|
2016-05-07 01:24:43 +00:00
|
|
|
self.data_type = data_type
|
2016-08-25 17:52:48 +00:00
|
|
|
self._unit_of_measurement = DATA_TYPES.get(data_type, '')
|
2015-07-23 17:36:05 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-07-23 17:36:05 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-09-01 16:58:32 +00:00
|
|
|
if not self.event:
|
|
|
|
return None
|
|
|
|
return self.event.values.get(self.data_type)
|
2015-07-23 17:36:05 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Get the name of the sensor."""
|
2016-11-22 20:47:37 +00:00
|
|
|
return "{} {}".format(self._name, self.data_type)
|
2015-07-23 17:36:05 +00:00
|
|
|
|
|
|
|
@property
|
2016-02-07 06:28:29 +00:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state attributes."""
|
2016-09-01 16:58:32 +00:00
|
|
|
if not self.event:
|
|
|
|
return None
|
|
|
|
return self.event.values
|
2015-07-23 17:36:05 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit this state is expressed in."""
|
2015-07-23 17:36:05 +00:00
|
|
|
return self._unit_of_measurement
|