mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Fix repr for States and Events without a timestamp (#86391)
This commit is contained in:
parent
711c92a87f
commit
52ea64d1d0
@ -171,16 +171,17 @@ class Events(Base): # type: ignore[misc,valid-type]
|
||||
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
|
||||
@ -307,16 +308,17 @@ class States(Base): # type: ignore[misc,valid-type]
|
||||
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
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user