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

Cache vehicle.is_awake() state for 2 seconds #619

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RichieB2B
Copy link

TWCManager checks my 2018 Model S (old car, does not support telemetry) every minute if it is needs to be charged. car_api_charge() calls both car_api_available() and vehicle.ready(). car_api_available() also calls vehicle.ready() which calls vehicle.is_awake() which makes the actual API calls.

This results in 2 API calls in rapid succession every minute:

2025-03-02 08:21:47,839 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:21:47,969 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:22:48,669 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:22:48,772 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:23:49,529 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:23:49,648 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:24:50,439 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>
2025-03-02 08:24:50,558 - 🚗 TeslaAPI 13 Car API cmd https://192.168.1.2:4443/api/1/vehicles/TESLA_VIN <Response [200]>

I did not want to mess with the logic so I implemented a 2 seconds cache for the awake state.

@MikeBishop
Copy link
Collaborator

I'd have to look, but I had thought there was already a two-minute cache for known-online state. Did we factor that out at some point?

@RichieB2B
Copy link
Author

I did not find a cache for this (in code or in the logs). 2 minutes actually makes more sense than 2 seconds though. :-)

@RichieB2B RichieB2B force-pushed the awake-cache branch 2 times, most recently from b3bf898 to f596237 Compare March 4, 2025 20:12
@MikeBishop
Copy link
Collaborator

Okay, took a look at the existing logic. So any API access that requires going all the way to the vehicle sets provesOnline in the API call. That causes self.lastAPIAccessTime to be updated. vehicle.ready() returns True if it's less than two minutes past self.lastAPIAccessTime, but calls self.is_awake() otherwise.

I think the issue is that the current design only caches positive results; i.e. if the car was asleep, we're not conservative about how frequently we re-check whether it's woken up yet. But once we've seen it online, we stop polling for at least two minutes, because that's the minimum observed time for the vehicle to stay online-but-idle after an API interaction without needing to wake again.

And to some extent that makes sense -- the original design was intended to avoid unnecessary wakes, not unnecessary API calls.

@RichieB2B
Copy link
Author

RichieB2B commented Mar 5, 2025

That makes some sense. This API call is not charged (pun intended), but calling it twice within a few ms seems abusive nonetheless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants