From 33ff84469add5778edfd8d50515c73b0a84c4611 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 27 May 2024 14:06:16 -1000 Subject: [PATCH] Align max expected entities constant between modules (#118102) --- homeassistant/const.py | 6 ++++++ homeassistant/core.py | 2 +- homeassistant/helpers/entity_values.py | 7 +++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/homeassistant/const.py b/homeassistant/const.py index bfbf7ca48a6..f5f5b35691c 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -1638,6 +1638,12 @@ FORMAT_DATE: Final = "%Y-%m-%d" FORMAT_TIME: Final = "%H:%M:%S" FORMAT_DATETIME: Final = f"{FORMAT_DATE} {FORMAT_TIME}" + +# Maximum entities expected in the state machine +# This is not a hard limit, but caches and other +# data structures will be pre-allocated to this size +MAX_EXPECTED_ENTITY_IDS: Final = 16384 + # These can be removed if no deprecated constant are in this module anymore __getattr__ = partial(check_if_deprecated_constant, module_globals=globals()) __dir__ = partial( diff --git a/homeassistant/core.py b/homeassistant/core.py index 6c2d7711a0d..27cf8fd9652 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -74,6 +74,7 @@ from .const import ( EVENT_STATE_CHANGED, EVENT_STATE_REPORTED, MATCH_ALL, + MAX_EXPECTED_ENTITY_IDS, MAX_LENGTH_EVENT_EVENT_TYPE, MAX_LENGTH_STATE_STATE, UnitOfLength, @@ -177,7 +178,6 @@ _DEPRECATED_SOURCE_YAML = DeprecatedConstantEnum(ConfigSource.YAML, "2025.1") # How long to wait until things that run on startup have to finish. TIMEOUT_EVENT_START = 15 -MAX_EXPECTED_ENTITY_IDS = 16384 EVENTS_EXCLUDED_FROM_MATCH_ALL = { EVENT_HOMEASSISTANT_CLOSE, diff --git a/homeassistant/helpers/entity_values.py b/homeassistant/helpers/entity_values.py index b5e46bdfe68..7d9e0aa29e1 100644 --- a/homeassistant/helpers/entity_values.py +++ b/homeassistant/helpers/entity_values.py @@ -7,16 +7,15 @@ from functools import lru_cache import re from typing import Any +from homeassistant.const import MAX_EXPECTED_ENTITY_IDS from homeassistant.core import split_entity_id -_MAX_EXPECTED_ENTITIES = 16384 - class EntityValues: """Class to store entity id based values. This class is expected to only be used infrequently - as it caches all entity ids up to _MAX_EXPECTED_ENTITIES. + as it caches all entity ids up to MAX_EXPECTED_ENTITY_IDS. The cache includes `self` so it is important to only use this in places where usage of `EntityValues` is immortal. @@ -41,7 +40,7 @@ class EntityValues: self._glob = compiled - @lru_cache(maxsize=_MAX_EXPECTED_ENTITIES) + @lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS) def get(self, entity_id: str) -> dict[str, str]: """Get config for an entity id.""" domain, _ = split_entity_id(entity_id)