From 977f423014d5519cd682f62c3efaabb14e9c8a9f Mon Sep 17 00:00:00 2001 From: Adrian Kumpf <8999358+adriankumpf@users.noreply.github.com> Date: Mon, 13 Dec 2021 18:19:20 +0100 Subject: [PATCH] Use new Chinese endpoints Related to #2214 --- lib/tesla_api/stream.ex | 10 ++++-- lib/tesla_api/vehicle.ex | 32 +++++++++++++++---- .../vehicles/vehicle/charging_sync_test.exs | 6 ++-- .../vehicles/vehicle/charging_test.exs | 2 +- .../vehicles/vehicle/streaming_test.exs | 6 ++-- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/tesla_api/stream.ex b/lib/tesla_api/stream.ex index 2665397d..4921c03e 100644 --- a/lib/tesla_api/stream.ex +++ b/lib/tesla_api/stream.ex @@ -15,8 +15,6 @@ defmodule TeslaApi.Stream do disconnects: 0 end - @endpoint_url "wss://streaming.vn.teslamotors.com/streaming/" - @columns ~w(speed odometer soc elevation est_heading est_lat est_lng power shift_state range est_range heading)a @@ -32,7 +30,13 @@ defmodule TeslaApi.Stream do auth: Keyword.fetch!(args, :auth) } - WebSockex.start_link(@endpoint_url, __MODULE__, state, + endpoint_url = + case Auth.region(state.auth) do + :chinese -> "wss://streaming.vn.teslamotors.cn/streaming/" + _global -> "wss://streaming.vn.teslamotors.com/streaming/" + end + + WebSockex.start_link(endpoint_url, __MODULE__, state, socket_connect_timeout: :timer.seconds(15), socket_recv_timeout: :timer.seconds(30), name: :"stream_#{state.vehicle_id}", diff --git a/lib/tesla_api/vehicle.ex b/lib/tesla_api/vehicle.ex index eb106039..a0966688 100644 --- a/lib/tesla_api/vehicle.ex +++ b/lib/tesla_api/vehicle.ex @@ -22,18 +22,38 @@ defmodule TeslaApi.Vehicle do vehicle_config: nil, vehicle_state: nil - def list(%Auth{token: token}) do - TeslaApi.get("/api/1/vehicles", opts: [access_token: token]) + def list(%Auth{} = auth) do + endpoint_url = + case Auth.region(auth) do + :chinese -> "https://owner-api.teslamotors.cn" + _global -> "https://owner-api.teslamotors.com" + end + + TeslaApi.get(endpoint_url <> "/api/1/vehicles", opts: [access_token: auth.token]) |> handle_response(transform: &result/1) end - def get(%Auth{token: token}, id) do - TeslaApi.get("/api/1/vehicles/#{id}", opts: [access_token: token]) + def get(%Auth{} = auth, id) do + endpoint_url = + case Auth.region(auth) do + :chinese -> "https://owner-api.teslamotors.cn" + _global -> "https://owner-api.teslamotors.com" + end + + TeslaApi.get(endpoint_url <> "/api/1/vehicles/#{id}", opts: [access_token: auth.token]) |> handle_response(transform: &result/1) end - def get_with_state(%Auth{token: token}, id) do - TeslaApi.get("/api/1/vehicles/#{id}/vehicle_data", opts: [access_token: token]) + def get_with_state(%Auth{} = auth, id) do + endpoint_url = + case Auth.region(auth) do + :chinese -> "https://owner-api.teslamotors.cn" + _global -> "https://owner-api.teslamotors.com" + end + + TeslaApi.get(endpoint_url <> "/api/1/vehicles/#{id}/vehicle_data", + opts: [access_token: auth.token] + ) |> handle_response(transform: &result/1) end diff --git a/test/teslamate/vehicles/vehicle/charging_sync_test.exs b/test/teslamate/vehicles/vehicle/charging_sync_test.exs index b2543834..af4926f1 100644 --- a/test/teslamate/vehicles/vehicle/charging_sync_test.exs +++ b/test/teslamate/vehicles/vehicle/charging_sync_test.exs @@ -38,9 +38,9 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingSyncTest do refute_receive _ end) =~ - """ - [warn] Invalid charge data: %{ideal_battery_range_km: [\"can't be blank\"]} - [warn] Invalid charge data: %{ideal_battery_range_km: [\"can't be blank\"]} + ~r""" + \[warn.*\] Invalid charge data: %{ideal_battery_range_km: \[\"can't be blank\"\]} + \[warn.*\] Invalid charge data: %{ideal_battery_range_km: \[\"can't be blank\"\]} """ end diff --git a/test/teslamate/vehicles/vehicle/charging_test.exs b/test/teslamate/vehicles/vehicle/charging_test.exs index 9e321299..b825c0a6 100644 --- a/test/teslamate/vehicles/vehicle/charging_test.exs +++ b/test/teslamate/vehicles/vehicle/charging_test.exs @@ -189,7 +189,7 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do assert capture_log(@log_opts, fn -> assert_receive {:insert_charge, ^cproc, %{date: _, charge_energy_added: 0.3}} assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :charging}}} - end) =~ "[warn] Discarded incomplete fetch result" + end) =~ "Discarded incomplete fetch result" assert_receive {:insert_position, ^car, %{}} assert_receive {:insert_charge, ^cproc, %{date: _, charge_energy_added: 0.3}} diff --git a/test/teslamate/vehicles/vehicle/streaming_test.exs b/test/teslamate/vehicles/vehicle/streaming_test.exs index a5272d3d..66f3e5ec 100644 --- a/test/teslamate/vehicles/vehicle/streaming_test.exs +++ b/test/teslamate/vehicles/vehicle/streaming_test.exs @@ -234,7 +234,7 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do assert_receive :continue? refute_receive _ end) =~ """ - [warn] Discarded stale fetch result: %{ + Discarded stale fetch result: %{ last: %TeslaApi.Vehicle.State.Drive{ gps_as_of: nil, heading: 120, @@ -298,7 +298,7 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do assert capture_log(@log_opts, fn -> stream(name, %{shift_state: "P", time: DateTime.add(now, -1, :second)}) refute_receive _ - end) =~ "[warn] Received stale stream data" + end) =~ "Received stale stream data" end end @@ -429,7 +429,7 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do assert capture_log(@log_opts, fn -> stream(name, %{shift_state: "D", time: DateTime.add(now, 1, :millisecond)}) refute_receive _ - end) =~ "[warn] Received stale stream data" + end) =~ "Received stale stream data" end end