diff --git a/homeassistant/helpers/entityfilter.py b/homeassistant/helpers/entityfilter.py index 837c5e2bc1d..24b65cba82a 100644 --- a/homeassistant/helpers/entityfilter.py +++ b/homeassistant/helpers/entityfilter.py @@ -4,11 +4,18 @@ from __future__ import annotations from collections.abc import Callable import fnmatch +from functools import lru_cache import re import voluptuous as vol -from homeassistant.const import CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE +from homeassistant.const import ( + CONF_DOMAINS, + CONF_ENTITIES, + CONF_EXCLUDE, + CONF_INCLUDE, + MAX_EXPECTED_ENTITY_IDS, +) from homeassistant.core import split_entity_id from . import config_validation as cv @@ -197,6 +204,7 @@ def _generate_filter_from_sets_and_pattern_lists( # - Otherwise: exclude if have_include and not have_exclude: + @lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS) def entity_included(entity_id: str) -> bool: """Return true if entity matches inclusion filters.""" return ( @@ -215,6 +223,7 @@ def _generate_filter_from_sets_and_pattern_lists( # - Otherwise: include if not have_include and have_exclude: + @lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS) def entity_not_excluded(entity_id: str) -> bool: """Return true if entity matches exclusion filters.""" return not ( @@ -234,6 +243,7 @@ def _generate_filter_from_sets_and_pattern_lists( # - Otherwise: exclude if include_d or include_eg: + @lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS) def entity_filter_4a(entity_id: str) -> bool: """Return filter function for case 4a.""" return entity_id in include_e or ( @@ -257,6 +267,7 @@ def _generate_filter_from_sets_and_pattern_lists( # - Otherwise: include if exclude_d or exclude_eg: + @lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS) def entity_filter_4b(entity_id: str) -> bool: """Return filter function for case 4b.""" domain = split_entity_id(entity_id)[0]