Reduce code in entity filter (#94882)

This commit is contained in:
J. Nick Koston 2023-06-21 08:37:19 +01:00 committed by GitHub
parent 31e9d95f66
commit f8bef95eb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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