Fix LazyState compatibility with State under_cached_property change (#132752)

This commit is contained in:
J. Nick Koston 2024-12-09 21:41:08 +01:00 committed by GitHub
parent aa7b69afd4
commit e4ba94f939
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View File

@ -96,6 +96,21 @@ class LazyState(State):
assert self._last_updated_ts is not None
return dt_util.utc_from_timestamp(self._last_updated_ts)
@cached_property
def last_updated_timestamp(self) -> float: # type: ignore[override]
"""Last updated timestamp."""
if TYPE_CHECKING:
assert self._last_updated_ts is not None
return self._last_updated_ts
@cached_property
def last_changed_timestamp(self) -> float: # type: ignore[override]
"""Last changed timestamp."""
ts = self._last_changed_ts or self._last_updated_ts
if TYPE_CHECKING:
assert ts is not None
return ts
def as_dict(self) -> dict[str, Any]: # type: ignore[override]
"""Return a dict representation of the LazyState.

View File

@ -346,6 +346,8 @@ async def test_lazy_state_handles_different_last_updated_and_last_changed(
"last_updated": "2021-06-12T03:04:01.000323+00:00",
"state": "off",
}
assert lstate.last_changed_timestamp == row.last_changed_ts
assert lstate.last_updated_timestamp == row.last_updated_ts
async def test_lazy_state_handles_same_last_updated_and_last_changed(
@ -379,3 +381,5 @@ async def test_lazy_state_handles_same_last_updated_and_last_changed(
"last_updated": "2021-06-12T03:04:01.000323+00:00",
"state": "off",
}
assert lstate.last_changed_timestamp == row.last_changed_ts
assert lstate.last_updated_timestamp == row.last_updated_ts