Add decorator typing [toon] (#107597)

This commit is contained in:
Marc Mueller 2024-01-10 14:11:13 +01:00 committed by GitHub
parent 3fba02a692
commit 402ead8df2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,19 +1,30 @@
"""Helpers for Toon.""" """Helpers for Toon."""
from __future__ import annotations
from collections.abc import Callable, Coroutine
import logging import logging
from typing import Any, Concatenate, ParamSpec, TypeVar
from toonapi import ToonConnectionError, ToonError from toonapi import ToonConnectionError, ToonError
from .models import ToonEntity
_ToonEntityT = TypeVar("_ToonEntityT", bound=ToonEntity)
_P = ParamSpec("_P")
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def toon_exception_handler(func): def toon_exception_handler(
func: Callable[Concatenate[_ToonEntityT, _P], Coroutine[Any, Any, None]],
) -> Callable[Concatenate[_ToonEntityT, _P], Coroutine[Any, Any, None]]:
"""Decorate Toon calls to handle Toon exceptions. """Decorate Toon calls to handle Toon exceptions.
A decorator that wraps the passed in function, catches Toon errors, A decorator that wraps the passed in function, catches Toon errors,
and handles the availability of the device in the data coordinator. and handles the availability of the device in the data coordinator.
""" """
async def handler(self, *args, **kwargs): async def handler(self: _ToonEntityT, *args: _P.args, **kwargs: _P.kwargs) -> None:
try: try:
await func(self, *args, **kwargs) await func(self, *args, **kwargs)
self.coordinator.async_update_listeners() self.coordinator.async_update_listeners()