Reduce code in entity filter (#94882)
This commit is contained in:
parent
31e9d95f66
commit
f8bef95eb6
1 changed files with 17 additions and 26 deletions
|
@ -185,22 +185,6 @@ def _generate_filter_from_sets_and_pattern_lists(
|
||||||
have_exclude = bool(exclude_e or exclude_d or exclude_eg)
|
have_exclude = bool(exclude_e or exclude_d or exclude_eg)
|
||||||
have_include = bool(include_e or include_d or include_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
|
# Case 1 - No filter
|
||||||
# - All entities included
|
# - All entities included
|
||||||
if not have_include and not have_exclude:
|
if not have_include and not have_exclude:
|
||||||
|
@ -213,12 +197,16 @@ 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:
|
||||||
|
|
||||||
def entity_filter_2(entity_id: str) -> bool:
|
def entity_included(entity_id: str) -> bool:
|
||||||
"""Return filter function for case 2."""
|
"""Return true if entity matches inclusion filters."""
|
||||||
domain = split_entity_id(entity_id)[0]
|
return (
|
||||||
return entity_included(domain, entity_id)
|
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
|
# Case 3 - Only excludes
|
||||||
# - Entity listed in exclude: exclude
|
# - Entity listed in exclude: exclude
|
||||||
|
@ -227,12 +215,15 @@ 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:
|
||||||
|
|
||||||
def entity_filter_3(entity_id: str) -> bool:
|
def entity_not_excluded(entity_id: str) -> bool:
|
||||||
"""Return filter function for case 3."""
|
"""Return true if entity matches exclusion filters."""
|
||||||
domain = split_entity_id(entity_id)[0]
|
return not (
|
||||||
return not entity_excluded(domain, entity_id)
|
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)
|
# Case 4 - Domain and/or glob includes (may also have excludes)
|
||||||
# - Entity listed in entities include: include
|
# - Entity listed in entities include: include
|
||||||
|
|
Loading…
Add table
Reference in a new issue