Fix error where SimpliSafe websocket would disconnect and not reconnect (#32199)

* Fix error where SimpliSafe websocket would disconnect and not reconnect

* Await
This commit is contained in:
Aaron Bach 2020-02-26 04:19:14 -07:00 committed by GitHub
parent 5a67d73a37
commit e435f6eb67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -474,16 +474,9 @@ class SimpliSafe:
tasks = [update_system(system) for system in self.systems.values()]
def cancel_tasks():
"""Cancel tasks and ensure their cancellation is processed."""
for task in tasks:
task.cancel()
try:
await asyncio.gather(*tasks)
except InvalidCredentialsError:
cancel_tasks()
results = await asyncio.gather(*tasks, return_exceptions=True)
for result in results:
if isinstance(result, InvalidCredentialsError):
if self._emergency_refresh_token_used:
_LOGGER.error(
"SimpliSafe authentication disconnected. Please restart HASS."
@ -499,16 +492,21 @@ class SimpliSafe:
return await self._api.refresh_access_token(
self._config_entry.data[CONF_TOKEN]
)
except SimplipyError as err:
cancel_tasks()
_LOGGER.error("SimpliSafe error while updating: %s", err)
if isinstance(result, SimplipyError):
_LOGGER.error("SimpliSafe error while updating: %s", result)
return
except Exception as err: # pylint: disable=broad-except
cancel_tasks()
_LOGGER.error("Unknown error while updating: %s", err)
if isinstance(result, SimplipyError):
_LOGGER.error("Unknown error while updating: %s", result)
return
if self._api.refresh_token_dirty:
# Reconnect the websocket:
await self._api.websocket.async_disconnect()
await self._api.websocket.async_connect()
# Save the new refresh token:
_async_save_refresh_token(
self._hass, self._config_entry, self._api.refresh_token
)