From 2628ce54d9d1856c01cbae6a29d089f9b057da12 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Fri, 18 Jun 2021 18:46:20 +0200 Subject: [PATCH] Type homeassistant triggers event (#51979) --- .strict-typing | 1 + .../homeassistant/triggers/event.py | 25 +++++++++++------ mypy.ini | 28 ++++++++++++++++++- script/hassfest/mypy_config.py | 7 ++++- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/.strict-typing b/.strict-typing index d389d9e1950..c87b0b270a8 100644 --- a/.strict-typing +++ b/.strict-typing @@ -35,6 +35,7 @@ homeassistant.components.geo_location.* homeassistant.components.gios.* homeassistant.components.group.* homeassistant.components.history.* +homeassistant.components.homeassistant.triggers.event homeassistant.components.http.* homeassistant.components.huawei_lte.* homeassistant.components.hyperion.* diff --git a/homeassistant/components/homeassistant/triggers/event.py b/homeassistant/components/homeassistant/triggers/event.py index 6b4cf520560..47dc5317bbd 100644 --- a/homeassistant/components/homeassistant/triggers/event.py +++ b/homeassistant/components/homeassistant/triggers/event.py @@ -1,11 +1,15 @@ """Offer event listening automation rules.""" +from __future__ import annotations + +from typing import Any + import voluptuous as vol +from homeassistant.components.automation import AutomationActionType from homeassistant.const import CONF_EVENT_DATA, CONF_PLATFORM -from homeassistant.core import HassJob, callback +from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback from homeassistant.helpers import config_validation as cv, template - -# mypy: allow-untyped-defs +from homeassistant.helpers.typing import ConfigType CONF_EVENT_TYPE = "event_type" CONF_EVENT_CONTEXT = "context" @@ -20,7 +24,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( ) -def _schema_value(value): +def _schema_value(value: Any) -> Any: if isinstance(value, list): return vol.In(value) @@ -28,8 +32,13 @@ def _schema_value(value): async def async_attach_trigger( - hass, config, action, automation_info, *, platform_type="event" -): + hass: HomeAssistant, + config: ConfigType, + action: AutomationActionType, + automation_info: dict[str, Any], + *, + platform_type: str = "event", +) -> CALLBACK_TYPE: """Listen for events based on configuration.""" trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} variables = None @@ -76,7 +85,7 @@ async def async_attach_trigger( job = HassJob(action) @callback - def handle_event(event): + def handle_event(event: Event) -> None: """Listen for events and calls the action when data matches.""" try: # Check that the event data and context match the configured @@ -107,7 +116,7 @@ async def async_attach_trigger( ] @callback - def remove_listen_events(): + def remove_listen_events() -> None: """Remove event listeners.""" for remove in removes: remove() diff --git a/mypy.ini b/mypy.ini index 52c48a54035..f80f2baa9e3 100644 --- a/mypy.ini +++ b/mypy.ini @@ -396,6 +396,17 @@ no_implicit_optional = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.homeassistant.triggers.event] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +no_implicit_optional = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.http.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -1148,7 +1159,22 @@ ignore_errors = true [mypy-homeassistant.components.home_plus_control.*] ignore_errors = true -[mypy-homeassistant.components.homeassistant.*] +[mypy-homeassistant.components.homeassistant.triggers.homeassistant] +ignore_errors = true + +[mypy-homeassistant.components.homeassistant.triggers.numeric_state] +ignore_errors = true + +[mypy-homeassistant.components.homeassistant.triggers.time_pattern] +ignore_errors = true + +[mypy-homeassistant.components.homeassistant.triggers.time] +ignore_errors = true + +[mypy-homeassistant.components.homeassistant.triggers.state] +ignore_errors = true + +[mypy-homeassistant.components.homeassistant.scene] ignore_errors = true [mypy-homeassistant.components.homekit.*] diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index e9567be8924..b934bed3bc1 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -85,7 +85,12 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.hisense_aehw4a1.*", "homeassistant.components.home_connect.*", "homeassistant.components.home_plus_control.*", - "homeassistant.components.homeassistant.*", + "homeassistant.components.homeassistant.triggers.homeassistant", + "homeassistant.components.homeassistant.triggers.numeric_state", + "homeassistant.components.homeassistant.triggers.time_pattern", + "homeassistant.components.homeassistant.triggers.time", + "homeassistant.components.homeassistant.triggers.state", + "homeassistant.components.homeassistant.scene", "homeassistant.components.homekit.*", "homeassistant.components.homekit_controller.*", "homeassistant.components.homematicip_cloud.*",