2020-06-23 01:22:41 +00:00
|
|
|
"""Helpers for Toon."""
|
2024-03-08 15:35:23 +00:00
|
|
|
|
2024-01-10 13:11:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Callable, Coroutine
|
2020-06-23 01:22:41 +00:00
|
|
|
import logging
|
2024-01-10 13:11:13 +00:00
|
|
|
from typing import Any, Concatenate, ParamSpec, TypeVar
|
2020-06-23 01:22:41 +00:00
|
|
|
|
|
|
|
from toonapi import ToonConnectionError, ToonError
|
|
|
|
|
2024-01-10 13:11:13 +00:00
|
|
|
from .models import ToonEntity
|
|
|
|
|
|
|
|
_ToonEntityT = TypeVar("_ToonEntityT", bound=ToonEntity)
|
|
|
|
_P = ParamSpec("_P")
|
|
|
|
|
2020-06-23 01:22:41 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-01-10 13:11:13 +00:00
|
|
|
def toon_exception_handler(
|
|
|
|
func: Callable[Concatenate[_ToonEntityT, _P], Coroutine[Any, Any, None]],
|
|
|
|
) -> Callable[Concatenate[_ToonEntityT, _P], Coroutine[Any, Any, None]]:
|
2020-06-23 01:22:41 +00:00
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
2024-01-10 13:11:13 +00:00
|
|
|
async def handler(self: _ToonEntityT, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
2020-06-23 01:22:41 +00:00
|
|
|
try:
|
|
|
|
await func(self, *args, **kwargs)
|
2022-06-03 11:55:57 +00:00
|
|
|
self.coordinator.async_update_listeners()
|
2020-06-23 01:22:41 +00:00
|
|
|
|
|
|
|
except ToonConnectionError as error:
|
|
|
|
_LOGGER.error("Error communicating with API: %s", error)
|
|
|
|
self.coordinator.last_update_success = False
|
2022-06-03 11:55:57 +00:00
|
|
|
self.coordinator.async_update_listeners()
|
2020-06-23 01:22:41 +00:00
|
|
|
|
|
|
|
except ToonError as error:
|
|
|
|
_LOGGER.error("Invalid response from API: %s", error)
|
|
|
|
|
|
|
|
return handler
|