From bee53f6004a1cd061092714cefeec52b0ef9fa1b Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 16 Jan 2024 23:18:30 +0100 Subject: [PATCH] Add decorator typing [yeelight] (#107598) --- homeassistant/components/yeelight/light.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index c5cd6f906f5..a9834823f5e 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -2,9 +2,10 @@ from __future__ import annotations import asyncio +from collections.abc import Callable, Coroutine import logging import math -from typing import Any +from typing import Any, Concatenate, ParamSpec, TypeVar import voluptuous as vol import yeelight @@ -66,6 +67,10 @@ from .const import ( from .device import YeelightDevice from .entity import YeelightEntity +_YeelightBaseLightT = TypeVar("_YeelightBaseLightT", bound="YeelightBaseLight") +_R = TypeVar("_R") +_P = ParamSpec("_P") + _LOGGER = logging.getLogger(__name__) ATTR_MINUTES = "minutes" @@ -238,10 +243,14 @@ def _parse_custom_effects(effects_config) -> dict[str, dict[str, Any]]: return effects -def _async_cmd(func): +def _async_cmd( + func: Callable[Concatenate[_YeelightBaseLightT, _P], Coroutine[Any, Any, _R]], +) -> Callable[Concatenate[_YeelightBaseLightT, _P], Coroutine[Any, Any, _R | None]]: """Define a wrapper to catch exceptions from the bulb.""" - async def _async_wrap(self: YeelightBaseLight, *args, **kwargs): + async def _async_wrap( + self: _YeelightBaseLightT, *args: _P.args, **kwargs: _P.kwargs + ) -> _R | None: for attempts in range(2): try: _LOGGER.debug("Calling %s with %s %s", func, args, kwargs) @@ -269,6 +278,7 @@ def _async_cmd(func): f"Error when calling {func.__name__} for bulb " f"{self.device.name} at {self.device.host}: {str(ex) or type(ex)}" ) from ex + return None return _async_wrap