From ab4e6818be1f17aa96efe3b4b79e58724d1a3c99 Mon Sep 17 00:00:00 2001 From: Seb Ruiz Date: Sun, 30 Aug 2020 18:34:49 +1000 Subject: [PATCH] Always try reconnect to websocket if connection is broken --- custom_components/bhyve/pybhyve/websocket.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/custom_components/bhyve/pybhyve/websocket.py b/custom_components/bhyve/pybhyve/websocket.py index 16c0ed5..c3e385c 100644 --- a/custom_components/bhyve/pybhyve/websocket.py +++ b/custom_components/bhyve/pybhyve/websocket.py @@ -111,8 +111,7 @@ async def running(self): elif msg.type == WSMsgType.CLOSE: _LOGGER.debug("Websocket received CLOSE message, ignoring") - # await self._ws.close() - # break + break elif msg.type == WSMsgType.CLOSED: _LOGGER.error("websocket connection closed") @@ -126,27 +125,26 @@ async def running(self): if self._ws.closed: _LOGGER.info("Websocket closed? %s", self._ws.closed) + self.state = STATE_STOPPED if self._ws.exception(): _LOGGER.warning( "Websocket exception: %s", self._ws.exception() or "Unknown" ) + self.state = STATE_STOPPED except aiohttp.ClientConnectorError: _LOGGER.error("Client connection error; state: %s", self.state) - if self.state != STATE_STOPPED: - self.retry() + self.retry() # pylint: disable=broad-except except Exception as err: _LOGGER.error("Unexpected error %s", err) - if self.state != STATE_STOPPED: - self.retry() + self.retry() else: - if self.state != STATE_STOPPED: - _LOGGER.info("Reconnecting websocket; state: %s", self.state) - self.retry() + _LOGGER.info("Reconnecting websocket; state: %s", self.state) + self.retry() async def stop(self): """Close websocket connection.""" @@ -170,4 +168,6 @@ async def send(self, payload): if not self._ws.closed: await self._ws.send_str(json.dumps(payload)) else: - _LOGGER.warning("Tried to send message whilst websocket closed; state: %s", self.state) + _LOGGER.warning( + "Tried to send message whilst websocket closed; state: %s", self.state + )