-
Notifications
You must be signed in to change notification settings - Fork 757
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: use product api endpoint to fetch vehicles #3630
fix: use product api endpoint to fetch vehicles #3630
Conversation
✅ Deploy Preview for teslamate ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
The concern I have with this is if you have other Tesla products, e.g. solar, they may get returned by the new API, and this could cause endless confusion if we try to process that as a car. If so, we might need some way of filtering the list returned so it only contains cars. Unfortunately, not something I can test. |
100% that's why it's WIP, only wanted to test if we think in the right direction in general. |
Testing for the presence of the vin and vehicle_id fields in the product_list response might be a good way to filter vehicles from the product list in the presence of powerwalls or solar. See the bottom part of tdorssers/TeslaPy#156 (comment) |
Agreed! I am somewhat sure the Tesla App checks primarily for "vehicle_id" to determine if a product from api/1/products should be tried to be loaded as a vehicle. From decompiled apk, shortened for readability:
|
I think the solution would be something like: diff --git a/lib/tesla_api/vehicle.ex b/lib/tesla_api/vehicle.ex
index 9c25743d..c39c475b 100644
--- a/lib/tesla_api/vehicle.ex
+++ b/lib/tesla_api/vehicle.ex
@@ -30,7 +30,7 @@ defmodule TeslaApi.Vehicle do
end
TeslaApi.get(endpoint_url <> "/api/1/products", opts: [access_token: auth.token])
- |> handle_response(transform: &result/1)
+ |> handle_response(transform: &list_result/1)
end
def get(%Auth{} = auth, id) do
@@ -61,6 +61,12 @@ defmodule TeslaApi.Vehicle do
|> handle_response(transform: &result/1)
end
+ def list_result(result) do
+ result
+ |> Enum.filter(fn(x) -> Map.has_key?(x, "vehicle_id") end)
+ |> Enum.map(&result/1)
+ end
+
def result(v) do
%__MODULE__{
id: v["id"],
@@ -88,7 +94,7 @@ defmodule TeslaApi.Vehicle do
case env do
%Tesla.Env{status: status, body: %{"response" => res}} when status in 200..299 ->
transform = Keyword.get(opts, :transform, & &1)
- {:ok, if(is_list(res), do: Enum.map(res, transform), else: transform.(res))}
+ {:ok, transform.(res)}
%Tesla.Env{status: 401} = env ->
{:error, %Error{reason: :unauthorized, env: env}} This is a bit more invasive then I originally imagined, because originally the transform function was called for every element in the list, but this means we can't filter out list elements. So I changed the transform function to operate on the entire result. I think it should only get a list for the list API call, so this should be OK. We could also do the filtering after the |
I have now tested this, I am not seeing any errors. Does that mean it is OK? ;-) |
It looks like this issue will only affect new installs, old installs will fall back to using the data from the database. But of course, this will be a problem if you have added/removed any vehicles.
|
- teslamate-org/teslamate#3630 Error - Tesla API said: '412 Precondition Failed' at .../Tesla/API.pm line 713. Not an ARRAY reference at .../Tesla/Vehicle.pm line 104.
Tested and it works. No other products on my account but only cars. |
…ly vehicles in products api endpoint
I should have opened a branch in the main repo... Anyway, I included brians suggestion, ready for testing on test machines, DO NOT use on Production nor stop your Production: https://github.com/teslamate-org/teslamate/pkgs/container/teslamate/171474733?tag=pr-3630 So you can replace in your docker-compose.yml
with
To test it once the GitHub runner is completed. Edit: build successful. |
This fixed my drives, did a quick'n'dirty edit as according to the PR and the last three drives where complete. I do not own other Tesla products, so cannot say if this brings adverse side effects if you have more products than cars. |
I do have a wall charger and vehicle. Tested this on a secondary install and works fine! |
Is there any way I can use this fix on Home Assistant Teslamate Add On? https://github.com/matt-FFFFFF/hassio-addon-teslamate |
Perfect, thanks for testing 🙏🏻 |
FYI: Running this PR image since yesterday without issues. I only have a Tesla car. |
Hi, I upgraded to 1.28.3 and things seem to be working fine, however I get warnings/errors ("car_id=1 [error] Error / :unknown") in my log relating to the vehicle endpoint https://owner-api.teslamotors.com/api/1/vehicles/ I would expect everything would. be using the product endpoint now, is that correct?
FYI |
no you are getting 408 errors, which means to many request |
Yes you are right about the 408. But still the https://owner-api.teslamotors.com/api/1/vehicles endpoint is used I thought that could no longer be used, or is that not correct? |
Please stop asking these kinds of questions or access will really be restricted. TL;DR: Currently everything works fine with TeslaMate and other loggers even without using dev accounts. |
We need top be careful here, we want to ensure that that we are a welcoming community, and are open to feedback from everyone. @bikeymouse The API call used above is The 408 error is the generic error that means your car is unreachable, e.g. maybe because it is asleep, or because it is outside network coverage, or something like that. |
#3629