From 2963aea3ecf15530187416aa73d7fc6519e21853 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Fri, 1 Apr 2022 10:08:00 +0100 Subject: [PATCH] Ignore old_state when using delta_values (#68402) * delta value updates don't require old_state * add test * merge --- .../components/utility_meter/sensor.py | 20 +++++++++----- tests/components/utility_meter/test_sensor.py | 27 +++++++++++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index c3d2be63a4b..92656fd0769 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -288,10 +288,15 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity): sensor.start(source_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)) if ( - old_state is None - or new_state is None - or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] + new_state is None or new_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] + or ( + not self._sensor_delta_values + and ( + old_state is None + or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] + ) + ) ): return @@ -309,9 +314,12 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity): self._state += adjustment except DecimalException as err: - _LOGGER.warning( - "Invalid state (%s > %s): %s", old_state.state, new_state.state, err - ) + if self._sensor_delta_values: + _LOGGER.warning("Invalid adjustment of %s: %s", new_state.state, err) + else: + _LOGGER.warning( + "Invalid state (%s > %s): %s", old_state.state, new_state.state, err + ) self.async_write_ha_state() @callback diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index 1b8328f5a62..2ee2f0b1c74 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -587,7 +587,7 @@ async def test_net_consumption(hass, yaml_config, config_entry_config): ), ), ) -async def test_non_net_consumption(hass, yaml_config, config_entry_config): +async def test_non_net_consumption(hass, yaml_config, config_entry_config, caplog): """Test utility sensor state.""" if yaml_config: assert await async_setup_component(hass, DOMAIN, yaml_config) @@ -621,6 +621,17 @@ async def test_non_net_consumption(hass, yaml_config, config_entry_config): ) await hass.async_block_till_done() + now = dt_util.utcnow() + timedelta(seconds=10) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.states.async_set( + entity_id, + None, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, + force_update=True, + ) + await hass.async_block_till_done() + assert "Invalid state " in caplog.text + state = hass.states.get("sensor.energy_bill") assert state is not None @@ -655,7 +666,7 @@ async def test_non_net_consumption(hass, yaml_config, config_entry_config): ), ), ) -async def test_delta_values(hass, yaml_config, config_entry_config): +async def test_delta_values(hass, yaml_config, config_entry_config, caplog): """Test utility meter "delta_values" mode.""" now = dt_util.utcnow() with alter_time(now): @@ -686,6 +697,18 @@ async def test_delta_values(hass, yaml_config, config_entry_config): state = hass.states.get("sensor.energy_bill") assert state.attributes.get("status") == PAUSED + now += timedelta(seconds=30) + with alter_time(now): + async_fire_time_changed(hass, now) + hass.states.async_set( + entity_id, + None, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, + force_update=True, + ) + await hass.async_block_till_done() + assert "Invalid adjustment of None" in caplog.text + now += timedelta(seconds=30) with alter_time(now): async_fire_time_changed(hass, now)