Skip to content

Commit

Permalink
Fix repr for States and Events without a timestamp (home-assistant#86391
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bdraco authored Jan 22, 2023
1 parent 711c92a commit 52ea64d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
22 changes: 12 additions & 10 deletions homeassistant/components/recorder/db_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,17 @@ def __repr__(self) -> str:
return (
"<recorder.Events("
f"id={self.event_id}, type='{self.event_type}', "
f"origin_idx='{self.origin_idx}', time_fired='{self.time_fired_isotime}'"
f"origin_idx='{self.origin_idx}', time_fired='{self._time_fired_isotime}'"
f", data_id={self.data_id})>"
)

@property
def time_fired_isotime(self) -> str:
def _time_fired_isotime(self) -> str:
"""Return time_fired as an isotime string."""
date_time = dt_util.utc_from_timestamp(self.time_fired_ts) or process_timestamp(
self.time_fired
)
if self.time_fired_ts is not None:
date_time = dt_util.utc_from_timestamp(self.time_fired_ts)
else:
date_time = process_timestamp(self.time_fired)
return date_time.isoformat(sep=" ", timespec="seconds")

@staticmethod
Expand Down Expand Up @@ -307,16 +308,17 @@ def __repr__(self) -> str:
return (
f"<recorder.States(id={self.state_id}, entity_id='{self.entity_id}',"
f" state='{self.state}', event_id='{self.event_id}',"
f" last_updated='{self.last_updated_isotime}',"
f" last_updated='{self._last_updated_isotime}',"
f" old_state_id={self.old_state_id}, attributes_id={self.attributes_id})>"
)

@property
def last_updated_isotime(self) -> str:
def _last_updated_isotime(self) -> str:
"""Return last_updated as an isotime string."""
date_time = dt_util.utc_from_timestamp(
self.last_updated_ts
) or process_timestamp(self.last_updated)
if self.last_updated_ts is not None:
date_time = dt_util.utc_from_timestamp(self.last_updated_ts)
else:
date_time = process_timestamp(self.last_updated)
return date_time.isoformat(sep=" ", timespec="seconds")

@staticmethod
Expand Down
34 changes: 34 additions & 0 deletions tests/components/recorder/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,40 @@ def test_repr():
assert "2016-07-09 11:00:00+00:00" in repr(Events.from_event(event))


def test_states_repr_without_timestamp():
"""Test repr for a state without last_updated_ts."""
fixed_time = datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC, microsecond=432432)
states = States(
entity_id="sensor.temp",
attributes=None,
context_id=None,
context_user_id=None,
context_parent_id=None,
origin_idx=None,
last_updated=fixed_time,
last_changed=fixed_time,
last_updated_ts=None,
last_changed_ts=None,
)
assert "2016-07-09 11:00:00+00:00" in repr(states)


def test_events_repr_without_timestamp():
"""Test repr for an event without time_fired_ts."""
fixed_time = datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC, microsecond=432432)
events = Events(
event_type="any",
event_data=None,
origin_idx=None,
time_fired=fixed_time,
time_fired_ts=None,
context_id=None,
context_user_id=None,
context_parent_id=None,
)
assert "2016-07-09 11:00:00+00:00" in repr(events)


def test_handling_broken_json_state_attributes(caplog):
"""Test we handle broken json in state attributes."""
state_attributes = StateAttributes(
Expand Down

0 comments on commit 52ea64d

Please sign in to comment.