mirror of
https://github.com/home-assistant/core.git
synced 2025-05-28 09:47:09 +00:00

* Remove async_remove_listener This avoids the ambuigity as to what happens if same callback is added multiple times. * Keep track of a context for each listener This allow a update coordinator to adapt what data to request on update from the backing service based on which entities are enabled. * Clone list before calling callbacks The callbacks can end up unregistering and modifying the dict while iterating. * Only yield actual values * Add a test for update context * Factor out iteration of _listeners to helper * Verify context is passed to coordinator * Switch to Any as type instead of object * Remove function which use was dropped earliers The use was removed in 8bee25c938a123f0da7569b4e2753598d478b900
29 lines
913 B
Python
29 lines
913 B
Python
"""Helpers for WLED."""
|
|
|
|
from wled import WLEDConnectionError, WLEDError
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
|
|
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.async_update_listeners()
|
|
|
|
except WLEDConnectionError as error:
|
|
self.coordinator.last_update_success = False
|
|
self.coordinator.async_update_listeners()
|
|
raise HomeAssistantError("Error communicating with WLED API") from error
|
|
|
|
except WLEDError as error:
|
|
raise HomeAssistantError("Invalid response from WLED API") from error
|
|
|
|
return handler
|