From cd21eb154991c60be843b985fed11b5824ac74bc Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 20 Feb 2024 14:21:02 +0100 Subject: [PATCH] Tesla: fix detecting vehicle asleep (#12369) --- go.mod | 2 +- go.sum | 4 ++-- vehicle/tesla/api_test.go | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 vehicle/tesla/api_test.go diff --git a/go.mod b/go.mod index cd2666a14..c8c57dee9 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/eclipse/paho.mqtt.golang v1.4.3 github.com/enbility/cemd v0.2.2 github.com/enbility/eebus-go v0.2.0 - github.com/evcc-io/tesla-proxy-client v0.0.0-20240219081629-3afc7df83f15 + github.com/evcc-io/tesla-proxy-client v0.0.0-20240220130921-cdf51549e213 github.com/fatih/structs v1.1.0 github.com/glebarez/sqlite v1.10.0 github.com/go-http-utils/etag v0.0.0-20161124023236-513ea8f21eb1 diff --git a/go.sum b/go.sum index 405efad16..9d9b3eca6 100644 --- a/go.sum +++ b/go.sum @@ -134,8 +134,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evcc-io/modbus v0.0.0-20230705160100-23e1df2c01c3 h1:QBtLqy85cy9KLRWqKxX+42bN1rWti8iU7pL7Vwf2UbE= github.com/evcc-io/modbus v0.0.0-20230705160100-23e1df2c01c3/go.mod h1:qVX2WhsI5xyAoM6I/MV1bXSKBPdLAjp7pCvieO/S0AY= -github.com/evcc-io/tesla-proxy-client v0.0.0-20240219081629-3afc7df83f15 h1:KWuX06b4nUyghzzN9QpAzC7kCW8Re9uL4ceZ1N04qGQ= -github.com/evcc-io/tesla-proxy-client v0.0.0-20240219081629-3afc7df83f15/go.mod h1:zWtAweBqXJTk3HSrPSecz3Q3a2hAUQ4vOE6paJfn03I= +github.com/evcc-io/tesla-proxy-client v0.0.0-20240220130921-cdf51549e213 h1:iHnuiPRtVoHtHd82y50sDQmJbn07xPQ3fr48+qLlvl8= +github.com/evcc-io/tesla-proxy-client v0.0.0-20240220130921-cdf51549e213/go.mod h1:zWtAweBqXJTk3HSrPSecz3Q3a2hAUQ4vOE6paJfn03I= github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= diff --git a/vehicle/tesla/api_test.go b/vehicle/tesla/api_test.go new file mode 100644 index 000000000..3d23bcb4d --- /dev/null +++ b/vehicle/tesla/api_test.go @@ -0,0 +1,44 @@ +package tesla + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/evcc-io/evcc/api" + "github.com/evcc-io/evcc/util/sponsor" + "github.com/evcc-io/tesla-proxy-client" + "github.com/stretchr/testify/require" + "golang.org/x/oauth2" +) + +func TestCommandResponse(t *testing.T) { + sponsor.Token = "token" + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.Path) + + switch r.URL.Path { + case "/api/1/vehicles/abc": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"response": {"vin": "abc"}}`)) + + case "/api/1/vehicles/abc/command/charge_start": + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(`{"response": null, "error": "vehicle unavailable: vehicle is offline or asleep"}`)) + } + })) + defer srv.Close() + + ts := oauth2.StaticTokenSource(new(oauth2.Token)) + client, err := tesla.NewClient(context.Background(), tesla.WithTokenSource(ts)) + require.NoError(t, err) + client.SetBaseUrl(srv.URL) + + v, err := client.Vehicle("abc") + require.NoError(t, err) + + require.ErrorIs(t, NewController(v).StartCharge(), api.ErrAsleep) +}