Bugfix: also schedule time based integration when source is 0 (#133438)

* Bugfix also schedule time based integration when source is 0

* Update tests/components/integration/test_sensor.py

Co-authored-by: Diogo Gomes <diogogomes@gmail.com>

* Improve comment in test. Remove redundant assertion.

---------

Co-authored-by: Diogo Gomes <diogogomes@gmail.com>
This commit is contained in:
Ron Weikamp 2024-12-18 10:41:46 +01:00 committed by Franck Nijhof
parent 8400ef8441
commit 59e6fa5138
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 34 additions and 1 deletions

View File

@ -576,7 +576,7 @@ class IntegrationSensor(RestoreSensor):
if (
self._max_sub_interval is not None
and source_state is not None
and (source_state_dec := _decimal_state(source_state.state))
and (source_state_dec := _decimal_state(source_state.state)) is not None
):
@callback

View File

@ -843,6 +843,39 @@ async def test_on_valid_source_expect_update_on_time(
assert float(state.state) < 1.8
async def test_on_0_source_expect_0_and_update_when_source_gets_positive(
hass: HomeAssistant,
) -> None:
"""Test whether time based integration updates the integral on a valid zero source."""
start_time = dt_util.utcnow()
with freeze_time(start_time) as freezer:
await _setup_integral_sensor(hass, max_sub_interval=DEFAULT_MAX_SUB_INTERVAL)
await _update_source_sensor(hass, 0)
await hass.async_block_till_done()
# wait one minute and one second
freezer.tick(61)
async_fire_time_changed(hass, dt_util.now())
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
assert condition.async_numeric_state(hass, state) is True
assert float(state.state) == 0 # integral is 0 after integration of 0
# wait one second and update state
freezer.tick(1)
async_fire_time_changed(hass, dt_util.now())
await _update_source_sensor(hass, 100)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
# approx 100*1/3600 (right method after 1 second since last integration)
assert 0.027 < float(state.state) < 0.029
async def test_on_unvailable_source_expect_no_update_on_time(
hass: HomeAssistant,
) -> None: