Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helpers for TechnoVE."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from typing import Any, Concatenate
7 
8 from technove import TechnoVEConnectionError, TechnoVEError
9 
10 from homeassistant.exceptions import HomeAssistantError
11 
12 from .entity import TechnoVEEntity
13 
14 
15 def technove_exception_handler[_TechnoVEEntityT: TechnoVEEntity, **_P](
16  func: Callable[Concatenate[_TechnoVEEntityT, _P], Coroutine[Any, Any, Any]],
17 ) -> Callable[Concatenate[_TechnoVEEntityT, _P], Coroutine[Any, Any, None]]:
18  """Decorate TechnoVE calls to handle TechnoVE exceptions.
19 
20  A decorator that wraps the passed in function, catches TechnoVE errors,
21  and handles the availability of the device in the data coordinator.
22  """
23 
24  async def handler(
25  self: _TechnoVEEntityT, *args: _P.args, **kwargs: _P.kwargs
26  ) -> None:
27  try:
28  await func(self, *args, **kwargs)
29 
30  except TechnoVEConnectionError as error:
31  self.coordinator.last_update_success = False
32  self.coordinator.async_update_listeners()
33  raise HomeAssistantError("Error communicating with TechnoVE API") from error
34 
35  except TechnoVEError as error:
36  raise HomeAssistantError("Invalid response from TechnoVE API") from error
37 
38  return handler