Skip to content

Commit

Permalink
fix(websocket): fix reconnect on disconnect
Browse files Browse the repository at this point in the history
Closes #397
  • Loading branch information
alandtse committed Oct 8, 2019
1 parent 5b28532 commit 91a6b57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ async def ws_connect() -> WebsocketEchoClient:
_LOGGER.debug("%s: Websocket creation failed: %s",
hide_email(email),
exception_)
return
return websocket

async def ws_handler(message_obj):
Expand Down Expand Up @@ -755,32 +756,47 @@ async def ws_handler(message_obj):

async def ws_open_handler():
"""Handle websocket open."""
import time
email: Text = login_obj.email
_LOGGER.debug("%s: Websocket succesfully connected",
hide_email(email))
(hass.data[DATA_ALEXAMEDIA]
['accounts'][email]['websocketerror']) = 0 # set errors to 0
(hass.data[DATA_ALEXAMEDIA]['accounts']
[email]['websocket_lastattempt']) = time.time()

async def ws_close_handler():
"""Handle websocket close.
This should attempt to reconnect up to 5 times
"""
from asyncio import sleep
import time
email: Text = login_obj.email
errors: int = (hass.data
[DATA_ALEXAMEDIA]['accounts'][email]['websocketerror'])
delay: int = 5 * 2 ** errors
if (errors < 5):
last_attempt = (hass.data[DATA_ALEXAMEDIA]['accounts']
[email]['websocket_lastattempt'])
now = time.time()
_LOGGER.debug("diff %s: delay %s",
(now - last_attempt),
delay)
if ((now - last_attempt) < delay):
return
while (errors < 5 and not (hass.data[DATA_ALEXAMEDIA]['accounts']
[email]['websocket'])):
_LOGGER.debug("%s: Websocket closed; reconnect #%i in %is",
hide_email(email),
errors,
delay)
await sleep(delay)
if (not (hass.data
[DATA_ALEXAMEDIA]['accounts'][email]['websocket'])):
(hass.data[DATA_ALEXAMEDIA]['accounts']
[email]['websocket_lastattempt']) = time.time()
(hass.data[DATA_ALEXAMEDIA]['accounts']
[email]['websocket']) = await ws_connect()
errors += 1
delay = 5 * 2 ** errors
else:
_LOGGER.debug("%s: Websocket closed; retries exceeded; polling",
hide_email(email))
Expand Down Expand Up @@ -828,6 +844,10 @@ async def ws_error_handler(message):
['entities']) = {'media_player': {}}
(hass.data[DATA_ALEXAMEDIA]
['accounts'][email]['new_devices']) = True # force initial update
(hass.data[DATA_ALEXAMEDIA]
['accounts'][email]['websocket_lastattempt']) = 0
(hass.data[DATA_ALEXAMEDIA]
['accounts'][email]['websocketerror']) = 0 # set errors to 0
(hass.data[DATA_ALEXAMEDIA]['accounts'][email]['websocket']) = \
await ws_connect()
await update_devices(login_obj, no_throttle=True)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alexa_media/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"documentation": "https://github.com/custom-components/alexa_media_player/wiki",
"dependencies": [],
"codeowners": ["@keatontaylor", "@alandtse"],
"requirements": ["alexapy==1.3.1"],
"requirements": ["alexapy==1.3.2"],
"homeassistant": "0.96.0"
}

0 comments on commit 91a6b57

Please sign in to comment.