Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid retry on sleep/unavailable when the car is asleep with wake_if_asleep=False #416

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions teslajsonpy/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,7 @@ async def remote_auto_steering_wheel_heat_climate_request(
"""

data = await self._send_command(
"REMOTE_AUTO_STEERING_WHEEL_HEAT_CLIMATE_REQUEST",
on=enable
"REMOTE_AUTO_STEERING_WHEEL_HEAT_CLIMATE_REQUEST", on=enable
)
if data and data["response"]["result"] is True:
params = {"auto_steering_wheel_heat": enable}
Expand Down Expand Up @@ -1017,7 +1016,9 @@ async def set_heated_steering_wheel_level(self, level: int) -> None:
def get_heated_steering_wheel_level(self) -> int:
"""Return the status of the heated steering wheel."""
if self.data_available:
return self._vehicle_data.get("climate_state", {}).get("steering_wheel_heat_level")
return self._vehicle_data.get("climate_state", {}).get(
"steering_wheel_heat_level"
)
return None

async def set_hvac_mode(self, value: str) -> None:
Expand Down
41 changes: 37 additions & 4 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
WAKE_TIMEOUT,
)
from teslajsonpy.energy import EnergySite, PowerwallSite, SolarPowerwallSite, SolarSite
from teslajsonpy.exceptions import TeslaException, custom_retry, custom_wait
from teslajsonpy.exceptions import (
TeslaException,
custom_retry,
custom_retry_except_unavailable,
custom_wait,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -743,7 +748,11 @@ async def _get_and_process_battery_summary(
cur_time - last_update,
ONLINE_INTERVAL,
)
if force or cur_time - last_update >= ONLINE_INTERVAL and update_vehicles:
if (
force
or cur_time - last_update >= ONLINE_INTERVAL
and update_vehicles
):
cars = await self.get_vehicles()
for car in cars:
self.set_id_vin(car_id=car["id"], vin=car["vin"])
Expand Down Expand Up @@ -1333,7 +1342,11 @@ async def api(
return response

# Perform request using given keyword arguments as parameters
return await self.__post_with_retries("", method=method, data=kwargs, url=uri)
# wake_if_asleep is False so we do not retry if the car is asleep
# or if the car is unavailable
return await self.__post_with_retries_except_unavailable(
"", method=method, data=kwargs, url=uri
)

@retry(
wait=custom_wait,
Expand All @@ -1342,5 +1355,25 @@ async def api(
reraise=True,
)
async def __post_with_retries(self, command, method="post", data=None, url=""):
"""Call connection.post with retries for common exceptions."""
"""Call connection.post with retries for common exceptions.

Retries if the car is unavailable.
"""
return await self.__connection.post(command, method=method, data=data, url=url)

@retry(
wait=custom_wait,
retry=custom_retry_except_unavailable,
stop=stop_after_delay(MAX_API_RETRY_TIME),
reraise=True,
)
async def __post_with_retries_except_unavailable(
self, command, method="post", data=None, url=""
):
"""Call connection.post with retries for common exceptions.

Does not retry if the car is unavailable. This should be
used when wake_if_asleep is False since its unlikely the
car will suddenly become available if its offline/sleep.
"""
return await self.__connection.post(command, method=method, data=data, url=url)
19 changes: 19 additions & 0 deletions teslajsonpy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ class HomelinkError(TeslaException):
pass


def custom_retry_except_unavailable(retry_state: RetryCallState) -> bool:
"""Determine whether Tenacity should retry.

Args
retry_state (RetryCallState): Provided by Tenacity

Returns
bool: whether or not to retry

"""
if not custom_retry(retry_state):
return False
ex = retry_state.outcome.exception()
if isinstance(ex, TeslaException):
if ex.code == 408: # "VEHICLE_UNAVAILABLE"
return False
return True


def custom_retry(retry_state: RetryCallState) -> bool:
"""Determine whether Tenacity should retry.