Add decorator typing [toon] (#107597)
This commit is contained in:
parent
3fba02a692
commit
402ead8df2
1 changed files with 13 additions and 2 deletions
|
@ -1,19 +1,30 @@
|
|||
"""Helpers for Toon."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Coroutine
|
||||
import logging
|
||||
from typing import Any, Concatenate, ParamSpec, TypeVar
|
||||
|
||||
from toonapi import ToonConnectionError, ToonError
|
||||
|
||||
from .models import ToonEntity
|
||||
|
||||
_ToonEntityT = TypeVar("_ToonEntityT", bound=ToonEntity)
|
||||
_P = ParamSpec("_P")
|
||||
|
||||
_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.
|
||||
|
||||
A decorator that wraps the passed in function, catches Toon errors,
|
||||
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:
|
||||
await func(self, *args, **kwargs)
|
||||
self.coordinator.async_update_listeners()
|
||||
|
|
Loading…
Add table
Reference in a new issue