From 30df871787976ef76f529575f44e356e07d44e2f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 25 Jun 2020 19:11:49 -0500 Subject: [PATCH] Improve isoformat timestamp performance for full states (#37105) --- homeassistant/components/history/__init__.py | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/homeassistant/components/history/__init__.py b/homeassistant/components/history/__init__.py index f943c126d3e..0d127444d88 100644 --- a/homeassistant/components/history/__init__.py +++ b/homeassistant/components/history/__init__.py @@ -669,6 +669,33 @@ class LazyState(State): """Set last updated datetime.""" self._last_updated = value + def as_dict(self): + """Return a dict representation of the LazyState. + + Async friendly. + + To be used for JSON serialization. + """ + if self._last_changed: + last_changed_isoformat = self._last_changed.isoformat() + else: + last_changed_isoformat = process_timestamp_to_utc_isoformat( + self._row.last_changed + ) + if self._last_updated: + last_updated_isoformat = self._last_updated.isoformat() + else: + last_updated_isoformat = process_timestamp_to_utc_isoformat( + self._row.last_updated + ) + return { + "entity_id": self.entity_id, + "state": self.state, + "attributes": self._attributes or self.attributes, + "last_changed": last_changed_isoformat, + "last_updated": last_updated_isoformat, + } + def __eq__(self, other): """Return the comparison.""" return (