mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +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 (
|
return (
|
||||||
"<recorder.Events("
|
"<recorder.Events("
|
||||||
f"id={self.event_id}, type='{self.event_type}', "
|
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})>"
|
f", data_id={self.data_id})>"
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def time_fired_isotime(self) -> str:
|
def _time_fired_isotime(self) -> str:
|
||||||
"""Return time_fired as an isotime string."""
|
"""Return time_fired as an isotime string."""
|
||||||
date_time = dt_util.utc_from_timestamp(self.time_fired_ts) or process_timestamp(
|
if self.time_fired_ts is not None:
|
||||||
self.time_fired
|
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")
|
return date_time.isoformat(sep=" ", timespec="seconds")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -307,16 +308,17 @@ class States(Base): # type: ignore[misc,valid-type]
|
|||||||
return (
|
return (
|
||||||
f"<recorder.States(id={self.state_id}, entity_id='{self.entity_id}',"
|
f"<recorder.States(id={self.state_id}, entity_id='{self.entity_id}',"
|
||||||
f" state='{self.state}', event_id='{self.event_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})>"
|
f" old_state_id={self.old_state_id}, attributes_id={self.attributes_id})>"
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def last_updated_isotime(self) -> str:
|
def _last_updated_isotime(self) -> str:
|
||||||
"""Return last_updated as an isotime string."""
|
"""Return last_updated as an isotime string."""
|
||||||
date_time = dt_util.utc_from_timestamp(
|
if self.last_updated_ts is not None:
|
||||||
self.last_updated_ts
|
date_time = dt_util.utc_from_timestamp(self.last_updated_ts)
|
||||||
) or process_timestamp(self.last_updated)
|
else:
|
||||||
|
date_time = process_timestamp(self.last_updated)
|
||||||
return date_time.isoformat(sep=" ", timespec="seconds")
|
return date_time.isoformat(sep=" ", timespec="seconds")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -79,6 +79,40 @@ def test_repr():
|
|||||||
assert "2016-07-09 11:00:00+00:00" in repr(Events.from_event(event))
|
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):
|
def test_handling_broken_json_state_attributes(caplog):
|
||||||
"""Test we handle broken json in state attributes."""
|
"""Test we handle broken json in state attributes."""
|
||||||
state_attributes = StateAttributes(
|
state_attributes = StateAttributes(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user