Skip to content

Commit

Permalink
Catch websocket exception and relative fix urls (#10)
Browse files Browse the repository at this point in the history
* Catch exception

* Catch server disconnect and log token

* Log websocket messages

* Switch to relative urls

* Remove debugging lines
  • Loading branch information
jacobtomlinson authored May 4, 2020
1 parent d9b4472 commit 65ead3f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions opsdroid_homeassistant/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def __init__(self, config, opsdroid=None):
self.listening = None
self.discovery_info = None
self.token = self.config.get("token")
self.api_url = urllib.parse.urljoin(self.config.get("url"), "/api/")
self.api_url = urllib.parse.urljoin(self.config.get("url"), "api/")

# The websocket URL can differ depending on how Home Assistant was installed.
# So we will iterate over the various urls when attempting to connect.
self.websocket_urls = [
urllib.parse.urljoin(self.api_url, "websocket"), # Plain Home Assistant
urllib.parse.urljoin(self.config.get("url"), "/websocket"), # Hassio proxy
urllib.parse.urljoin(self.config.get("url"), "websocket"), # Hassio proxy
]
self.id = 1

Expand All @@ -74,7 +74,11 @@ async def listen(self):
elif msg.type == aiohttp.WSMsgType.ERROR:
break
_LOGGER.info("Home Assistant closed the websocket, retrying...")
except aiohttp.client_exceptions.ClientConnectorError:
except (
aiohttp.client_exceptions.ClientConnectorError,
aiohttp.client_exceptions.WSServerHandshakeError,
aiohttp.client_exceptions.ServerDisconnectedError,
):
_LOGGER.info("Unable to connect to Home Assistant, retrying...")
await asyncio.sleep(1)

Expand Down

0 comments on commit 65ead3f

Please sign in to comment.