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))
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

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."""
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)