Add rfxtrx device classes to known types (#37698)
* Add device classes to known types * Update homeassistant/components/rfxtrx/sensor.py Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>pull/37731/head^2
parent
91271f388c
commit
5255bf20d3
|
@ -75,8 +75,8 @@ DATA_TYPES = OrderedDict(
|
|||
("Energy usage", ""),
|
||||
("Voltage", ""),
|
||||
("Current", ""),
|
||||
("Battery numeric", ""),
|
||||
("Rssi numeric", ""),
|
||||
("Battery numeric", UNIT_PERCENTAGE),
|
||||
("Rssi numeric", "dBm"),
|
||||
]
|
||||
)
|
||||
|
||||
|
|
|
@ -4,7 +4,13 @@ import logging
|
|||
from RFXtrx import SensorEvent
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.components.sensor import (
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
PLATFORM_SCHEMA,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_DEVICES, CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
@ -40,6 +46,34 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the RFXtrx platform."""
|
||||
data_ids = set()
|
||||
|
@ -115,6 +149,9 @@ class RfxtrxSensor(Entity):
|
|||
self._device_id = get_device_id(device)
|
||||
self._unique_id = "_".join(x for x in (*self._device_id, data_type))
|
||||
|
||||
self._device_class = DEVICE_CLASSES.get(data_type)
|
||||
self._convert_fun = CONVERT_FUNCTIONS.get(data_type, lambda x: x)
|
||||
|
||||
if event:
|
||||
self._apply_event(event)
|
||||
|
||||
|
@ -137,7 +174,8 @@ class RfxtrxSensor(Entity):
|
|||
"""Return the state of the sensor."""
|
||||
if not self.event:
|
||||
return None
|
||||
return self.event.values.get(self.data_type)
|
||||
value = self.event.values.get(self.data_type)
|
||||
return self._convert_fun(value)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -156,6 +194,11 @@ class RfxtrxSensor(Entity):
|
|||
"""Return the unit this state is expressed in."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return a device class for sensor."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique identifier of remote device."""
|
||||
|
|
|
@ -80,13 +80,13 @@ async def test_one_sensor_no_datatype(hass, rfxtrx):
|
|||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Rssi numeric"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.attributes.get("unit_of_measurement") == "dBm"
|
||||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Battery numeric"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
|
||||
async def test_several_sensors(hass, rfxtrx):
|
||||
|
@ -156,8 +156,8 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "7"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.state == "-64"
|
||||
assert state.attributes.get("unit_of_measurement") == "dBm"
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
|
@ -166,8 +166,8 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "9"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.state == "90"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
# 2
|
||||
await _signal_event(hass, "0a52080405020095240279")
|
||||
|
@ -185,8 +185,8 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "7"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.state == "-64"
|
||||
assert state.attributes.get("unit_of_measurement") == "dBm"
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
|
@ -195,8 +195,8 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "9"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.state == "90"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
# 1 Update
|
||||
await _signal_event(hass, "0a52085e070100b31b0279")
|
||||
|
@ -214,8 +214,8 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "7"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.state == "-64"
|
||||
assert state.attributes.get("unit_of_measurement") == "dBm"
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
|
@ -224,8 +224,8 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "9"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
assert state.state == "90"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
assert len(hass.states.async_all()) == 10
|
||||
|
||||
|
|
Loading…
Reference in New Issue