From 198ade5e2be84580d4d7918789efe85b44bcafa5 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 7 Jan 2022 00:43:54 +0100 Subject: [PATCH] Fix typing `TrackTemplateResultListener` [helpers.event] (#63541) --- homeassistant/components/websocket_api/commands.py | 4 +++- homeassistant/helpers/event.py | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index c9857f14d03..cbf84cd002e 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -354,7 +354,9 @@ async def handle_render_template( return @callback - def _template_listener(event: Event, updates: list[TrackTemplateResult]) -> None: + def _template_listener( + event: Event | None, updates: list[TrackTemplateResult] + ) -> None: nonlocal info track_template_result = updates.pop() result = track_template_result.result diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 289d3321a41..24b0b66bc97 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -9,7 +9,7 @@ from datetime import datetime, timedelta import functools as ft import logging import time -from typing import Any, Callable, List, cast +from typing import Any, Callable, List, Union, cast import attr @@ -533,7 +533,7 @@ class _TrackStateChangeFiltered: """Handle removal / refresh of tracker init.""" self.hass = hass self._action = action - self._listeners: dict[str, Callable] = {} + self._listeners: dict[str, Callable[[], None]] = {} self._last_track_states: TrackStates = track_states @callback @@ -721,7 +721,7 @@ def async_track_template( @callback def _template_changed_listener( - event: Event, updates: list[TrackTemplateResult] + event: Event | None, updates: list[TrackTemplateResult] ) -> None: """Check if condition is correct and run action.""" track_result = updates.pop() @@ -769,7 +769,7 @@ class _TrackTemplateResultInfo: self, hass: HomeAssistant, track_templates: Sequence[TrackTemplate], - action: Callable, + action: Callable[[Event | None, list[TrackTemplateResult]], None], has_super_template: bool = False, ) -> None: """Handle removal / refresh of tracker init.""" @@ -786,7 +786,7 @@ class _TrackTemplateResultInfo: self._rate_limit = KeyedRateLimit(hass) self._info: dict[Template, RenderInfo] = {} self._track_state_changes: _TrackStateChangeFiltered | None = None - self._time_listeners: dict[Template, Callable] = {} + self._time_listeners: dict[Template, Callable[[], None]] = {} def async_setup(self, raise_on_template_error: bool, strict: bool = False) -> None: """Activation of template tracking.""" @@ -986,7 +986,7 @@ class _TrackTemplateResultInfo: replayed is True if the event is being replayed because the rate limit was hit. """ - updates = [] + updates: list[TrackTemplateResult] = [] info_changed = False now = event.time_fired if not replayed and event else dt_util.utcnow() @@ -1075,7 +1075,7 @@ class _TrackTemplateResultInfo: TrackTemplateResultListener = Callable[ [ - Event, + Union[Event, None], List[TrackTemplateResult], ], None,