Keep cloud tokens always valid (#20762)
* Keep auth token always valid * Remove unused refresh_auth message * Capture EndpointConnectionError * Lint
This commit is contained in:
1 parent
b1faad0a50
commit
2733919cd8
5 files changed
+122
-36
No files matched your search
@@ -62,12 +62,18 @@ class CloudIoT:
|
||||
# Local code waiting for a response
|
||||
self._response_handler = {}
|
||||
self._on_connect = []
|
||||
self._on_disconnect = []
|
||||
|
||||
@callback
|
||||
def register_on_connect(self, on_connect_cb):
|
||||
"""Register an async on_connect callback."""
|
||||
self._on_connect.append(on_connect_cb)
|
||||
|
||||
@callback
|
||||
def register_on_disconnect(self, on_disconnect_cb):
|
||||
"""Register an async on_disconnect callback."""
|
||||
self._on_disconnect.append(on_disconnect_cb)
|
||||
|
||||
@property
|
||||
def connected(self):
|
||||
"""Return if we're currently connected."""
|
||||
@@ -102,6 +108,17 @@ class CloudIoT:
|
||||
# Still adding it here to make sure we can always reconnect
|
||||
_LOGGER.exception("Unexpected error")
|
||||
|
||||
if self.state == STATE_CONNECTED and self._on_disconnect:
|
||||
try:
|
||||
yield from asyncio.wait([
|
||||
cb() for cb in self._on_disconnect
|
||||
])
|
||||
except Exception: # pylint: disable=broad-except
|
||||
# Safety net. This should never hit.
|
||||
# Still adding it here to make sure we don't break the flow
|
||||
_LOGGER.exception(
|
||||
"Unexpected error in on_disconnect callbacks")
|
||||
|
||||
if self.close_requested:
|
||||
break
|
||||
|
||||
@@ -192,7 +209,13 @@ class CloudIoT:
|
||||
self.state = STATE_CONNECTED
|
||||
|
||||
if self._on_connect:
|
||||
yield from asyncio.wait([cb() for cb in self._on_connect])
|
||||
try:
|
||||
yield from asyncio.wait([cb() for cb in self._on_connect])
|
||||
except Exception: # pylint: disable=broad-except
|
||||
# Safety net. This should never hit.
|
||||
# Still adding it here to make sure we don't break the flow
|
||||
_LOGGER.exception(
|
||||
"Unexpected error in on_connect callbacks")
|
||||
|
||||
while not client.closed:
|
||||
msg = yield from client.receive()
|
||||
@@ -326,11 +349,6 @@ async def async_handle_cloud(hass, cloud, payload):
|
||||
await cloud.logout()
|
||||
_LOGGER.error("You have been logged out from Home Assistant cloud: %s",
|
||||
payload['reason'])
|
||||
elif action == 'refresh_auth':
|
||||
# Refresh the auth token between now and payload['seconds']
|
||||
hass.helpers.event.async_call_later(
|
||||
random.randint(0, payload['seconds']),
|
||||
lambda now: auth_api.check_token(cloud))
|
||||
else:
|
||||
_LOGGER.warning("Received unknown cloud action: %s", action)
|
||||
|
||||
|
||||
Reference in new issue
Block a user