Add decorator typing [yeelight] (#107598)

This commit is contained in:
Marc Mueller 2024-01-16 23:18:30 +01:00 committed by GitHub
parent 25f4fe4a85
commit bee53f6004
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,9 +2,10 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Callable, Coroutine
import logging import logging
import math import math
from typing import Any from typing import Any, Concatenate, ParamSpec, TypeVar
import voluptuous as vol import voluptuous as vol
import yeelight import yeelight
@ -66,6 +67,10 @@ from .const import (
from .device import YeelightDevice from .device import YeelightDevice
from .entity import YeelightEntity from .entity import YeelightEntity
_YeelightBaseLightT = TypeVar("_YeelightBaseLightT", bound="YeelightBaseLight")
_R = TypeVar("_R")
_P = ParamSpec("_P")
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_MINUTES = "minutes" ATTR_MINUTES = "minutes"
@ -238,10 +243,14 @@ def _parse_custom_effects(effects_config) -> dict[str, dict[str, Any]]:
return effects 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.""" """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): for attempts in range(2):
try: try:
_LOGGER.debug("Calling %s with %s %s", func, args, kwargs) _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"Error when calling {func.__name__} for bulb "
f"{self.device.name} at {self.device.host}: {str(ex) or type(ex)}" f"{self.device.name} at {self.device.host}: {str(ex) or type(ex)}"
) from ex ) from ex
return None
return _async_wrap return _async_wrap