From 0fbeb3bf7b32555ab1f552fd0707e21b02e3ec3d Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Fri, 25 Sep 2020 14:27:38 +0200 Subject: [PATCH] Use HA constants for Rfxtrx units (#40562) * Add more units and device classes * Battery level is a value beteen 0 and 9 where 9 indicate full Actual 0 battery can't occur since since then we would get to signal * Adjust tests * Add wind direction and adjust rain rate * Adjust data types to be None rather than empty string * Set counter values to count unit * Forgotten unit --- homeassistant/components/rfxtrx/__init__.py | 50 ++++++++++++--------- homeassistant/components/rfxtrx/sensor.py | 22 +++++++-- tests/components/rfxtrx/test_sensor.py | 14 +++--- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 6082d54df12..eb13800f748 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -18,11 +18,19 @@ from homeassistant.const import ( CONF_DEVICES, CONF_HOST, CONF_PORT, + DEGREE, + ELECTRICAL_CURRENT_AMPERE, + ENERGY_KILO_WATT_HOUR, EVENT_HOMEASSISTANT_STOP, + LENGTH_MILLIMETERS, PERCENTAGE, POWER_WATT, + PRESSURE_HPA, + SPEED_METERS_PER_SECOND, TEMP_CELSIUS, + TIME_HOURS, UV_INDEX, + VOLT, ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv @@ -52,30 +60,28 @@ DATA_TYPES = OrderedDict( ("Temperature", TEMP_CELSIUS), ("Temperature2", TEMP_CELSIUS), ("Humidity", PERCENTAGE), - ("Barometer", ""), - ("Wind direction", ""), - ("Rain rate", ""), + ("Barometer", PRESSURE_HPA), + ("Wind direction", DEGREE), + ("Rain rate", f"{LENGTH_MILLIMETERS}/{TIME_HOURS}"), ("Energy usage", POWER_WATT), - ("Total usage", POWER_WATT), - ("Sound", ""), - ("Sensor Status", ""), - ("Counter value", ""), + ("Total usage", ENERGY_KILO_WATT_HOUR), + ("Sound", None), + ("Sensor Status", None), + ("Counter value", "count"), ("UV", UV_INDEX), - ("Humidity status", ""), - ("Forecast", ""), - ("Forecast numeric", ""), - ("Rain total", ""), - ("Wind average speed", ""), - ("Wind gust", ""), - ("Chill", ""), - ("Total usage", ""), - ("Count", ""), - ("Current Ch. 1", ""), - ("Current Ch. 2", ""), - ("Current Ch. 3", ""), - ("Energy usage", ""), - ("Voltage", ""), - ("Current", ""), + ("Humidity status", None), + ("Forecast", None), + ("Forecast numeric", None), + ("Rain total", LENGTH_MILLIMETERS), + ("Wind average speed", SPEED_METERS_PER_SECOND), + ("Wind gust", SPEED_METERS_PER_SECOND), + ("Chill", TEMP_CELSIUS), + ("Count", "count"), + ("Current Ch. 1", ELECTRICAL_CURRENT_AMPERE), + ("Current Ch. 2", ELECTRICAL_CURRENT_AMPERE), + ("Current Ch. 3", ELECTRICAL_CURRENT_AMPERE), + ("Voltage", VOLT), + ("Current", ELECTRICAL_CURRENT_AMPERE), ("Battery numeric", PERCENTAGE), ("Rssi numeric", "dBm"), ] diff --git a/homeassistant/components/rfxtrx/sensor.py b/homeassistant/components/rfxtrx/sensor.py index 4acde6b0450..81e0c60e055 100644 --- a/homeassistant/components/rfxtrx/sensor.py +++ b/homeassistant/components/rfxtrx/sensor.py @@ -9,7 +9,14 @@ from homeassistant.components.sensor import ( DEVICE_CLASS_SIGNAL_STRENGTH, DEVICE_CLASS_TEMPERATURE, ) -from homeassistant.const import CONF_DEVICES +from homeassistant.const import ( + CONF_DEVICES, + DEVICE_CLASS_CURRENT, + DEVICE_CLASS_ENERGY, + DEVICE_CLASS_POWER, + DEVICE_CLASS_PRESSURE, + DEVICE_CLASS_VOLTAGE, +) from homeassistant.core import callback from . import ( @@ -30,7 +37,7 @@ def _battery_convert(value): """Battery is given as a value between 0 and 9.""" if value is None: return None - return value * 10 + return (value + 1) * 10 def _rssi_convert(value): @@ -41,10 +48,17 @@ def _rssi_convert(value): DEVICE_CLASSES = { + "Barometer": DEVICE_CLASS_PRESSURE, "Battery numeric": DEVICE_CLASS_BATTERY, - "Rssi numeric": DEVICE_CLASS_SIGNAL_STRENGTH, + "Current Ch. 1": DEVICE_CLASS_CURRENT, + "Current Ch. 2": DEVICE_CLASS_CURRENT, + "Current Ch. 3": DEVICE_CLASS_CURRENT, + "Energy usage": DEVICE_CLASS_POWER, "Humidity": DEVICE_CLASS_HUMIDITY, + "Rssi numeric": DEVICE_CLASS_SIGNAL_STRENGTH, "Temperature": DEVICE_CLASS_TEMPERATURE, + "Total usage": DEVICE_CLASS_ENERGY, + "Voltage": DEVICE_CLASS_VOLTAGE, } @@ -124,7 +138,7 @@ class RfxtrxSensor(RfxtrxEntity): """Initialize the sensor.""" super().__init__(device, device_id, event=event) self.data_type = data_type - self._unit_of_measurement = DATA_TYPES.get(data_type, "") + self._unit_of_measurement = DATA_TYPES.get(data_type) self._name = f"{device.type_string} {device.id_string} {data_type}" self._unique_id = "_".join(x for x in (*self._device_id, data_type)) diff --git a/tests/components/rfxtrx/test_sensor.py b/tests/components/rfxtrx/test_sensor.py index 0186d403245..d0100e4ea14 100644 --- a/tests/components/rfxtrx/test_sensor.py +++ b/tests/components/rfxtrx/test_sensor.py @@ -87,7 +87,7 @@ async def test_one_sensor_no_datatype(hass, rfxtrx): assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == f"{base_name} Humidity status" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None state = hass.states.get(f"{base_id}_rssi_numeric") assert state @@ -164,7 +164,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "normal" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None state = hass.states.get(f"{base_id}_rssi_numeric") assert state @@ -178,7 +178,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_battery_numeric") assert state - assert state.state == "90" + assert state.state == "100" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE # 2 @@ -193,7 +193,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "normal" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None state = hass.states.get(f"{base_id}_rssi_numeric") assert state @@ -207,7 +207,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_battery_numeric") assert state - assert state.state == "90" + assert state.state == "100" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE # 1 Update @@ -222,7 +222,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "normal" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None state = hass.states.get(f"{base_id}_rssi_numeric") assert state @@ -236,7 +236,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_battery_numeric") assert state - assert state.state == "90" + assert state.state == "100" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert len(hass.states.async_all()) == 10