diff --git a/homeassistant/components/recorder/models/state.py b/homeassistant/components/recorder/models/state.py index 89281a85c15..f5e49881b8f 100644 --- a/homeassistant/components/recorder/models/state.py +++ b/homeassistant/components/recorder/models/state.py @@ -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. diff --git a/tests/components/recorder/test_models.py b/tests/components/recorder/test_models.py index 9078b2e861c..a0703f1f2c5 100644 --- a/tests/components/recorder/test_models.py +++ b/tests/components/recorder/test_models.py @@ -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