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: Improve wake up retry logic #379

Merged
merged 3 commits into from
Dec 9, 2022
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
29 changes: 20 additions & 9 deletions teslajsonpy/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
from typing import Optional

from teslajsonpy.exceptions import HomelinkError
from teslajsonpy.exceptions import HomelinkError, TeslaException

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -656,7 +656,7 @@ async def _send_command(
additional_path_vars: dict = None,
wake_if_asleep: bool = True,
**kwargs,
) -> dict:
) -> Optional[dict]:
"""Wrap commands sent to Tesla API.

Args
Expand All @@ -671,11 +671,23 @@ async def _send_command(
path_vars.update(additional_path_vars)

_LOGGER.debug("Sending command: %s", name)
data = await self._controller.api(
name, path_vars=path_vars, wake_if_asleep=wake_if_asleep, **kwargs
)
_LOGGER.debug("Response from command %s: %s", name, data)
return data
try:
data = await self._controller.api(
name, path_vars=path_vars, wake_if_asleep=wake_if_asleep, **kwargs
)
_LOGGER.debug("Response from command %s: %s", name, data)
return data
except TeslaException as ex:
if ex.code == 408 and not wake_if_asleep and not self.is_on:
# 408 due to being asleep and we didn't try to wake it
_LOGGER.debug(
"Vehicle unavailable for command: %s, car state: %s, wake_if_asleep: %s",
name,
self.state,
wake_if_asleep,
)
return None
raise ex

def _get_lat_long(self) -> float:
"""Get current latitude and longitude."""
Expand Down Expand Up @@ -981,8 +993,7 @@ async def stop_charge(self) -> None:

async def wake_up(self) -> None:
"""Send command to wake up."""
# Avoid wake wrapper loop for wake command
await self._send_command("WAKE_UP", wake_if_asleep=False)
await self._controller.wake_up(car_id=self.id)

async def toggle_trunk(self) -> None:
"""Actuate rear trunk."""
Expand Down
2 changes: 2 additions & 0 deletions teslajsonpy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
DRIVING_INTERVAL = 60 # interval when driving detected
UPDATE_INTERVAL = 300 # Default polling interval for vehicle
WEBSOCKET_TIMEOUT = 11 # time for websocket to timeout
WAKE_TIMEOUT = 60 # max time to wait for vehicle to wake
WAKE_CHECK_INTERVAL = 2 # wait period between wake checks after a wake request
RELEASE_NOTES_URL = "https://teslascope.com/teslapedia/software/"
AUTH_DOMAIN = "https://auth.tesla.com"
API_URL = "https://owner-api.teslamotors.com"
Expand Down
Loading