Compare commits

...

2 Commits

Author SHA1 Message Date
Franck Nijhof 8bd7932d17 Rename unknown_error to invalid_response for ToonError 2026-05-29 07:16:00 +00:00
Franck Nijhof b3a5d80d01 Raise errors instead of swallowing exceptions in Toon action handlers 2026-05-28 22:37:55 +00:00
4 changed files with 18 additions and 14 deletions
-2
View File
@@ -102,14 +102,12 @@ class ToonThermostatDevice(ToonDisplayDeviceEntity, ClimateEntity):
"""Return the current state of the burner."""
return {"heating_type": self.coordinator.data.agreement.heating_type}
# pylint: disable-next=home-assistant-action-swallowed-exception
@toon_exception_handler
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Change the setpoint of the thermostat."""
temperature = kwargs.get(ATTR_TEMPERATURE)
await self.coordinator.toon.set_current_setpoint(temperature)
# pylint: disable-next=home-assistant-action-swallowed-exception
@toon_exception_handler
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
+12 -8
View File
@@ -1,14 +1,14 @@
"""Helpers for Toon."""
from collections.abc import Callable, Coroutine
import logging
from typing import Any, Concatenate
from toonapi import ToonConnectionError, ToonError
from .entity import ToonEntity
from homeassistant.exceptions import HomeAssistantError
_LOGGER = logging.getLogger(__name__)
from .const import DOMAIN
from .entity import ToonEntity
def toon_exception_handler[_ToonEntityT: ToonEntity, **_P](
@@ -17,20 +17,24 @@ def toon_exception_handler[_ToonEntityT: ToonEntity, **_P](
"""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.
and raises a translated ``HomeAssistantError``.
"""
async def handler(self: _ToonEntityT, *args: _P.args, **kwargs: _P.kwargs) -> None:
try:
await func(self, *args, **kwargs)
self.coordinator.async_update_listeners()
except ToonConnectionError as error:
_LOGGER.error("Error communicating with API: %s", error)
self.coordinator.last_update_success = False
self.coordinator.async_update_listeners()
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="communication_error",
) from error
except ToonError as error:
_LOGGER.error("Invalid response from API: %s", error)
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="invalid_response",
) from error
return handler
@@ -34,6 +34,12 @@
}
},
"exceptions": {
"communication_error": {
"message": "An error occurred while communicating with the Toon device."
},
"invalid_response": {
"message": "Received an invalid response from the Toon device."
},
"oauth2_implementation_unavailable": {
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
}
-4
View File
@@ -60,7 +60,6 @@ class ToonSwitch(ToonEntity, SwitchEntity):
class ToonProgramSwitch(ToonSwitch, ToonDisplayDeviceEntity):
"""Defines a Toon program switch."""
# pylint: disable-next=home-assistant-action-swallowed-exception
@toon_exception_handler
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the Toon program switch."""
@@ -68,7 +67,6 @@ class ToonProgramSwitch(ToonSwitch, ToonDisplayDeviceEntity):
ACTIVE_STATE_AWAY, PROGRAM_STATE_OFF
)
# pylint: disable-next=home-assistant-action-swallowed-exception
@toon_exception_handler
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the Toon program switch."""
@@ -80,7 +78,6 @@ class ToonProgramSwitch(ToonSwitch, ToonDisplayDeviceEntity):
class ToonHolidayModeSwitch(ToonSwitch, ToonDisplayDeviceEntity):
"""Defines a Toon Holiday mode switch."""
# pylint: disable-next=home-assistant-action-swallowed-exception
@toon_exception_handler
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the Toon holiday mode switch."""
@@ -88,7 +85,6 @@ class ToonHolidayModeSwitch(ToonSwitch, ToonDisplayDeviceEntity):
ACTIVE_STATE_AWAY, PROGRAM_STATE_ON
)
# pylint: disable-next=home-assistant-action-swallowed-exception
@toon_exception_handler
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the Toon holiday mode switch."""