...
 
Commits (7)
This diff is collapsed.
......@@ -24,7 +24,11 @@ from yarl import URL
import backoff
from .alexalogin import AlexaLogin
from .errors import AlexapyLoginError, AlexapyTooManyRequestsError
from .errors import (
AlexapyLoginError,
AlexapyTooManyRequestsError,
AlexapyConnectionError,
)
from .helpers import _catch_all_exceptions, hide_email
_LOGGER = logging.getLogger(__name__)
......@@ -64,14 +68,14 @@ class AlexaAPI:
ex,
)
@_catch_all_exceptions
@backoff.on_exception(
backoff.expo,
AlexapyTooManyRequestsError,
(AlexapyTooManyRequestsError, AlexapyConnectionError),
max_time=60,
max_tries=5,
logger=__name__,
)
@_catch_all_exceptions
async def _request(
self,
method: Text,
......@@ -132,14 +136,14 @@ class AlexaAPI:
return await self._request("delete", uri, data)
@staticmethod
@_catch_all_exceptions
@backoff.on_exception(
backoff.expo,
AlexapyTooManyRequestsError,
(AlexapyTooManyRequestsError, AlexapyConnectionError),
max_time=60,
max_tries=5,
logger=__name__,
)
@_catch_all_exceptions
async def _static_request(
method: Text,
login: AlexaLogin,
......
......@@ -617,7 +617,7 @@ class AlexaLogin:
site = status["ap_error_href"]
_LOGGER.debug("Found post url to get; forcing get to %s", site)
self._lastreq = None
elif formsite != "get":
elif formsite and formsite != "get":
site = formsite
_LOGGER.debug("Found post url to %s", site)
return site
......
......@@ -256,6 +256,9 @@ class WebsocketEchoClient:
await self.websocket.send_bytes(self._encode_gw_handshake())
await asyncio.sleep(0.1)
await self.websocket.send_bytes(self._encode_gw_register())
await asyncio.sleep(0.1)
await self.websocket.send_bytes(self._encode_ping())
await asyncio.sleep(0.5)
if not self.websocket.closed:
await self.open_callback()
......@@ -297,3 +300,15 @@ class WebsocketEchoClient:
msg += '{"command":"REGISTER_CONNECTION"}' # Message UUID
msg += "FABE"
return bytes(msg, "utf-8")
def _encode_ping(self) -> bytes:
# pylint: disable=no-self-use
_LOGGER.debug("Encoding PING.")
msg = "MSG 0x00000065 " # MSG channel
msg += "0x0e414e47 f 0x00000001 " # Message number with no cont
msg += "0xbc2fbb5f " # Checksum
msg += "0x00000062 " # Content Length
msg += "PIN30" # Message content
msg += "FABE"
return bytes(msg, "utf-8")
# MSG 0x00000065 0x0e414e47 f 0x00000001 0xbc2fbb5f 0x00000062 PIN" + 30 + "FABE"
......@@ -11,6 +11,7 @@ https://gitlab.com/keatontaylor/alexapy
"""
import logging
from json import JSONDecodeError
from json.decoder import JSONDecodeError as JSONDecodeError2
from aiohttp import ClientConnectionError
......@@ -66,7 +67,7 @@ def _catch_all_exceptions(func):
template = "An exception of type {0} occurred." " Arguments:\n{1!r}"
try:
return func(*args, **kwargs)
except ClientConnectionError as ex:
except (ClientConnectionError, KeyError) as ex:
message = template.format(type(ex).__name__, ex.args)
_LOGGER.error(
"%s.%s: A connection error occured: %s",
......@@ -75,7 +76,7 @@ def _catch_all_exceptions(func):
message,
)
raise AlexapyConnectionError
except JSONDecodeError as ex:
except (JSONDecodeError, JSONDecodeError2) as ex:
message = template.format(type(ex).__name__, ex.args)
_LOGGER.error(
"%s.%s: A login error occured: %s",
......