Fix tradfri coordinator error handling (#65204)

pull/65378/head
Martin Hjelmare 2022-02-01 18:09:51 +01:00 committed by GitHub
parent 65ea54927d
commit 3697f5611c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 20 deletions

View File

@ -51,27 +51,23 @@ class TradfriDeviceDataUpdateCoordinator(DataUpdateCoordinator[Device]):
@callback @callback
def _observe_update(self, device: Device) -> None: def _observe_update(self, device: Device) -> None:
"""Update the coordinator for a device when a change is detected.""" """Update the coordinator for a device when a change is detected."""
self.update_interval = timedelta(seconds=SCAN_INTERVAL) # Reset update interval
self.async_set_updated_data(data=device) self.async_set_updated_data(data=device)
@callback @callback
def _exception_callback(self, device: Device, exc: Exception | None = None) -> None: def _exception_callback(self, exc: Exception) -> None:
"""Schedule handling exception..""" """Schedule handling exception.."""
self.hass.async_create_task(self._handle_exception(device=device, exc=exc)) self.hass.async_create_task(self._handle_exception(exc))
async def _handle_exception( async def _handle_exception(self, exc: Exception) -> None:
self, device: Device, exc: Exception | None = None
) -> None:
"""Handle observe exceptions in a coroutine.""" """Handle observe exceptions in a coroutine."""
self._exception = ( # Store exception so that it gets raised in _async_update_data
exc # Store exception so that it gets raised in _async_update_data self._exception = exc
)
_LOGGER.debug("Observation failed for %s, trying again", device, exc_info=exc) _LOGGER.debug(
self.update_interval = timedelta( "Observation failed for %s, trying again", self.device, exc_info=exc
seconds=5 )
) # Change interval so we get a swift refresh # Change interval so we get a swift refresh
self.update_interval = timedelta(seconds=5)
await self.async_request_refresh() await self.async_request_refresh()
async def _async_update_data(self) -> Device: async def _async_update_data(self) -> Device:
@ -82,10 +78,7 @@ class TradfriDeviceDataUpdateCoordinator(DataUpdateCoordinator[Device]):
self._exception = None # Clear stored exception self._exception = None # Clear stored exception
raise exc # pylint: disable-msg=raising-bad-type raise exc # pylint: disable-msg=raising-bad-type
except RequestError as err: except RequestError as err:
raise UpdateFailed( raise UpdateFailed(f"Error communicating with API: {err}.") from err
f"Error communicating with API: {err}. Try unplugging and replugging your "
f"IKEA gateway."
) from err
if not self.data or not self.last_update_success: # Start subscription if not self.data or not self.last_update_success: # Start subscription
try: try:
@ -95,8 +88,11 @@ class TradfriDeviceDataUpdateCoordinator(DataUpdateCoordinator[Device]):
duration=0, duration=0,
) )
await self.api(cmd) await self.api(cmd)
except RequestError as exc: except RequestError as err:
await self._handle_exception(device=self.device, exc=exc) raise UpdateFailed(f"Error communicating with API: {err}.") from err
# Reset update interval
self.update_interval = timedelta(seconds=SCAN_INTERVAL)
return self.device return self.device