From 13abac101b1107012aa58620e6f547f193e78393 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 11 Jan 2022 13:59:08 +0100 Subject: [PATCH] Drop legacy support for sensor sum statistics (#63884) --- homeassistant/components/sensor/recorder.py | 37 +---------- tests/components/sensor/test_recorder.py | 72 ++++++++++----------- 2 files changed, 35 insertions(+), 74 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index ceb88d23265..50d09b207a0 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -65,16 +65,6 @@ from . import ( _LOGGER = logging.getLogger(__name__) -DEVICE_CLASS_STATISTICS: dict[str, dict[str, set[str]]] = { - STATE_CLASS_MEASUREMENT: { - # Deprecated, support will be removed in Home Assistant 2021.11 - SensorDeviceClass.ENERGY: {"sum"}, - SensorDeviceClass.GAS: {"sum"}, - SensorDeviceClass.MONETARY: {"sum"}, - }, - STATE_CLASS_TOTAL: {}, - STATE_CLASS_TOTAL_INCREASING: {}, -} DEFAULT_STATISTICS = { STATE_CLASS_MEASUREMENT: {"mean", "min", "max"}, STATE_CLASS_TOTAL: {"sum"}, @@ -375,13 +365,7 @@ def _wanted_statistics(sensor_states: list[State]) -> dict[str, set[str]]: wanted_statistics = {} for state in sensor_states: state_class = state.attributes[ATTR_STATE_CLASS] - device_class = state.attributes.get(ATTR_DEVICE_CLASS) - if device_class in DEVICE_CLASS_STATISTICS[state_class]: - wanted_statistics[state.entity_id] = DEVICE_CLASS_STATISTICS[state_class][ - device_class - ] - else: - wanted_statistics[state.entity_id] = DEFAULT_STATISTICS[state_class] + wanted_statistics[state.entity_id] = DEFAULT_STATISTICS[state_class] return wanted_statistics @@ -532,14 +516,6 @@ def _compile_statistics( # noqa: C901 _sum = last_stats[entity_id][0]["sum"] or 0.0 for fstate, state in fstates: - - # Deprecated, will be removed in Home Assistant 2021.11 - if ( - "last_reset" not in state.attributes - and state_class == STATE_CLASS_MEASUREMENT - ): - continue - reset = False if ( state_class != STATE_CLASS_TOTAL_INCREASING @@ -604,11 +580,6 @@ def _compile_statistics( # noqa: C901 else: new_state = fstate - # Deprecated, will be removed in Home Assistant 2021.11 - if last_reset is None and state_class == STATE_CLASS_MEASUREMENT: - # No valid updates - continue - if new_state is None or old_state is None: # No valid updates continue @@ -636,11 +607,7 @@ def list_statistic_ids(hass: HomeAssistant, statistic_type: str | None = None) - device_class = state.attributes.get(ATTR_DEVICE_CLASS) native_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - if device_class in DEVICE_CLASS_STATISTICS[state_class]: - provided_statistics = DEVICE_CLASS_STATISTICS[state_class][device_class] - else: - provided_statistics = DEFAULT_STATISTICS[state_class] - + provided_statistics = DEFAULT_STATISTICS[state_class] if statistic_type is not None and statistic_type not in provided_statistics: continue diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index faadb7140e2..155060222c8 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -36,7 +36,7 @@ BATTERY_SENSOR_ATTRIBUTES = { } ENERGY_SENSOR_ATTRIBUTES = { "device_class": "energy", - "state_class": "measurement", + "state_class": "total", "unit_of_measurement": "kWh", } NONE_SENSOR_ATTRIBUTES = { @@ -59,7 +59,7 @@ TEMPERATURE_SENSOR_ATTRIBUTES = { } GAS_SENSOR_ATTRIBUTES = { "device_class": "gas", - "state_class": "measurement", + "state_class": "total", "unit_of_measurement": "m³", } @@ -305,7 +305,7 @@ def test_compile_hourly_statistics_unsupported(hass_recorder, caplog, attributes assert "Error while processing event StatisticsTask" not in caplog.text -@pytest.mark.parametrize("state_class", ["measurement", "total"]) +@pytest.mark.parametrize("state_class", ["total"]) @pytest.mark.parametrize( "units,device_class,unit,display_unit,factor", [ @@ -431,7 +431,7 @@ def test_compile_hourly_sum_statistics_amount( assert "Detected new cycle for sensor.test1, value dropped" not in caplog.text -@pytest.mark.parametrize("state_class", ["measurement"]) +@pytest.mark.parametrize("state_class", ["total"]) @pytest.mark.parametrize( "device_class,unit,native_unit,factor", [ @@ -540,7 +540,7 @@ def test_compile_hourly_sum_statistics_amount_reset_every_state_change( assert "Error while processing event StatisticsTask" not in caplog.text -@pytest.mark.parametrize("state_class", ["measurement"]) +@pytest.mark.parametrize("state_class", ["total"]) @pytest.mark.parametrize( "device_class,unit,native_unit,factor", [ @@ -617,7 +617,7 @@ def test_compile_hourly_sum_statistics_amount_invalid_last_reset( assert "Ignoring invalid last reset 'festivus' for sensor.test1" in caplog.text -@pytest.mark.parametrize("state_class", ["measurement"]) +@pytest.mark.parametrize("state_class", ["total"]) @pytest.mark.parametrize( "device_class,unit,native_unit,factor", [ @@ -1101,21 +1101,15 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog): setup_component(hass, "sensor", {}) sns1_attr = { "device_class": "energy", - "state_class": "measurement", + "state_class": "total", "unit_of_measurement": "kWh", "last_reset": None, } sns2_attr = {"device_class": "energy"} sns3_attr = {} - sns4_attr = { - "device_class": "energy", - "state_class": "measurement", - "unit_of_measurement": "kWh", - } seq1 = [10, 15, 20, 10, 30, 40, 50, 60, 70] seq2 = [110, 120, 130, 0, 30, 45, 55, 65, 75] seq3 = [0, 0, 5, 10, 30, 50, 60, 80, 90] - seq4 = [0, 0, 5, 10, 30, 50, 60, 80, 90] four, eight, states = record_meter_states( hass, period0, "sensor.test1", sns1_attr, seq1 @@ -1124,8 +1118,6 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog): states = {**states, **_states} _, _, _states = record_meter_states(hass, period0, "sensor.test3", sns3_attr, seq3) states = {**states, **_states} - _, _, _states = record_meter_states(hass, period0, "sensor.test4", sns4_attr, seq4) - states = {**states, **_states} hist = history.get_significant_states( hass, period0 - timedelta.resolution, eight + timedelta.resolution @@ -1204,11 +1196,9 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "unit_of_measurement": "Wh", "last_reset": None, } - sns4_attr = {**ENERGY_SENSOR_ATTRIBUTES} seq1 = [10, 15, 20, 10, 30, 40, 50, 60, 70] seq2 = [110, 120, 130, 0, 30, 45, 55, 65, 75] seq3 = [0, 0, 5, 10, 30, 50, 60, 80, 90] - seq4 = [0, 0, 5, 10, 30, 50, 60, 80, 90] four, eight, states = record_meter_states( hass, period0, "sensor.test1", sns1_attr, seq1 @@ -1217,8 +1207,6 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): states = {**states, **_states} _, _, _states = record_meter_states(hass, period0, "sensor.test3", sns3_attr, seq3) states = {**states, **_states} - _, _, _states = record_meter_states(hass, period0, "sensor.test4", sns4_attr, seq4) - states = {**states, **_states} hist = history.get_significant_states( hass, period0 - timedelta.resolution, eight + timedelta.resolution ) @@ -1523,29 +1511,35 @@ def test_compile_hourly_statistics_fails(hass_recorder, caplog): @pytest.mark.parametrize( - "device_class,unit,native_unit,statistic_type", + "state_class,device_class,unit,native_unit,statistic_type", [ - ("battery", "%", "%", "mean"), - ("battery", None, None, "mean"), - ("energy", "Wh", "kWh", "sum"), - ("energy", "kWh", "kWh", "sum"), - ("humidity", "%", "%", "mean"), - ("humidity", None, None, "mean"), - ("monetary", "USD", "USD", "sum"), - ("monetary", "None", "None", "sum"), - ("gas", "m³", "m³", "sum"), - ("gas", "ft³", "m³", "sum"), - ("pressure", "Pa", "Pa", "mean"), - ("pressure", "hPa", "Pa", "mean"), - ("pressure", "mbar", "Pa", "mean"), - ("pressure", "inHg", "Pa", "mean"), - ("pressure", "psi", "Pa", "mean"), - ("temperature", "°C", "°C", "mean"), - ("temperature", "°F", "°C", "mean"), + ("measurement", "battery", "%", "%", "mean"), + ("measurement", "battery", None, None, "mean"), + ("total", "energy", "Wh", "kWh", "sum"), + ("total", "energy", "kWh", "kWh", "sum"), + ("measurement", "energy", "Wh", "kWh", "mean"), + ("measurement", "energy", "kWh", "kWh", "mean"), + ("measurement", "humidity", "%", "%", "mean"), + ("measurement", "humidity", None, None, "mean"), + ("total", "monetary", "USD", "USD", "sum"), + ("total", "monetary", "None", "None", "sum"), + ("total", "gas", "m³", "m³", "sum"), + ("total", "gas", "ft³", "m³", "sum"), + ("measurement", "monetary", "USD", "USD", "mean"), + ("measurement", "monetary", "None", "None", "mean"), + ("measurement", "gas", "m³", "m³", "mean"), + ("measurement", "gas", "ft³", "m³", "mean"), + ("measurement", "pressure", "Pa", "Pa", "mean"), + ("measurement", "pressure", "hPa", "Pa", "mean"), + ("measurement", "pressure", "mbar", "Pa", "mean"), + ("measurement", "pressure", "inHg", "Pa", "mean"), + ("measurement", "pressure", "psi", "Pa", "mean"), + ("measurement", "temperature", "°C", "°C", "mean"), + ("measurement", "temperature", "°F", "°C", "mean"), ], ) def test_list_statistic_ids( - hass_recorder, caplog, device_class, unit, native_unit, statistic_type + hass_recorder, caplog, state_class, device_class, unit, native_unit, statistic_type ): """Test listing future statistic ids.""" hass = hass_recorder() @@ -1553,7 +1547,7 @@ def test_list_statistic_ids( attributes = { "device_class": device_class, "last_reset": 0, - "state_class": "measurement", + "state_class": state_class, "unit_of_measurement": unit, } hass.states.set("sensor.test1", 0, attributes=attributes)