Skip to content

Commit

Permalink
Add missing last_reported_timestamp to LazyState (home-assistant#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 9, 2024
1 parent be34d30 commit f177336
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions homeassistant/components/recorder/models/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ def last_changed_timestamp(self) -> float: # type: ignore[override]
assert ts is not None
return ts

@cached_property
def last_reported_timestamp(self) -> float: # type: ignore[override]
"""Last reported timestamp."""
ts = self._last_reported_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.
Expand Down
37 changes: 37 additions & 0 deletions tests/components/recorder/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ async def test_lazy_state_handles_different_last_updated_and_last_changed(
state="off",
attributes='{"shared":true}',
last_updated_ts=now.timestamp(),
last_reported_ts=now.timestamp(),
last_changed_ts=(now - timedelta(seconds=60)).timestamp(),
)
lstate = LazyState(
Expand All @@ -339,6 +340,7 @@ async def test_lazy_state_handles_different_last_updated_and_last_changed(
}
assert lstate.last_updated.timestamp() == row.last_updated_ts
assert lstate.last_changed.timestamp() == row.last_changed_ts
assert lstate.last_reported.timestamp() == row.last_updated_ts
assert lstate.as_dict() == {
"attributes": {"shared": True},
"entity_id": "sensor.valid",
Expand All @@ -348,6 +350,7 @@ async def test_lazy_state_handles_different_last_updated_and_last_changed(
}
assert lstate.last_changed_timestamp == row.last_changed_ts
assert lstate.last_updated_timestamp == row.last_updated_ts
assert lstate.last_reported_timestamp == row.last_updated_ts


async def test_lazy_state_handles_same_last_updated_and_last_changed(
Expand All @@ -361,6 +364,7 @@ async def test_lazy_state_handles_same_last_updated_and_last_changed(
attributes='{"shared":true}',
last_updated_ts=now.timestamp(),
last_changed_ts=now.timestamp(),
last_reported_ts=None,
)
lstate = LazyState(
row, {}, None, row.entity_id, row.state, row.last_updated_ts, False
Expand All @@ -374,6 +378,7 @@ async def test_lazy_state_handles_same_last_updated_and_last_changed(
}
assert lstate.last_updated.timestamp() == row.last_updated_ts
assert lstate.last_changed.timestamp() == row.last_changed_ts
assert lstate.last_reported.timestamp() == row.last_updated_ts
assert lstate.as_dict() == {
"attributes": {"shared": True},
"entity_id": "sensor.valid",
Expand All @@ -383,3 +388,35 @@ async def test_lazy_state_handles_same_last_updated_and_last_changed(
}
assert lstate.last_changed_timestamp == row.last_changed_ts
assert lstate.last_updated_timestamp == row.last_updated_ts
assert lstate.last_reported_timestamp == row.last_updated_ts


async def test_lazy_state_handles_different_last_reported(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that the LazyState handles last_reported different from last_updated."""
now = datetime(2021, 6, 12, 3, 4, 1, 323, tzinfo=dt_util.UTC)
row = PropertyMock(
entity_id="sensor.valid",
state="off",
attributes='{"shared":true}',
last_updated_ts=(now - timedelta(seconds=60)).timestamp(),
last_reported_ts=now.timestamp(),
last_changed_ts=(now - timedelta(seconds=60)).timestamp(),
)
lstate = LazyState(
row, {}, None, row.entity_id, row.state, row.last_updated_ts, False
)
assert lstate.as_dict() == {
"attributes": {"shared": True},
"entity_id": "sensor.valid",
"last_changed": "2021-06-12T03:03:01.000323+00:00",
"last_updated": "2021-06-12T03:03:01.000323+00:00",
"state": "off",
}
assert lstate.last_updated.timestamp() == row.last_updated_ts
assert lstate.last_changed.timestamp() == row.last_changed_ts
assert lstate.last_reported.timestamp() == row.last_reported_ts
assert lstate.last_changed_timestamp == row.last_changed_ts
assert lstate.last_updated_timestamp == row.last_updated_ts
assert lstate.last_reported_timestamp == row.last_reported_ts

0 comments on commit f177336

Please sign in to comment.