core/homeassistant/components/technove/helpers.py

39 lines
1.3 KiB
Python

"""Helpers for TechnoVE."""
from __future__ import annotations
from collections.abc import Callable, Coroutine
from typing import Any, Concatenate
from technove import TechnoVEConnectionError, TechnoVEError
from homeassistant.exceptions import HomeAssistantError
from .entity import TechnoVEEntity
def technove_exception_handler[_TechnoVEEntityT: TechnoVEEntity, **_P](
func: Callable[Concatenate[_TechnoVEEntityT, _P], Coroutine[Any, Any, Any]],
) -> Callable[Concatenate[_TechnoVEEntityT, _P], Coroutine[Any, Any, None]]:
"""Decorate TechnoVE calls to handle TechnoVE exceptions.
A decorator that wraps the passed in function, catches TechnoVE errors,
and handles the availability of the device in the data coordinator.
"""
async def handler(
self: _TechnoVEEntityT, *args: _P.args, **kwargs: _P.kwargs
) -> None:
try:
await func(self, *args, **kwargs)
except TechnoVEConnectionError as error:
self.coordinator.last_update_success = False
self.coordinator.async_update_listeners()
raise HomeAssistantError("Error communicating with TechnoVE API") from error
except TechnoVEError as error:
raise HomeAssistantError("Invalid response from TechnoVE API") from error
return handler