Fix last_reported_timestamp not being updated when last_reported is changed (#118341)

* Reduce number of calls to last_reported_timestamp

When a state is created, last_update is always the same
as last_reported, and we only update it later if it changes
so we can pre-set the cached property to avoid it being
run when the recorder accesses it later.

* fix cache not being overridden

* coverage
This commit is contained in:
J. Nick Koston
2024-05-28 19:03:19 -10:00
committed by GitHub
parent f3fa843b9d
commit 76aa504e36
2 changed files with 26 additions and 5 deletions

View File

@@ -3524,3 +3524,18 @@ async def test_set_time_zone_deprecated(hass: HomeAssistant) -> None:
),
):
await hass.config.set_time_zone("America/New_York")
async def test_async_set_updates_last_reported(hass: HomeAssistant) -> None:
"""Test async_set method updates last_reported AND last_reported_timestamp."""
hass.states.async_set("light.bowl", "on", {})
state = hass.states.get("light.bowl")
last_reported = state.last_reported
last_reported_timestamp = state.last_reported_timestamp
for _ in range(2):
hass.states.async_set("light.bowl", "on", {})
assert state.last_reported != last_reported
assert state.last_reported_timestamp != last_reported_timestamp
last_reported = state.last_reported
last_reported_timestamp = state.last_reported_timestamp