Add cache to more complex entity filters (#118344)

Many of these do regexes and since the entity_ids are almost
always the same we should cache these
This commit is contained in:
J. Nick Koston 2024-05-28 17:07:50 -10:00 committed by GitHub
parent d22871f1fd
commit b94bf1f214
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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