Type check all helpers (#25373)

* Type check all helpers, add inline exclusions for work in progress

* Remove unused Script._template_cache

* Add some missing type hints

* Remove unneeded type: ignore

* Type hint fixes

* Mypy assistance tweaks

* Don't look for None in deprecated config "at most once" check

* Avoid None name slugify attempt when generating entity id

* Avoid None state store attempt on entity remove
This commit is contained in:
Ville Skyttä 2019-07-21 19:59:02 +03:00 committed by Paulus Schoutsen
parent 0653f57fb4
commit d64f1e767c
19 changed files with 119 additions and 69 deletions

View file

@ -2,12 +2,13 @@
import logging
from contextlib import suppress
from datetime import datetime
from itertools import islice
from typing import Optional, Sequence
from typing import Optional, Sequence, Callable, Dict, List, Set, Tuple
import voluptuous as vol
from homeassistant.core import HomeAssistant, Context, callback
from homeassistant.core import HomeAssistant, Context, callback, CALLBACK_TYPE
from homeassistant.const import CONF_CONDITION, CONF_TIMEOUT
from homeassistant import exceptions
from homeassistant.helpers import (
@ -20,6 +21,10 @@ import homeassistant.util.dt as date_util
from homeassistant.util.async_ import (
run_coroutine_threadsafe, run_callback_threadsafe)
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
# mypy: no-check-untyped-defs
_LOGGER = logging.getLogger(__name__)
CONF_ALIAS = 'alias'
@ -76,7 +81,8 @@ class _SuspendScript(Exception):
class Script():
"""Representation of a script."""
def __init__(self, hass: HomeAssistant, sequence, name: str = None,
def __init__(self, hass: HomeAssistant, sequence,
name: Optional[str] = None,
change_listener=None) -> None:
"""Initialize the script."""
self.hass = hass
@ -85,14 +91,13 @@ class Script():
self.name = name
self._change_listener = change_listener
self._cur = -1
self._exception_step = None
self._exception_step = None # type: Optional[int]
self.last_action = None
self.last_triggered = None
self.last_triggered = None # type: Optional[datetime]
self.can_cancel = any(CONF_DELAY in action or CONF_WAIT_TEMPLATE
in action for action in self.sequence)
self._async_listener = []
self._template_cache = {}
self._config_cache = {}
self._async_listener = [] # type: List[CALLBACK_TYPE]
self._config_cache = {} # type: Dict[Set[Tuple], Callable[..., bool]]
self._actions = {
ACTION_DELAY: self._async_delay,
ACTION_WAIT_TEMPLATE: self._async_wait_template,