Skip to content

Commit

Permalink
fix: track HA shutdown requests
Browse files Browse the repository at this point in the history
The component will now distinguish between a HA shutdown where the session
is closed voluntarily versus an error. This will avoid deleting a good
.pickle file forcing a relogin request. This will also prevent attempts
to use the API after a requested close.
closes #819
  • Loading branch information
alandtse committed Jul 4, 2020
1 parent 7879fd0 commit 5b626ae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ async def async_update_data():
email not in hass.data[DATA_ALEXAMEDIA]["accounts"]
or "login_successful" not in login_obj.status
or login_obj.session.closed
or login_obj.close_requested
):
return
existing_serials = _existing_serials(hass, login_obj)
Expand Down Expand Up @@ -804,6 +805,11 @@ async def ws_close_handler():
import time

email: Text = login_obj.email
if login_obj.close_requested:
_LOGGER.debug(
"%s: Close requested; will not reconnect websocket", hide_email(email)
)
return
errors: int = (hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"])
delay: int = 5 * 2 ** errors
last_attempt = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
Expand Down Expand Up @@ -859,7 +865,9 @@ async def ws_error_handler(message):
type(message),
)
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = None
if login_obj.session.closed or message == "<class 'aiohttp.streams.EofStream'>":
if not login_obj.close_requested and (
login_obj.session.closed or message == "<class 'aiohttp.streams.EofStream'>"
):
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = 5
_LOGGER.debug("%s: Immediate abort on EoFstream", hide_email(email))
return
Expand Down
9 changes: 8 additions & 1 deletion custom_components/alexa_media/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
from typing import Any, Callable, List, Optional, Text

from alexapy import AlexapyLoginError, hide_email
from alexapy import AlexapyLoginCloseRequested, AlexapyLoginError, hide_email
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_component import EntityComponent

Expand Down Expand Up @@ -151,6 +151,13 @@ async def wrapper(*args, **kwargs) -> Any:
instance.check_login_changes()
try:
result = await func(*args, **kwargs)
except AlexapyLoginCloseRequested:
_LOGGER.debug(
"%s.%s: Ignoring attempt to access Alexa after HA shutdown",
func.__module__[func.__module__.find(".") + 1 :],
func.__name__,
)
return None
except AlexapyLoginError as ex:
_LOGGER.debug(
"%s.%s: detected bad login: %s",
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,5 +5,5 @@
"documentation": "https://github.com/custom-components/alexa_media_player/wiki",
"dependencies": ["configurator"],
"codeowners": ["@keatontaylor", "@alandtse"],
"requirements": ["alexapy==1.10.7"]
"requirements": ["alexapy==1.10.8"]
}

0 comments on commit 5b626ae

Please sign in to comment.