From e91a159efa3641361adbef10f1553d9537855ce5 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:14:33 +0100 Subject: [PATCH] Add decorator typing [modern_forms] (#107558) --- homeassistant/components/modern_forms/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/modern_forms/__init__.py b/homeassistant/components/modern_forms/__init__.py index fafd7f9c8d2..78d2fafa078 100644 --- a/homeassistant/components/modern_forms/__init__.py +++ b/homeassistant/components/modern_forms/__init__.py @@ -1,8 +1,10 @@ """The Modern Forms integration.""" from __future__ import annotations +from collections.abc import Callable, Coroutine from datetime import timedelta import logging +from typing import Any, Concatenate, ParamSpec, TypeVar from aiomodernforms import ( ModernFormsConnectionError, @@ -24,6 +26,11 @@ from homeassistant.helpers.update_coordinator import ( from .const import DOMAIN +_ModernFormsDeviceEntityT = TypeVar( + "_ModernFormsDeviceEntityT", bound="ModernFormsDeviceEntity" +) +_P = ParamSpec("_P") + SCAN_INTERVAL = timedelta(seconds=5) PLATFORMS = [ Platform.BINARY_SENSOR, @@ -64,14 +71,18 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return unload_ok -def modernforms_exception_handler(func): +def modernforms_exception_handler( + func: Callable[Concatenate[_ModernFormsDeviceEntityT, _P], Any], +) -> Callable[Concatenate[_ModernFormsDeviceEntityT, _P], Coroutine[Any, Any, None]]: """Decorate Modern Forms calls to handle Modern Forms exceptions. A decorator that wraps the passed in function, catches Modern Forms errors, and handles the availability of the device in the data coordinator. """ - async def handler(self, *args, **kwargs): + async def handler( + self: _ModernFormsDeviceEntityT, *args: _P.args, **kwargs: _P.kwargs + ) -> None: try: await func(self, *args, **kwargs) self.coordinator.async_update_listeners()