2019-02-13 21:21:14 +01:00
|
|
|
"""Support for RFXtrx sensors."""
|
2015-07-23 19:36:05 +02:00
|
|
|
import logging
|
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
from RFXtrx import ControlEvent, SensorEvent
|
2019-10-12 21:37:59 +02:00
|
|
|
|
2020-07-10 21:47:37 +02:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_BATTERY,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
)
|
2020-07-13 02:57:19 +02:00
|
|
|
from homeassistant.const import CONF_DEVICES
|
2020-07-13 23:24:28 +02:00
|
|
|
from homeassistant.core import callback
|
2015-07-23 19:36:05 +02:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from . import (
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_AUTOMATIC_ADD,
|
|
|
|
DATA_TYPES,
|
2020-07-06 00:10:26 +02:00
|
|
|
SIGNAL_EVENT,
|
2020-07-18 13:43:38 +02:00
|
|
|
RfxtrxEntity,
|
2020-07-10 14:52:07 +02:00
|
|
|
get_device_id,
|
2019-12-11 14:58:49 +00:00
|
|
|
get_rfx_object,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2015-09-29 08:20:25 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-07-23 19:36:05 +02:00
|
|
|
|
|
|
|
|
2020-07-10 21:47:37 +02: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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-13 23:24:28 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass, config_entry, async_add_entities,
|
|
|
|
):
|
|
|
|
"""Set up platform."""
|
2020-07-21 09:44:00 +02:00
|
|
|
discovery_info = config_entry.data
|
2020-07-10 14:52:07 +02:00
|
|
|
data_ids = set()
|
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
def supported(event):
|
|
|
|
return isinstance(event, (ControlEvent, SensorEvent))
|
|
|
|
|
2020-07-10 14:52:07 +02:00
|
|
|
entities = []
|
2020-07-13 02:57:19 +02:00
|
|
|
for packet_id in discovery_info[CONF_DEVICES]:
|
2019-12-11 14:58:49 +00:00
|
|
|
event = get_rfx_object(packet_id)
|
2020-07-10 14:52:07 +02:00
|
|
|
if event is None:
|
|
|
|
_LOGGER.error("Invalid device: %s", packet_id)
|
2016-02-25 17:40:24 +01:00
|
|
|
continue
|
2020-07-12 22:03:22 +02:00
|
|
|
if not supported(event):
|
|
|
|
continue
|
2020-07-10 14:52:07 +02:00
|
|
|
|
|
|
|
device_id = get_device_id(event.device)
|
2020-07-12 22:03:22 +02:00
|
|
|
for data_type in set(event.values) & set(DATA_TYPES):
|
2020-07-10 14:52:07 +02:00
|
|
|
data_id = (*device_id, data_type)
|
|
|
|
if data_id in data_ids:
|
|
|
|
continue
|
|
|
|
data_ids.add(data_id)
|
|
|
|
|
2020-07-13 02:57:19 +02:00
|
|
|
entity = RfxtrxSensor(event.device, device_id, data_type)
|
2020-07-10 14:52:07 +02:00
|
|
|
entities.append(entity)
|
|
|
|
|
2020-07-13 23:24:28 +02:00
|
|
|
async_add_entities(entities)
|
2016-02-23 18:01:53 +01:00
|
|
|
|
2020-07-13 23:24:28 +02:00
|
|
|
@callback
|
2020-07-13 02:57:19 +02:00
|
|
|
def sensor_update(event, device_id):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Handle sensor updates from the RFXtrx gateway."""
|
2020-07-12 22:03:22 +02:00
|
|
|
if not supported(event):
|
2016-02-23 18:01:53 +01:00
|
|
|
return
|
|
|
|
|
2020-07-10 14:52:07 +02:00
|
|
|
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)
|
|
|
|
|
2020-07-12 22:03:22 +02:00
|
|
|
_LOGGER.info(
|
|
|
|
"Added sensor (Device ID: %s Class: %s Sub: %s, Event: %s)",
|
2020-07-10 14:52:07 +02:00
|
|
|
event.device.id_string.lower(),
|
|
|
|
event.device.__class__.__name__,
|
|
|
|
event.device.subtype,
|
2020-07-12 22:03:22 +02:00
|
|
|
"".join(f"{x:02x}" for x in event.data),
|
2020-07-10 14:52:07 +02:00
|
|
|
)
|
|
|
|
|
2020-07-13 02:57:19 +02:00
|
|
|
entity = RfxtrxSensor(event.device, device_id, data_type, event=event)
|
2020-07-13 23:24:28 +02:00
|
|
|
async_add_entities([entity])
|
2015-09-27 11:13:49 +02:00
|
|
|
|
2020-07-06 00:10:26 +02:00
|
|
|
# Subscribe to main RFXtrx events
|
2020-07-12 22:03:22 +02:00
|
|
|
if discovery_info[CONF_AUTOMATIC_ADD]:
|
2020-07-13 23:24:28 +02:00
|
|
|
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, sensor_update)
|
2015-07-23 19:36:05 +02:00
|
|
|
|
2015-09-29 08:20:25 +02:00
|
|
|
|
2020-07-18 13:43:38 +02:00
|
|
|
class RfxtrxSensor(RfxtrxEntity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a RFXtrx sensor."""
|
2015-07-23 19:36:05 +02:00
|
|
|
|
2020-07-13 02:57:19 +02:00
|
|
|
def __init__(self, device, device_id, data_type, event=None):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2020-07-18 13:43:38 +02:00
|
|
|
super().__init__(device, device_id, event=event)
|
2016-05-07 03:24:43 +02:00
|
|
|
self.data_type = data_type
|
2019-07-31 12:25:30 -07:00
|
|
|
self._unit_of_measurement = DATA_TYPES.get(data_type, "")
|
2020-07-18 13:43:38 +02:00
|
|
|
self._name = f"{device.type_string} {device.id_string} {data_type}"
|
2020-07-10 14:52:07 +02:00
|
|
|
self._unique_id = "_".join(x for x in (*self._device_id, data_type))
|
2015-07-23 19:36:05 +02:00
|
|
|
|
2020-07-10 21:47:37 +02:00
|
|
|
self._device_class = DEVICE_CLASSES.get(data_type)
|
|
|
|
self._convert_fun = CONVERT_FUNCTIONS.get(data_type, lambda x: x)
|
|
|
|
|
2015-07-23 19:36:05 +02:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the sensor."""
|
2020-07-17 10:27:07 +02:00
|
|
|
if not self._event:
|
2016-09-01 18:58:32 +02:00
|
|
|
return None
|
2020-07-17 10:27:07 +02:00
|
|
|
value = self._event.values.get(self.data_type)
|
2020-07-10 21:47:37 +02:00
|
|
|
return self._convert_fun(value)
|
2015-07-23 19:36:05 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the unit this state is expressed in."""
|
2015-07-23 19:36:05 +02:00
|
|
|
return self._unit_of_measurement
|
2020-06-28 06:59:42 +02:00
|
|
|
|
2020-07-18 01:23:49 +02:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def force_update(self) -> bool:
|
|
|
|
"""We should force updates. Repeated states have meaning."""
|
|
|
|
return True
|
|
|
|
|
2020-07-10 21:47:37 +02:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return a device class for sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2020-07-13 23:24:28 +02:00
|
|
|
@callback
|
2020-07-13 02:57:19 +02:00
|
|
|
def _handle_event(self, event, device_id):
|
2020-07-09 11:40:37 +02:00
|
|
|
"""Check if event applies to me and update."""
|
|
|
|
if not isinstance(event, SensorEvent):
|
|
|
|
return
|
|
|
|
|
2020-07-13 02:57:19 +02:00
|
|
|
if device_id != self._device_id:
|
2020-07-09 11:40:37 +02:00
|
|
|
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)
|
|
|
|
|
2020-07-13 23:24:28 +02:00
|
|
|
self.async_write_ha_state()
|