Make creation of capabilities_updated_at deque in Entity lazy (#114711)

Most entities will never update their capabilities so we should
avoid creating the deque as its a large chunk of the entity
creation time
This commit is contained in:
J. Nick Koston 2024-04-03 01:53:17 -10:00 committed by GitHub
parent b9f27d2b31
commit 2b9f22f11e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1131,7 +1131,16 @@ class Entity(
):
if not self.__capabilities_updated_at_reported:
time_now = hass.loop.time()
capabilities_updated_at = self.__capabilities_updated_at
# _Entity__capabilities_updated_at is because of name mangling
if not (
capabilities_updated_at := getattr(
self, "_Entity__capabilities_updated_at", None
)
):
self.__capabilities_updated_at = deque(
maxlen=CAPABILITIES_UPDATE_LIMIT + 1
)
capabilities_updated_at = self.__capabilities_updated_at
capabilities_updated_at.append(time_now)
while time_now - capabilities_updated_at[0] > 3600:
capabilities_updated_at.popleft()
@ -1444,8 +1453,6 @@ class Entity(
)
self._async_subscribe_device_updates()
self.__capabilities_updated_at = deque(maxlen=CAPABILITIES_UPDATE_LIMIT + 1)
async def async_internal_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass.