Storm Watch silently degrading on stale Fleet API token - #665
Open
MikeBishop wants to merge 2 commits into
Open
Conversation
Token refresh only happened as a side effect of car_api_available(), which is only called from charge/wake/limit commands. Deployments that never send those (BLE-only vehicle control, Powerwall-only monitoring) never refreshed the token at all. Extract the refresh check into TeslaAPI.refreshTokenIfNeeded() and call it periodically from the main loop, independent of any vehicle command.
getStormWatch() only read the bearer token/expiry passively and silently skipped the whole check when stale, caching an empty result for up to cloudUpdateInterval with no log output. Have it call the new refreshTokenIfNeeded() before checking, log the skip path, and fix a latent UnboundLocalError where the except handler referenced r.status_code even when the request itself raised before r was assigned.
Test Results4 456 tests ±0 4 352 ✅ ±0 13m 26s ⏱️ -5s For more details on these failures, see this check. Results for commit 02c56cf. ± Comparison against base commit 827e71d. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Storm Watch (TeslaPowerwall2.getStormWatch()) only ever read the Fleet API bearer token/expiry passively — it never triggered a refresh itself, and the only refresh path in the codebase (TeslaAPI.car_api_available()) was called exclusively from vehicle-command code (charge, wake, apply limit). Any deployment where nothing sends those commands — e.g. relying on BLE for local vehicle control never refreshed the token, so Storm Watch quietly went stale and cached an empty result for up to 30 minutes at a time with zero logging.
Fix: extracted the refresh-if-stale check out of car_api_available() into a standalone TeslaAPI.refreshTokenIfNeeded(), called it directly from getStormWatch(), and added a periodic call to it in the main loop (every 5 minutes) independent of any vehicle command.
(Also fixed a latent UnboundLocalError in the exception handler (referenced r.status_code when the request itself could throw before r was ever assigned) and added a debug-level log line for the previously-silent skip path.)