Skip to content

Commit

Permalink
fix: fix give up condition for command retries
Browse files Browse the repository at this point in the history
Commands will no longer retry if we needs user interaction
(e.g., bad credentials)
  • Loading branch information
alandtse committed May 1, 2021
1 parent ef46aef commit 8954299
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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 8954299

Please sign in to comment.