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,41 +474,39 @@ class SimpliSafe:
tasks = [update_system(system) for system in self.systems.values()] tasks = [update_system(system) for system in self.systems.values()]
def cancel_tasks(): results = await asyncio.gather(*tasks, return_exceptions=True)
"""Cancel tasks and ensure their cancellation is processed.""" for result in results:
for task in tasks: if isinstance(result, InvalidCredentialsError):
task.cancel() if self._emergency_refresh_token_used:
_LOGGER.error(
"SimpliSafe authentication disconnected. Please restart HASS."
)
remove_listener = self._hass.data[DOMAIN][DATA_LISTENER].pop(
self._config_entry.entry_id
)
remove_listener()
return
try: _LOGGER.warning("SimpliSafe cloud error; trying stored refresh token")
await asyncio.gather(*tasks) self._emergency_refresh_token_used = True
except InvalidCredentialsError: return await self._api.refresh_access_token(
cancel_tasks() self._config_entry.data[CONF_TOKEN]
)
if self._emergency_refresh_token_used: if isinstance(result, SimplipyError):
_LOGGER.error( _LOGGER.error("SimpliSafe error while updating: %s", result)
"SimpliSafe authentication disconnected. Please restart HASS."
)
remove_listener = self._hass.data[DOMAIN][DATA_LISTENER].pop(
self._config_entry.entry_id
)
remove_listener()
return return
_LOGGER.warning("SimpliSafe cloud error; trying stored refresh token") if isinstance(result, SimplipyError):
self._emergency_refresh_token_used = True _LOGGER.error("Unknown error while updating: %s", result)
return await self._api.refresh_access_token( return
self._config_entry.data[CONF_TOKEN]
)
except SimplipyError as err:
cancel_tasks()
_LOGGER.error("SimpliSafe error while updating: %s", err)
return
except Exception as err: # pylint: disable=broad-except
cancel_tasks()
_LOGGER.error("Unknown error while updating: %s", err)
return
if self._api.refresh_token_dirty: 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( _async_save_refresh_token(
self._hass, self._config_entry, self._api.refresh_token self._hass, self._config_entry, self._api.refresh_token
) )