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