Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ddc219

Browse files
autinerdjoostlek
andauthored
Add exception handler and exception translations to eheimdigital (home-assistant#145476)
* Add exception handler and exception translations to eheimdigital * Fix --------- Co-authored-by: Joostlek <joostlek@outlook.com>
1 parent 5202bbb commit 6ddc219

File tree

9 files changed

+65
-43
lines changed

9 files changed

+65
-43
lines changed

‎homeassistant/components/eheimdigital/climate.py‎

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from eheimdigital.device import EheimDigitalDevice
66
from eheimdigital.heater import EheimDigitalHeater
7-
from eheimdigital.types import EheimDigitalClientError, HeaterMode, HeaterUnit
7+
from eheimdigital.types import HeaterMode, HeaterUnit
88

99
from homeassistant.components.climate import (
1010
PRESET_NONE,
@@ -20,12 +20,11 @@
2020
UnitOfTemperature,
2121
)
2222
from homeassistant.core import HomeAssistant
23-
from homeassistant.exceptions import HomeAssistantError
2423
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2524

2625
from .const import HEATER_BIO_MODE, HEATER_PRESET_TO_HEATER_MODE, HEATER_SMART_MODE
2726
from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator
28-
from .entity import EheimDigitalEntity
27+
from .entity import EheimDigitalEntity, exception_handler
2928

3029
# Coordinator is used to centralize the data updates
3130
PARALLEL_UPDATES = 0
@@ -83,34 +82,28 @@ def __init__(
8382
self._attr_unique_id = self._device_address
8483
self._async_update_attrs()
8584

85+
@exception_handler
8686
async def async_set_preset_mode(self, preset_mode: str) -> None:
8787
"""Set the preset mode."""
88-
try:
89-
if preset_mode in HEATER_PRESET_TO_HEATER_MODE:
90-
await self._device.set_operation_mode(
91-
HEATER_PRESET_TO_HEATER_MODE[preset_mode]
92-
)
93-
except EheimDigitalClientError as err:
94-
raise HomeAssistantError from err
88+
if preset_mode in HEATER_PRESET_TO_HEATER_MODE:
89+
await self._device.set_operation_mode(
90+
HEATER_PRESET_TO_HEATER_MODE[preset_mode]
91+
)
9592

93+
@exception_handler
9694
async def async_set_temperature(self, **kwargs: Any) -> None:
9795
"""Set a new temperature."""
98-
try:
99-
if ATTR_TEMPERATURE in kwargs:
100-
await self._device.set_target_temperature(kwargs[ATTR_TEMPERATURE])
101-
except EheimDigitalClientError as err:
102-
raise HomeAssistantError from err
96+
if ATTR_TEMPERATURE in kwargs:
97+
await self._device.set_target_temperature(kwargs[ATTR_TEMPERATURE])
10398

99+
@exception_handler
104100
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
105101
"""Set the heating mode."""
106-
try:
107-
match hvac_mode:
108-
case HVACMode.OFF:
109-
await self._device.set_active(active=False)
110-
case HVACMode.AUTO:
111-
await self._device.set_active(active=True)
112-
except EheimDigitalClientError as err:
113-
raise HomeAssistantError from err
102+
match hvac_mode:
103+
case HVACMode.OFF:
104+
await self._device.set_active(active=False)
105+
case HVACMode.AUTO:
106+
await self._device.set_active(active=True)
114107

115108
def _async_update_attrs(self) -> None:
116109
if self._device.temperature_unit == HeaterUnit.CELSIUS:

‎homeassistant/components/eheimdigital/entity.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""Base entity for EHEIM Digital."""
22

33
from abc import ABC, abstractmethod
4-
from typing import TYPE_CHECKING
4+
from collections.abc import Callable, Coroutine
5+
from typing import TYPE_CHECKING, Any, Concatenate
56

67
from eheimdigital.device import EheimDigitalDevice
8+
from eheimdigital.types import EheimDigitalClientError
79

810
from homeassistant.const import CONF_HOST
911
from homeassistant.core import callback
12+
from homeassistant.exceptions import HomeAssistantError
1013
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
1114
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1215

@@ -51,3 +54,24 @@ def _handle_coordinator_update(self) -> None:
5154
"""Update attributes when the coordinator updates."""
5255
self._async_update_attrs()
5356
super()._handle_coordinator_update()
57+
58+
59+
def exception_handler[_EntityT: EheimDigitalEntity[EheimDigitalDevice], **_P](
60+
func: Callable[Concatenate[_EntityT, _P], Coroutine[Any, Any, Any]],
61+
) -> Callable[Concatenate[_EntityT, _P], Coroutine[Any, Any, None]]:
62+
"""Decorate AirGradient calls to handle exceptions.
63+
64+
A decorator that wraps the passed in function, catches AirGradient errors.
65+
"""
66+
67+
async def handler(self: _EntityT, *args: _P.args, **kwargs: _P.kwargs) -> None:
68+
try:
69+
await func(self, *args, **kwargs)
70+
except EheimDigitalClientError as error:
71+
raise HomeAssistantError(
72+
translation_domain=DOMAIN,
73+
translation_key="communication_error",
74+
translation_placeholders={"error": str(error)},
75+
) from error
76+
77+
return handler

‎homeassistant/components/eheimdigital/light.py‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from eheimdigital.classic_led_ctrl import EheimDigitalClassicLEDControl
66
from eheimdigital.device import EheimDigitalDevice
7-
from eheimdigital.types import EheimDigitalClientError, LightMode
7+
from eheimdigital.types import LightMode
88

99
from homeassistant.components.light import (
1010
ATTR_BRIGHTNESS,
@@ -15,13 +15,12 @@
1515
LightEntityFeature,
1616
)
1717
from homeassistant.core import HomeAssistant
18-
from homeassistant.exceptions import HomeAssistantError
1918
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2019
from homeassistant.util.color import brightness_to_value, value_to_brightness
2120

2221
from .const import EFFECT_DAYCL_MODE, EFFECT_TO_LIGHT_MODE
2322
from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator
24-
from .entity import EheimDigitalEntity
23+
from .entity import EheimDigitalEntity, exception_handler
2524

2625
BRIGHTNESS_SCALE = (1, 100)
2726

@@ -88,6 +87,7 @@ def available(self) -> bool:
8887
"""Return whether the entity is available."""
8988
return super().available and self._device.light_level[self._channel] is not None
9089

90+
@exception_handler
9191
async def async_turn_on(self, **kwargs: Any) -> None:
9292
"""Turn on the light."""
9393
if ATTR_EFFECT in kwargs:
@@ -96,22 +96,17 @@ async def async_turn_on(self, **kwargs: Any) -> None:
9696
if ATTR_BRIGHTNESS in kwargs:
9797
if self._device.light_mode == LightMode.DAYCL_MODE:
9898
await self._device.set_light_mode(LightMode.MAN_MODE)
99-
try:
100-
await self._device.turn_on(
101-
int(brightness_to_value(BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])),
102-
self._channel,
103-
)
104-
except EheimDigitalClientError as err:
105-
raise HomeAssistantError from err
99+
await self._device.turn_on(
100+
int(brightness_to_value(BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])),
101+
self._channel,
102+
)
106103

104+
@exception_handler
107105
async def async_turn_off(self, **kwargs: Any) -> None:
108106
"""Turn off the light."""
109107
if self._device.light_mode == LightMode.DAYCL_MODE:
110108
await self._device.set_light_mode(LightMode.MAN_MODE)
111-
try:
112-
await self._device.turn_off(self._channel)
113-
except EheimDigitalClientError as err:
114-
raise HomeAssistantError from err
109+
await self._device.turn_off(self._channel)
115110

116111
def _async_update_attrs(self) -> None:
117112
light_level = self._device.light_level[self._channel]

‎homeassistant/components/eheimdigital/number.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2727

2828
from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator
29-
from .entity import EheimDigitalEntity
29+
from .entity import EheimDigitalEntity, exception_handler
3030

3131
PARALLEL_UPDATES = 0
3232

@@ -182,6 +182,7 @@ def __init__(
182182
self._attr_unique_id = f"{self._device_address}_{description.key}"
183183

184184
@override
185+
@exception_handler
185186
async def async_set_native_value(self, value: float) -> None:
186187
return await self.entity_description.set_value_fn(self._device, value)
187188

‎homeassistant/components/eheimdigital/quality_scale.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ rules:
5858
entity-device-class: done
5959
entity-disabled-by-default: done
6060
entity-translations: done
61-
exception-translations: todo
61+
exception-translations: done
6262
icon-translations: todo
6363
reconfiguration-flow: todo
6464
repair-issues: todo

‎homeassistant/components/eheimdigital/select.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1414

1515
from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator
16-
from .entity import EheimDigitalEntity
16+
from .entity import EheimDigitalEntity, exception_handler
1717

1818
PARALLEL_UPDATES = 0
1919

@@ -94,6 +94,7 @@ def __init__(
9494
self._attr_unique_id = f"{self._device_address}_{description.key}"
9595

9696
@override
97+
@exception_handler
9798
async def async_select_option(self, option: str) -> None:
9899
return await self.entity_description.set_value_fn(self._device, option)
99100

‎homeassistant/components/eheimdigital/strings.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,10 @@
101101
"name": "Night start time"
102102
}
103103
}
104+
},
105+
"exceptions": {
106+
"communication_error": {
107+
"message": "An error occurred while communicating with the EHEIM Digital hub: {error}"
108+
}
104109
}
105110
}

‎homeassistant/components/eheimdigital/switch.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1111

1212
from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator
13-
from .entity import EheimDigitalEntity
13+
from .entity import EheimDigitalEntity, exception_handler
1414

1515
# Coordinator is used to centralize the data updates
1616
PARALLEL_UPDATES = 0
@@ -58,10 +58,12 @@ def __init__(
5858
self._async_update_attrs()
5959

6060
@override
61+
@exception_handler
6162
async def async_turn_off(self, **kwargs: Any) -> None:
6263
await self._device.set_active(active=False)
6364

6465
@override
66+
@exception_handler
6567
async def async_turn_on(self, **kwargs: Any) -> None:
6668
await self._device.set_active(active=True)
6769

‎homeassistant/components/eheimdigital/time.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1616

1717
from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator
18-
from .entity import EheimDigitalEntity
18+
from .entity import EheimDigitalEntity, exception_handler
1919

2020
PARALLEL_UPDATES = 0
2121

@@ -122,6 +122,7 @@ def __init__(
122122
self._attr_unique_id = f"{device.mac_address}_{description.key}"
123123

124124
@override
125+
@exception_handler
125126
async def async_set_value(self, value: time) -> None:
126127
"""Change the time."""
127128
return await self.entity_description.set_value_fn(self._device, value)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /