Tesla: fix detecting vehicle asleep (#12369)

This commit is contained in:
andig 2024-02-20 14:21:02 +01:00
parent c4874c17da
commit cd21eb1549
3 changed files with 47 additions and 3 deletions

2
go.mod
View file

@ -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

4
go.sum
View file

@ -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=

44
vehicle/tesla/api_test.go Normal file
View file

@ -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)
}