forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tesla using official vehicle command library (evcc-io#10802)
- Loading branch information
Showing
13 changed files
with
570 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
template: tesla-command | ||
products: | ||
- brand: Tesla | ||
description: | ||
generic: Vehicle-Command API | ||
requirements: | ||
description: | ||
de: | | ||
Es wird ein `access` und ein `refresh` Token für die Kommunikation mit der Tesla API erstellt werden. | ||
Die Tokens werden unter https://tesla.evcc.io erstellt. | ||
en: | | ||
You need to generate an `access` and a `refresh` token for communicating with the Tesla API. | ||
Tokens are generated using https://tesla.evcc.io. | ||
params: | ||
- name: title | ||
- name: icon | ||
default: car | ||
advanced: true | ||
- name: accessToken | ||
required: true | ||
mask: true | ||
help: | ||
en: "See https://docs.evcc.io/en/docs/devices/vehicles#tesla-command" | ||
de: "Siehe https://docs.evcc.io/docs/devices/vehicles#tesla-command" | ||
- name: refreshToken | ||
required: true | ||
mask: true | ||
help: | ||
en: "See https://docs.evcc.io/en/docs/devices/vehicles#tesla-command" | ||
de: "Siehe https://docs.evcc.io/docs/devices/vehicles#tesla-command" | ||
- name: vin | ||
example: W... | ||
- name: capacity | ||
- name: phases | ||
advanced: true | ||
- preset: vehicle-identify | ||
render: | | ||
type: tesla-command | ||
{{- if .title }} | ||
title: {{ .title }} | ||
{{- end }} | ||
{{- if .icon }} | ||
icon: {{ .icon }} | ||
{{- end }} | ||
tokens: | ||
access: {{ .accessToken }} | ||
refresh: {{ .refreshToken }} | ||
capacity: {{ .capacity }} | ||
{{- if .phases }} | ||
phases: {{ .phases }} | ||
{{- end }} | ||
{{- if .vin }} | ||
vin: {{ .vin }} | ||
{{- end }} | ||
{{ include "vehicle-identify" . }} | ||
features: ["coarsecurrent"] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package vehicle | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"time" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/util" | ||
"github.com/evcc-io/evcc/util/request" | ||
vc "github.com/evcc-io/evcc/vehicle/tesla-vehicle-command" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
// TeslaCommand is an api.Vehicle implementation for Tesla cars using the official Tesla vehicle-command api. | ||
type TeslaCommand struct { | ||
*embed | ||
*vc.Provider | ||
} | ||
|
||
func init() { | ||
if id := os.Getenv("TESLA_CLIENT_ID"); id != "" { | ||
vc.OAuth2Config.ClientID = id | ||
} | ||
if secret := os.Getenv("TESLA_CLIENT_SECRET"); secret != "" { | ||
vc.OAuth2Config.ClientSecret = secret | ||
} | ||
if vc.OAuth2Config.ClientID == "" { | ||
registry.Add("tesla-command", NewTeslaCommandFromConfig) | ||
} | ||
} | ||
|
||
// const privateKeyFile = "tesla-privatekey.pem" | ||
|
||
// NewTeslaCommandFromConfig creates a new vehicle | ||
func NewTeslaCommandFromConfig(other map[string]interface{}) (api.Vehicle, error) { | ||
cc := struct { | ||
embed `mapstructure:",squash"` | ||
Tokens Tokens | ||
VIN string | ||
Timeout time.Duration | ||
Cache time.Duration | ||
}{ | ||
Timeout: 10 * time.Second, | ||
Cache: interval, | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := cc.Tokens.Error(); err != nil { | ||
return nil, err | ||
} | ||
|
||
log := util.NewLogger("tesla-command").Redact(vc.OAuth2Config.ClientID, vc.OAuth2Config.ClientSecret) | ||
|
||
client := request.NewClient(log) | ||
|
||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client) | ||
ts := vc.OAuth2Config.TokenSource(ctx, &oauth2.Token{ | ||
AccessToken: cc.Tokens.Access, | ||
RefreshToken: cc.Tokens.Refresh, | ||
Expiry: time.Now(), | ||
}) | ||
|
||
identity, err := vc.NewIdentity(log, ts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
api := vc.NewAPI(log, identity, cc.Timeout) | ||
|
||
vehicle, err := ensureVehicleEx( | ||
cc.VIN, api.Vehicles, | ||
func(v *vc.Vehicle) string { | ||
return v.Vin | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
v := &TeslaCommand{ | ||
embed: &cc.embed, | ||
Provider: vc.NewProvider(api, vehicle.ID, cc.Cache), | ||
} | ||
|
||
if v.Title_ == "" { | ||
v.Title_ = vehicle.DisplayName | ||
} | ||
/* | ||
privKey, err := protocol.LoadPrivateKey(privateKeyFile) | ||
if err != nil { | ||
log.WARN.Println("private key not found, commands are disabled") | ||
return v, nil | ||
} | ||
vv, err := identity.Account().GetVehicle(context.Background(), vehicle.Vin, privKey, cache.New(8)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cs, err := vc.NewCommandSession(vv, cc.Timeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := &struct { | ||
*TeslaCommand | ||
*vc.CommandSession | ||
}{ | ||
TeslaCommand: v, | ||
CommandSession: cs, | ||
} | ||
*/ | ||
|
||
return v, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package vc | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/bogosj/tesla" | ||
"github.com/evcc-io/evcc/util" | ||
"github.com/evcc-io/evcc/util/request" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
FleetAudienceEU = "https://fleet-api.prd.eu.vn.cloud.tesla.com" | ||
) | ||
|
||
type API struct { | ||
*request.Helper | ||
identity *Identity | ||
} | ||
|
||
func NewAPI(log *util.Logger, identity *Identity, timeout time.Duration) *API { | ||
client := request.NewHelper(log) | ||
client.Client.Timeout = timeout | ||
client.Transport = &oauth2.Transport{ | ||
Source: identity, | ||
Base: client.Transport, | ||
} | ||
|
||
return &API{ | ||
Helper: client, | ||
identity: identity, | ||
} | ||
} | ||
|
||
func (v *API) Vehicles() ([]*Vehicle, error) { | ||
// ctx, cancel := context.WithTimeout(context.Background(), v.Timeout) | ||
// defer cancel() | ||
|
||
// b, err := v.identity.Account().Get(ctx, "api/1/vehicles") | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// var res tesla.VehiclesResponse | ||
// if err := json.Unmarshal(b, &res); err != nil { | ||
// return nil, err | ||
// } | ||
|
||
var res tesla.VehiclesResponse | ||
err := v.GetJSON(fmt.Sprintf("%s/api/1/vehicles", FleetAudienceEU), &res) | ||
|
||
return res.Response, err | ||
} | ||
|
||
func (v *API) VehicleData(id int64) (*VehicleData, error) { | ||
// ctx, cancel := context.WithTimeout(context.Background(), request.Timeout) | ||
// defer cancel() | ||
|
||
// b, err := v.identity.Account().Get(ctx, fmt.Sprintf("api/1/vehicles/%d/vehicle_data", id)) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// var res tesla.VehicleData | ||
// if err := json.Unmarshal(b, &res); err != nil { | ||
// return nil, err | ||
// } | ||
|
||
var res tesla.VehicleData | ||
err := v.GetJSON(fmt.Sprintf("%s/api/1/vehicles/%d/vehicle_data", FleetAudienceEU, id), &res) | ||
|
||
return &res, err | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package vc | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
"github.com/teslamotors/vehicle-command/pkg/connector/inet" | ||
) | ||
|
||
// ApiError converts HTTP 408 error to ErrTimeout | ||
func ApiError(err error) error { | ||
if err != nil && (errors.Is(err, inet.ErrVehicleNotAwake) || | ||
strings.HasSuffix(err.Error(), "408 Request Timeout") || strings.HasSuffix(err.Error(), "408 (Request Timeout)")) { | ||
err = api.ErrAsleep | ||
} | ||
return err | ||
} |
Oops, something went wrong.