mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
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:
parent
d22871f1fd
commit
b94bf1f214
@ -4,11 +4,18 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
from functools import lru_cache
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import voluptuous as vol
|
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 homeassistant.core import split_entity_id
|
||||||
|
|
||||||
from . import config_validation as cv
|
from . import config_validation as cv
|
||||||
@ -197,6 +204,7 @@ def _generate_filter_from_sets_and_pattern_lists(
|
|||||||
# - Otherwise: exclude
|
# - Otherwise: exclude
|
||||||
if have_include and not have_exclude:
|
if have_include and not have_exclude:
|
||||||
|
|
||||||
|
@lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS)
|
||||||
def entity_included(entity_id: str) -> bool:
|
def entity_included(entity_id: str) -> bool:
|
||||||
"""Return true if entity matches inclusion filters."""
|
"""Return true if entity matches inclusion filters."""
|
||||||
return (
|
return (
|
||||||
@ -215,6 +223,7 @@ def _generate_filter_from_sets_and_pattern_lists(
|
|||||||
# - Otherwise: include
|
# - Otherwise: include
|
||||||
if not have_include and have_exclude:
|
if not have_include and have_exclude:
|
||||||
|
|
||||||
|
@lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS)
|
||||||
def entity_not_excluded(entity_id: str) -> bool:
|
def entity_not_excluded(entity_id: str) -> bool:
|
||||||
"""Return true if entity matches exclusion filters."""
|
"""Return true if entity matches exclusion filters."""
|
||||||
return not (
|
return not (
|
||||||
@ -234,6 +243,7 @@ def _generate_filter_from_sets_and_pattern_lists(
|
|||||||
# - Otherwise: exclude
|
# - Otherwise: exclude
|
||||||
if include_d or include_eg:
|
if include_d or include_eg:
|
||||||
|
|
||||||
|
@lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS)
|
||||||
def entity_filter_4a(entity_id: str) -> bool:
|
def entity_filter_4a(entity_id: str) -> bool:
|
||||||
"""Return filter function for case 4a."""
|
"""Return filter function for case 4a."""
|
||||||
return entity_id in include_e or (
|
return entity_id in include_e or (
|
||||||
@ -257,6 +267,7 @@ def _generate_filter_from_sets_and_pattern_lists(
|
|||||||
# - Otherwise: include
|
# - Otherwise: include
|
||||||
if exclude_d or exclude_eg:
|
if exclude_d or exclude_eg:
|
||||||
|
|
||||||
|
@lru_cache(maxsize=MAX_EXPECTED_ENTITY_IDS)
|
||||||
def entity_filter_4b(entity_id: str) -> bool:
|
def entity_filter_4b(entity_id: str) -> bool:
|
||||||
"""Return filter function for case 4b."""
|
"""Return filter function for case 4b."""
|
||||||
domain = split_entity_id(entity_id)[0]
|
domain = split_entity_id(entity_id)[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user