From 0d511c697c09dc91c8c51d5fd731df1768bf8827 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 30 Mar 2025 10:20:24 -1000 Subject: [PATCH] Improve performance of as_compressed_state (#141800) We have to build all of these at startup. Its a lot faster to compare floats instead of datetime objects. Since we already have to fetch last_changed_timestamp, use it to compare with last_updated_timestamp since we already know we will have last_updated_timestamp --- homeassistant/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 46ae499e2ca..ec251832dba 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1935,13 +1935,14 @@ class State: # to avoid callers outside of this module # from misusing it by mistake. context = state_context._as_dict # noqa: SLF001 + last_changed_timestamp = self.last_changed_timestamp compressed_state: CompressedState = { COMPRESSED_STATE_STATE: self.state, COMPRESSED_STATE_ATTRIBUTES: self.attributes, COMPRESSED_STATE_CONTEXT: context, - COMPRESSED_STATE_LAST_CHANGED: self.last_changed_timestamp, + COMPRESSED_STATE_LAST_CHANGED: last_changed_timestamp, } - if self.last_changed != self.last_updated: + if last_changed_timestamp != self.last_updated_timestamp: compressed_state[COMPRESSED_STATE_LAST_UPDATED] = ( self.last_updated_timestamp )