Add more type hints to helpers (#18350)

* Add type hints to helpers.entityfilter

* Add type hints to helpers.deprecation
This commit is contained in:
Ville Skyttä 2018-11-11 18:39:50 +02:00 committed by Paulus Schoutsen
parent b8c06ad019
commit 9411fca955
3 changed files with 20 additions and 16 deletions

View file

@ -1,4 +1,5 @@
"""Helper class to implement include/exclude of entities and domains."""
from typing import Callable, Dict, Iterable
import voluptuous as vol
@ -11,14 +12,14 @@ CONF_EXCLUDE_DOMAINS = 'exclude_domains'
CONF_EXCLUDE_ENTITIES = 'exclude_entities'
def _convert_filter(config):
def _convert_filter(config: Dict[str, Iterable[str]]) -> Callable[[str], bool]:
filt = generate_filter(
config[CONF_INCLUDE_DOMAINS],
config[CONF_INCLUDE_ENTITIES],
config[CONF_EXCLUDE_DOMAINS],
config[CONF_EXCLUDE_ENTITIES],
)
filt.config = config
setattr(filt, 'config', config)
return filt
@ -33,8 +34,10 @@ FILTER_SCHEMA = vol.All(
}), _convert_filter)
def generate_filter(include_domains, include_entities,
exclude_domains, exclude_entities):
def generate_filter(include_domains: Iterable[str],
include_entities: Iterable[str],
exclude_domains: Iterable[str],
exclude_entities: Iterable[str]) -> Callable[[str], bool]:
"""Return a function that will filter entities based on the args."""
include_d = set(include_domains)
include_e = set(include_entities)
@ -50,7 +53,7 @@ def generate_filter(include_domains, include_entities,
# Case 2 - includes, no excludes - only include specified entities
if have_include and not have_exclude:
def entity_filter_2(entity_id):
def entity_filter_2(entity_id: str) -> bool:
"""Return filter function for case 2."""
domain = split_entity_id(entity_id)[0]
return (entity_id in include_e or
@ -60,7 +63,7 @@ def generate_filter(include_domains, include_entities,
# Case 3 - excludes, no includes - only exclude specified entities
if not have_include and have_exclude:
def entity_filter_3(entity_id):
def entity_filter_3(entity_id: str) -> bool:
"""Return filter function for case 3."""
domain = split_entity_id(entity_id)[0]
return (entity_id not in exclude_e and
@ -75,7 +78,7 @@ def generate_filter(include_domains, include_entities,
# note: if both include and exclude domains specified,
# the exclude domains are ignored
if include_d:
def entity_filter_4a(entity_id):
def entity_filter_4a(entity_id: str) -> bool:
"""Return filter function for case 4a."""
domain = split_entity_id(entity_id)[0]
if domain in include_d:
@ -88,7 +91,7 @@ def generate_filter(include_domains, include_entities,
# - if domain is excluded, pass if entity is included
# - if domain is not excluded, pass if entity not excluded
if exclude_d:
def entity_filter_4b(entity_id):
def entity_filter_4b(entity_id: str) -> bool:
"""Return filter function for case 4b."""
domain = split_entity_id(entity_id)[0]
if domain in exclude_d:
@ -99,7 +102,7 @@ def generate_filter(include_domains, include_entities,
# Case 4c - neither include or exclude domain specified
# - Only pass if entity is included. Ignore entity excludes.
def entity_filter_4c(entity_id):
def entity_filter_4c(entity_id: str) -> bool:
"""Return filter function for case 4c."""
return entity_id in include_e