hass-core/homeassistant/components/wled/helpers.py

29 lines
901 B
Python
Raw Normal View History

2021-06-09 20:15:46 +02:00
"""Helpers for WLED."""
from wled import WLEDConnectionError, WLEDError
2022-05-24 16:30:41 +02:00
from homeassistant.exceptions import HomeAssistantError
2021-06-09 20:15:46 +02:00
def wled_exception_handler(func):
"""Decorate WLED calls to handle WLED exceptions.
A decorator that wraps the passed in function, catches WLED errors,
and handles the availability of the device in the data coordinator.
"""
async def handler(self, *args, **kwargs):
try:
await func(self, *args, **kwargs)
self.coordinator.update_listeners()
except WLEDConnectionError as error:
self.coordinator.last_update_success = False
self.coordinator.update_listeners()
2022-05-24 16:30:41 +02:00
raise HomeAssistantError("Error communicating with WLED API") from error
2021-06-09 20:15:46 +02:00
except WLEDError as error:
2022-05-24 16:30:41 +02:00
raise HomeAssistantError("Invalid response from WLED API") from error
2021-06-09 20:15:46 +02:00
return handler