2019-02-13 20:21:14 +00:00
|
|
|
"""Support for RFXtrx sensors."""
|
2015-07-23 17:36:05 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-12 19:37:59 +00:00
|
|
|
from RFXtrx import SensorEvent
|
2019-10-14 15:38:34 +00:00
|
|
|
import voluptuous as vol
|
2019-10-12 19:37:59 +00:00
|
|
|
|
2020-07-10 19:47:37 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_BATTERY,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
)
|
2020-07-10 12:52:07 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, CONF_DEVICES, CONF_NAME
|
2018-01-21 06:35:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_AUTOMATIC_ADD,
|
|
|
|
CONF_DATA_TYPE,
|
|
|
|
CONF_FIRE_EVENT,
|
|
|
|
DATA_TYPES,
|
2020-07-05 22:10:26 +00:00
|
|
|
SIGNAL_EVENT,
|
2020-07-10 12:52:07 +00:00
|
|
|
get_device_id,
|
2019-12-11 14:58:49 +00:00
|
|
|
get_rfx_object,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2015-09-29 06:20:25 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2019-07-31 19:25:30 +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,
|
2018-01-12 19:52:53 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-04-10 23:05:32 +00:00
|
|
|
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2020-07-10 19:47:37 +00:00
|
|
|
def _battery_convert(value):
|
|
|
|
"""Battery is given as a value between 0 and 9."""
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return value * 10
|
|
|
|
|
|
|
|
|
|
|
|
def _rssi_convert(value):
|
|
|
|
"""Rssi is given as dBm value."""
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return f"{value*8-120}"
|
|
|
|
|
|
|
|
|
|
|
|
DEVICE_CLASSES = {
|
|
|
|
"Battery numeric": DEVICE_CLASS_BATTERY,
|
|
|
|
"Rssi numeric": DEVICE_CLASS_SIGNAL_STRENGTH,
|
|
|
|
"Humidity": DEVICE_CLASS_HUMIDITY,
|
|
|
|
"Temperature": DEVICE_CLASS_TEMPERATURE,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CONVERT_FUNCTIONS = {
|
|
|
|
"Battery numeric": _battery_convert,
|
|
|
|
"Rssi numeric": _rssi_convert,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-24 03:41:09 +00:00
|
|
|
"""Set up the RFXtrx platform."""
|
2020-07-10 12:52:07 +00:00
|
|
|
data_ids = set()
|
|
|
|
|
|
|
|
entities = []
|
2016-09-01 16:58:32 +00:00
|
|
|
for packet_id, entity_info in config[CONF_DEVICES].items():
|
2019-12-11 14:58:49 +00:00
|
|
|
event = get_rfx_object(packet_id)
|
2020-07-10 12:52:07 +00:00
|
|
|
if event is None:
|
|
|
|
_LOGGER.error("Invalid device: %s", packet_id)
|
2016-02-25 16:40:24 +00:00
|
|
|
continue
|
2020-07-10 12:52:07 +00:00
|
|
|
|
|
|
|
if entity_info[CONF_DATA_TYPE]:
|
|
|
|
data_types = entity_info[CONF_DATA_TYPE]
|
|
|
|
else:
|
|
|
|
data_types = list(set(event.values) & set(DATA_TYPES))
|
|
|
|
|
|
|
|
device_id = get_device_id(event.device)
|
|
|
|
for data_type in data_types:
|
|
|
|
data_id = (*device_id, data_type)
|
|
|
|
if data_id in data_ids:
|
|
|
|
continue
|
|
|
|
data_ids.add(data_id)
|
|
|
|
|
|
|
|
entity = RfxtrxSensor(
|
2020-06-29 04:39:56 +00:00
|
|
|
event.device,
|
2020-07-10 12:52:07 +00:00
|
|
|
entity_info[CONF_NAME],
|
|
|
|
data_type,
|
|
|
|
entity_info[CONF_FIRE_EVENT],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-07-10 12:52:07 +00:00
|
|
|
entities.append(entity)
|
|
|
|
|
|
|
|
add_entities(entities)
|
2016-02-23 17:01:53 +00:00
|
|
|
|
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
|
|
|
|
|
2019-09-03 19:14:39 +00:00
|
|
|
pkt_id = "".join(f"{x:02x}" for x in event.data)
|
2020-07-10 12:52:07 +00:00
|
|
|
device_id = get_device_id(event.device)
|
|
|
|
for data_type in set(event.values) & set(DATA_TYPES):
|
|
|
|
data_id = (*device_id, data_type)
|
|
|
|
if data_id in data_ids:
|
|
|
|
continue
|
|
|
|
data_ids.add(data_id)
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Added sensor (Device ID: %s Class: %s Sub: %s)",
|
|
|
|
event.device.id_string.lower(),
|
|
|
|
event.device.__class__.__name__,
|
|
|
|
event.device.subtype,
|
|
|
|
)
|
|
|
|
|
|
|
|
entity = RfxtrxSensor(event.device, pkt_id, data_type, event=event)
|
|
|
|
add_entities([entity])
|
2015-09-27 09:13:49 +00:00
|
|
|
|
2020-07-05 22:10:26 +00:00
|
|
|
# Subscribe to main RFXtrx events
|
2020-07-10 12:52:07 +00:00
|
|
|
if config[CONF_AUTOMATIC_ADD]:
|
|
|
|
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, 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
|
|
|
|
2020-07-09 09:40:37 +00:00
|
|
|
def __init__(self, device, name, data_type, should_fire_event=False, event=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-07-09 09:40:37 +00:00
|
|
|
self.event = None
|
2020-07-05 22:10:26 +00:00
|
|
|
self._device = device
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unit_of_measurement = DATA_TYPES.get(data_type, "")
|
2020-07-10 12:52:07 +00:00
|
|
|
self._device_id = get_device_id(device)
|
|
|
|
self._unique_id = "_".join(x for x in (*self._device_id, data_type))
|
2015-07-23 17:36:05 +00:00
|
|
|
|
2020-07-10 19:47:37 +00:00
|
|
|
self._device_class = DEVICE_CLASSES.get(data_type)
|
|
|
|
self._convert_fun = CONVERT_FUNCTIONS.get(data_type, lambda x: x)
|
|
|
|
|
2020-07-09 09:40:37 +00:00
|
|
|
if event:
|
|
|
|
self._apply_event(event)
|
|
|
|
|
2020-07-05 22:10:26 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Restore RFXtrx switch device state (ON/OFF)."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
self.async_on_remove(
|
|
|
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
2020-07-09 09:40:37 +00:00
|
|
|
SIGNAL_EVENT, self._handle_event
|
2020-07-05 22:10:26 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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
|
2020-07-10 19:47:37 +00:00
|
|
|
value = self.event.values.get(self.data_type)
|
|
|
|
return self._convert_fun(value)
|
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."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{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):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return the device 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
|
2020-06-28 04:59:42 +00:00
|
|
|
|
2020-07-10 19:47:37 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return a device class for sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2020-06-28 04:59:42 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique identifier of remote device."""
|
|
|
|
return self._unique_id
|
2020-07-05 22:10:26 +00:00
|
|
|
|
2020-07-09 09:40:37 +00:00
|
|
|
def _apply_event(self, event):
|
2020-07-05 22:10:26 +00:00
|
|
|
"""Apply command from rfxtrx."""
|
|
|
|
self.event = event
|
2020-07-09 09:40:37 +00:00
|
|
|
|
|
|
|
def _handle_event(self, event):
|
|
|
|
"""Check if event applies to me and update."""
|
|
|
|
if not isinstance(event, SensorEvent):
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.device.id_string != self._device.id_string:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.data_type not in event.values:
|
|
|
|
return
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Sensor update (Device ID: %s Class: %s Sub: %s)",
|
|
|
|
event.device.id_string,
|
|
|
|
event.device.__class__.__name__,
|
|
|
|
event.device.subtype,
|
|
|
|
)
|
|
|
|
|
|
|
|
self._apply_event(event)
|
|
|
|
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
if self.should_fire_event:
|
|
|
|
self.hass.bus.fire("signal_received", {ATTR_ENTITY_ID: self.entity_id})
|