Ignore old_state when using delta_values (#68402)

* delta value updates don't require old_state

* add test

* merge
This commit is contained in:
Diogo Gomes 2022-04-01 10:08:00 +01:00 committed by GitHub
parent 617f459b57
commit 2963aea3ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 8 deletions

View File

@ -288,10 +288,15 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
sensor.start(source_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)) sensor.start(source_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT))
if ( if (
old_state is None new_state is None
or new_state is None
or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE]
or new_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] 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 return
@ -309,6 +314,9 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
self._state += adjustment self._state += adjustment
except DecimalException as err: except DecimalException as err:
if self._sensor_delta_values:
_LOGGER.warning("Invalid adjustment of %s: %s", new_state.state, err)
else:
_LOGGER.warning( _LOGGER.warning(
"Invalid state (%s > %s): %s", old_state.state, new_state.state, err "Invalid state (%s > %s): %s", old_state.state, new_state.state, err
) )

View File

@ -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.""" """Test utility sensor state."""
if yaml_config: if yaml_config:
assert await async_setup_component(hass, DOMAIN, 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() 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") state = hass.states.get("sensor.energy_bill")
assert state is not None 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.""" """Test utility meter "delta_values" mode."""
now = dt_util.utcnow() now = dt_util.utcnow()
with alter_time(now): 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") state = hass.states.get("sensor.energy_bill")
assert state.attributes.get("status") == PAUSED 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) now += timedelta(seconds=30)
with alter_time(now): with alter_time(now):
async_fire_time_changed(hass, now) async_fire_time_changed(hass, now)