2023-01-15 16:30:27 +00:00
|
|
|
"""Helpers for HomeWizard."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Callable, Coroutine
|
2023-01-23 06:28:43 +00:00
|
|
|
from typing import Any, Concatenate, ParamSpec, TypeVar
|
2023-01-15 16:30:27 +00:00
|
|
|
|
|
|
|
from homewizard_energy.errors import DisabledError, RequestError
|
|
|
|
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
|
2023-11-27 06:47:46 +00:00
|
|
|
from .const import DOMAIN
|
2023-01-15 16:30:27 +00:00
|
|
|
from .entity import HomeWizardEntity
|
|
|
|
|
|
|
|
_HomeWizardEntityT = TypeVar("_HomeWizardEntityT", bound=HomeWizardEntity)
|
|
|
|
_P = ParamSpec("_P")
|
|
|
|
|
|
|
|
|
|
|
|
def homewizard_exception_handler(
|
2023-12-20 22:55:09 +00:00
|
|
|
func: Callable[Concatenate[_HomeWizardEntityT, _P], Coroutine[Any, Any, Any]],
|
2023-01-15 16:30:27 +00:00
|
|
|
) -> Callable[Concatenate[_HomeWizardEntityT, _P], Coroutine[Any, Any, None]]:
|
|
|
|
"""Decorate HomeWizard Energy calls to handle HomeWizardEnergy exceptions.
|
|
|
|
|
|
|
|
A decorator that wraps the passed in function, catches HomeWizardEnergy errors,
|
2023-01-16 08:23:03 +00:00
|
|
|
and reloads the integration when the API was disabled so the reauth flow is
|
|
|
|
triggered.
|
2023-01-15 16:30:27 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
async def handler(
|
|
|
|
self: _HomeWizardEntityT, *args: _P.args, **kwargs: _P.kwargs
|
|
|
|
) -> None:
|
|
|
|
try:
|
|
|
|
await func(self, *args, **kwargs)
|
|
|
|
except RequestError as ex:
|
2023-11-27 06:47:46 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
"An error occurred while communicating with HomeWizard device",
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="communication_error",
|
|
|
|
) from ex
|
2023-01-15 16:30:27 +00:00
|
|
|
except DisabledError as ex:
|
2023-10-29 17:23:48 +00:00
|
|
|
await self.hass.config_entries.async_reload(
|
|
|
|
self.coordinator.config_entry.entry_id
|
|
|
|
)
|
2023-11-27 06:47:46 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
"The local API of the HomeWizard device is disabled",
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="api_disabled",
|
|
|
|
) from ex
|
2023-01-15 16:30:27 +00:00
|
|
|
|
|
|
|
return handler
|