Skip to content

Commit

Permalink
Merge pull request #833 from alandtse/#819
Browse files Browse the repository at this point in the history
fix: update_coordinator polling on websocket
  • Loading branch information
alandtse authored Jul 6, 2020
2 parents 50113be + 10ceed5 commit 41d6ca6
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,6 @@ 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))
Expand All @@ -801,8 +800,6 @@ async def ws_close_handler():
This should attempt to reconnect up to 5 times
"""
from asyncio import sleep
import time

email: Text = login_obj.email
if login_obj.close_requested:
Expand All @@ -818,9 +815,10 @@ async def ws_close_handler():
now = time.time()
if (now - last_attempt) < delay:
return
while errors < 5 and not (
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"]
):
websocket_enabled: bool = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"websocket"
]
while errors < 5 and not (websocket_enabled):
_LOGGER.debug(
"%s: Websocket closed; reconnect #%i in %is",
hide_email(email),
Expand All @@ -830,22 +828,24 @@ async def ws_close_handler():
hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"websocket_lastattempt"
] = time.time()
hass.data[DATA_ALEXAMEDIA]["accounts"][email][
websocket_enabled = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"websocket"
] = await ws_connect()
errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = (
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] + 1
)
delay = 5 * 2 ** errors
await sleep(delay)
errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
_LOGGER.debug(
"%s: Websocket closed; retries exceeded; polling", hide_email(email)
)
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = None
await asyncio.sleep(delay)
if not websocket_enabled:
_LOGGER.debug(
"%s: Websocket closed; retries exceeded; polling", hide_email(email)
)
coordinator = hass.data[DATA_ALEXAMEDIA]["accounts"][email].get("coordinator")
if coordinator:
coordinator.update_interval = timedelta(seconds=scan_interval)
coordinator.update_interval = timedelta(
seconds=scan_interval * 10 if websocket_enabled else scan_interval
)
await coordinator.async_request_refresh()

async def ws_error_handler(message):
Expand Down Expand Up @@ -902,6 +902,10 @@ async def ws_error_handler(message):
seconds=scan_interval * 10 if websocket_enabled else scan_interval
),
)
else:
coordinator.update_interval = timedelta(
seconds=scan_interval * 10 if websocket_enabled else scan_interval
)
# Fetch initial data so we have data when entities subscribe
_LOGGER.debug("Refreshing coordinator")
await coordinator.async_refresh()
Expand Down

0 comments on commit 41d6ca6

Please sign in to comment.