Skip to content

Commit

Permalink
Merge branch 'async' into PyISY_beta
Browse files Browse the repository at this point in the history
  • Loading branch information
shbatm committed May 24, 2020
2 parents 11acd6e + ff447f7 commit 770ba3e
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions pyisy/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"Origin": "com.universal-devices.websockets.isy",
}
WS_HEARTBEAT = 60
WS_TIMEOUT = aiohttp.ClientTimeout(total=60, connect=60, sock_connect=60, sock_read=60)
WS_TIMEOUT = 60
WS_MAX_RETRIES = 4
WS_RETRY_BACKOFF = [0.01, 1, 10, 30, 60] # Seconds

Expand Down Expand Up @@ -403,7 +403,7 @@ def heartbeat_time(self):
async def _websocket_guardian(self):
"""Watch and reset websocket connection if no messages received."""
while self.status != ES_STOP_UPDATES:
asyncio.sleep(self._hbwait)
await asyncio.sleep(self._hbwait)
if self.heartbeat_time > self._hbwait:
_LOGGER.debug("Websocket missed a heartbeat, resetting connection.")
self.status = ES_LOST_STREAM_CONNECTION
Expand Down Expand Up @@ -474,52 +474,38 @@ async def websocket(self, retries=0):
) as ws:
self.status = ES_CONNECTED
retries = 0
_LOGGER.debug("Successfully connected to websocket... %s", self.status)
_LOGGER.debug("Successfully connected to websocket.")

async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
await self._route_message(msg.data)

elif msg.type == aiohttp.WSMsgType.CLOSED:
_LOGGER.warning("Websocket connection closed: %s", msg.data)
await ws.close()
break

elif msg.type == aiohttp.WSMsgType.ERROR:
_LOGGER.error("Websocket error: %s", ws.exception())
break

elif msg.type == aiohttp.WSMsgType.PING:
_LOGGER.debug("Websocket ping received.")
ws.pong()

elif msg.type == aiohttp.WSMsgType.PONG:
_LOGGER.debug("Websocket pong received.")

elif msg.type == aiohttp.WSMsgType.BINARY:
_LOGGER.warning("Unexpected binary message received.")

self.status = ES_DISCONNECTED

except (
aiohttp.ClientConnectorError,
aiohttp.ClientOSError,
aiohttp.client_exceptions.ServerDisconnectedError,
):
_LOGGER.debug("ISY not ready or closed connection. Retrying.")
await self.reconnect(WS_RETRY_BACKOFF[retries], retries=retries)
except aiohttp.ClientConnectorError:
if self.status != ES_STOP_UPDATES:
_LOGGER.error("Websocket Client Connector Error.")
self.status = ES_LOST_STREAM_CONNECTION
await self.reconnect(RECONNECT_DELAY)
await self.reconnect(WS_RETRY_BACKOFF[retries], retries=retries)
except asyncio.CancelledError:
self.status = ES_DISCONNECTED
except Exception as err:
if self.status != ES_STOP_UPDATES:
_LOGGER.exception("Unexpected error %s", err)
_LOGGER.exception("Unexpected websocket error %s", err)
self.status = ES_LOST_STREAM_CONNECTION
await self.reconnect(RECONNECT_DELAY)
await self.reconnect(WS_RETRY_BACKOFF[retries], retries=retries)
else:
if self.status != ES_STOP_UPDATES:
self.status = ES_DISCONNECTED
await self.reconnect(RECONNECT_DELAY)
await self.reconnect(WS_RETRY_BACKOFF[retries], retries=retries)

0 comments on commit 770ba3e

Please sign in to comment.