From f8bef95eb65bbc395e41931f1c1d88afa5c63ec9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 21 Jun 2023 08:37:19 +0100 Subject: [PATCH] Reduce code in entity filter (#94882) --- homeassistant/helpers/entityfilter.py | 43 +++++++++++---------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/homeassistant/helpers/entityfilter.py b/homeassistant/helpers/entityfilter.py index a9d3ccad138..1a449ec15f0 100644 --- a/homeassistant/helpers/entityfilter.py +++ b/homeassistant/helpers/entityfilter.py @@ -185,22 +185,6 @@ def _generate_filter_from_sets_and_pattern_lists( have_exclude = bool(exclude_e or exclude_d or exclude_eg) have_include = bool(include_e or include_d or include_eg) - def entity_included(domain: str, entity_id: str) -> bool: - """Return true if entity matches inclusion filters.""" - return ( - entity_id in include_e - or domain in include_d - or (bool(include_eg and include_eg.match(entity_id))) - ) - - def entity_excluded(domain: str, entity_id: str) -> bool: - """Return true if entity matches exclusion filters.""" - return ( - entity_id in exclude_e - or domain in exclude_d - or (bool(exclude_eg and exclude_eg.match(entity_id))) - ) - # Case 1 - No filter # - All entities included if not have_include and not have_exclude: @@ -213,12 +197,16 @@ def _generate_filter_from_sets_and_pattern_lists( # - Otherwise: exclude if have_include and not have_exclude: - def entity_filter_2(entity_id: str) -> bool: - """Return filter function for case 2.""" - domain = split_entity_id(entity_id)[0] - return entity_included(domain, entity_id) + def entity_included(entity_id: str) -> bool: + """Return true if entity matches inclusion filters.""" + return ( + entity_id in include_e + or split_entity_id(entity_id)[0] in include_d + or (bool(include_eg and include_eg.match(entity_id))) + ) - return entity_filter_2 + # Return filter function for case 2 + return entity_included # Case 3 - Only excludes # - Entity listed in exclude: exclude @@ -227,12 +215,15 @@ def _generate_filter_from_sets_and_pattern_lists( # - Otherwise: include if not have_include and have_exclude: - def entity_filter_3(entity_id: str) -> bool: - """Return filter function for case 3.""" - domain = split_entity_id(entity_id)[0] - return not entity_excluded(domain, entity_id) + def entity_not_excluded(entity_id: str) -> bool: + """Return true if entity matches exclusion filters.""" + return not ( + entity_id in exclude_e + or split_entity_id(entity_id)[0] in exclude_d + or (exclude_eg and exclude_eg.match(entity_id)) + ) - return entity_filter_3 + return entity_not_excluded # Case 4 - Domain and/or glob includes (may also have excludes) # - Entity listed in entities include: include