From a391b8dd9d212caeaf597126162d2c42928471c2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 11 Oct 2022 13:51:28 +0200 Subject: [PATCH] Support correcting sensor volume unit (#80081) --- homeassistant/components/sensor/recorder.py | 9 ++++++++- tests/components/sensor/test_recorder.py | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index ba4924589f4..7bb2a998b9e 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -23,7 +23,12 @@ from homeassistant.components.recorder.models import ( StatisticMetaData, StatisticResult, ) -from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, REVOLUTIONS_PER_MINUTE +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + REVOLUTIONS_PER_MINUTE, + VOLUME_CUBIC_FEET, + VOLUME_CUBIC_METERS, +) from homeassistant.core import HomeAssistant, State, split_entity_id from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity import entity_sources @@ -49,6 +54,8 @@ DEFAULT_STATISTICS = { EQUIVALENT_UNITS = { "RPM": REVOLUTIONS_PER_MINUTE, + "ft3": VOLUME_CUBIC_FEET, + "m3": VOLUME_CUBIC_METERS, } # Keep track of entities for which a warning about decreasing value has been logged diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index 73804e83d94..4b0db42c618 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -1912,6 +1912,9 @@ def test_list_statistic_ids_unsupported(hass_recorder, caplog, _attributes): ("battery", "%", "cats", None, 13.050847, -10, 30), ("battery", None, "cats", None, 13.050847, -10, 30), (None, "kW", "Wh", "power", 13.050847, -10, 30), + # Can't downgrade from ft³ to ft3 or from m³ to m3 + (None, "ft³", "ft3", "volume", 13.050847, -10, 30), + (None, "m³", "m3", "volume", 13.050847, -10, 30), ], ) def test_compile_hourly_statistics_changing_units_1( @@ -2193,10 +2196,12 @@ def test_compile_hourly_statistics_changing_units_3( @pytest.mark.parametrize( - "device_class, state_unit, state_unit2, unit_class, mean, mean2, min, max", + "device_class, state_unit, state_unit2, unit_class, unit_class2, mean, mean2, min, max", [ - (None, "RPM", "rpm", None, 13.050847, 13.333333, -10, 30), - (None, "rpm", "RPM", None, 13.050847, 13.333333, -10, 30), + (None, "RPM", "rpm", None, None, 13.050847, 13.333333, -10, 30), + (None, "rpm", "RPM", None, None, 13.050847, 13.333333, -10, 30), + (None, "ft3", "ft³", None, "volume", 13.050847, 13.333333, -10, 30), + (None, "m3", "m³", None, "volume", 13.050847, 13.333333, -10, 30), ], ) def test_compile_hourly_statistics_equivalent_units_1( @@ -2206,6 +2211,7 @@ def test_compile_hourly_statistics_equivalent_units_1( state_unit, state_unit2, unit_class, + unit_class2, mean, mean2, min, @@ -2277,7 +2283,7 @@ def test_compile_hourly_statistics_equivalent_units_1( "name": None, "source": "recorder", "statistics_unit_of_measurement": state_unit2, - "unit_class": unit_class, + "unit_class": unit_class2, }, ] stats = statistics_during_period(hass, zero, period="5minute") @@ -2317,6 +2323,8 @@ def test_compile_hourly_statistics_equivalent_units_1( [ (None, "RPM", "rpm", None, 13.333333, -10, 30), (None, "rpm", "RPM", None, 13.333333, -10, 30), + (None, "ft3", "ft³", None, 13.333333, -10, 30), + (None, "m3", "m³", None, 13.333333, -10, 30), ], ) def test_compile_hourly_statistics_equivalent_units_2(