"""Helpers for TechnoVE.""" from collections.abc import Callable, Coroutine from typing import Any, Concatenate from technove import TechnoVEConnectionError, TechnoVEError from homeassistant.exceptions import HomeAssistantError from .const import DOMAIN 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( translation_domain=DOMAIN, translation_key="communication_error", ) from error except TechnoVEError as error: raise HomeAssistantError( translation_domain=DOMAIN, translation_key="invalid_response", ) from error return handler