Improve isoformat timestamp performance for full states (#37105)

pull/37116/head
J. Nick Koston 2020-06-25 19:11:49 -05:00 committed by GitHub
parent fd1a8dd96c
commit 30df871787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 0 deletions

View File

@ -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 (