From 52ea64d1d05c3101d0b37f1c0bd0a5967b9f983b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Jan 2023 08:11:42 -1000 Subject: [PATCH] Fix repr for States and Events without a timestamp (#86391) --- .../components/recorder/db_schema.py | 22 ++++++------ tests/components/recorder/test_models.py | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/recorder/db_schema.py b/homeassistant/components/recorder/db_schema.py index 0aa1f163d3d265..1b5ac87c24a16b 100644 --- a/homeassistant/components/recorder/db_schema.py +++ b/homeassistant/components/recorder/db_schema.py @@ -171,16 +171,17 @@ def __repr__(self) -> str: return ( "" ) @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 @@ def __repr__(self) -> str: return ( f"" ) @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 diff --git a/tests/components/recorder/test_models.py b/tests/components/recorder/test_models.py index 2e823bc19e9ebf..646b865477afde 100644 --- a/tests/components/recorder/test_models.py +++ b/tests/components/recorder/test_models.py @@ -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(