Skip to content

Commit

Permalink
Merge pull request #199 from zabuldon/dev
Browse files Browse the repository at this point in the history
2021-05-01
  • Loading branch information
alandtse committed May 1, 2021
2 parents 1da98b6 + 20d356d commit e37c90d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
10 changes: 5 additions & 5 deletions teslajsonpy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,12 @@ async def get_authorization_code(
if resp.history:
for item in resp.history:
if (
item.status in [301, 302, 303, 304, 305, 306, 307, 308]
item.status_code in [301, 302, 303, 304, 305, 306, 307, 308]
and resp.url.host != self.auth_domain.host
):
_LOGGER.debug(
"Detected %s redirect from %s to %s; changing proxy host",
item.status,
item.status_code,
item.url.host,
resp.url.host,
)
Expand Down Expand Up @@ -487,10 +487,10 @@ async def get_authorization_code(
resp = await self.websession.post(str(url), data=data)
_process_resp(resp)
await asyncio.sleep(3)
if not resp.history or not URL(resp.history[-1].url).query.get("code"):
if not resp.history or not URL(str(resp.history[-1].url)).query.get("code"):
_LOGGER.debug("Failed to authenticate")
raise IncompleteCredentials("Unable to login with credentials")
code_url = URL(resp.history[-1].url)
code_url = URL(str(resp.history[-1].url))
_LOGGER.debug("Found code %s", code_url.query.get("code"))
return code_url.query.get("code")

Expand Down Expand Up @@ -599,7 +599,7 @@ def get_inputs(soup: BeautifulSoup, searchfield=None) -> Dict[str, str]:
def _process_resp(resp) -> Text:
if resp.history:
for item in resp.history:
_LOGGER.debug("%s: redirected from\n%s", item.method, item.url)
_LOGGER.debug("%s: redirected from\n%s", item.request.method, item.url)
url = str(resp.request.url)
method = resp.request.method
status = resp.status_code
Expand Down
9 changes: 7 additions & 2 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ONLINE_INTERVAL,
SLEEP_INTERVAL,
)
from teslajsonpy.exceptions import RetryLimitError, TeslaException
from teslajsonpy.exceptions import should_giveup, RetryLimitError, TeslaException
from teslajsonpy.homeassistant.battery_sensor import Battery, Range
from teslajsonpy.homeassistant.binary_sensor import (
ChargerConnectionSensor,
Expand Down Expand Up @@ -496,7 +496,12 @@ async def data_request(self, car_id, name, wake_if_asleep=False):
)["response"]

@backoff.on_exception(
min_expo, TeslaException, max_time=60, logger=__name__, min_value=15
min_expo,
TeslaException,
max_time=60,
logger=__name__,
min_value=15,
giveup=should_giveup,
)
async def command(self, car_id, name, data=None, wake_if_asleep=True):
"""Post name command to the car_id.
Expand Down
20 changes: 20 additions & 0 deletions teslajsonpy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,23 @@ class UnknownPresetMode(TeslaException):
"""Class of exceptions for Unknown Preset."""

pass


def should_giveup(ex: TeslaException) -> bool:
"""Test whether the exception should result in a retry.
This is consumed by backoff.
Args
ex (TeslaException): The exception
Returns
bool: whether backoff should give up
"""
return isinstance(ex, (IncompleteCredentials, RetryLimitError)) or ex.code in [
401,
404,
405,
423,
]

0 comments on commit e37c90d

Please sign in to comment.