forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesla.go
123 lines (105 loc) · 2.67 KB
/
tesla.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/util/transport"
"github.com/evcc-io/evcc/vehicle/tesla"
teslaclient "github.com/evcc-io/tesla-proxy-client"
"golang.org/x/oauth2"
)
// Tesla is an api.Vehicle implementation for Tesla cars using the official Tesla vehicle-command api.
type Tesla struct {
*embed
*tesla.Provider
*tesla.Controller
}
func init() {
if id := os.Getenv("TESLA_CLIENT_ID"); id != "" {
tesla.OAuth2Config.ClientID = id
}
if secret := os.Getenv("TESLA_CLIENT_SECRET"); secret != "" {
tesla.OAuth2Config.ClientSecret = secret
}
if tesla.OAuth2Config.ClientID != "" {
registry.Add("tesla", NewTeslaFromConfig)
}
}
// NewTeslaFromConfig creates a new vehicle
func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
Tokens Tokens
VIN string
CommandProxy string
Cache time.Duration
Timeout time.Duration
}{
CommandProxy: tesla.ProxyBaseUrl,
Cache: interval,
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
token, err := cc.Tokens.Token()
if err != nil {
return nil, err
}
log := util.NewLogger("tesla").Redact(
cc.Tokens.Access, cc.Tokens.Refresh,
tesla.OAuth2Config.ClientID, tesla.OAuth2Config.ClientSecret,
)
identity, err := tesla.NewIdentity(log, token)
if err != nil {
return nil, err
}
hc := request.NewClient(log)
hc.Transport = &oauth2.Transport{
Source: identity,
Base: hc.Transport,
}
tc, err := teslaclient.NewClient(context.Background(), teslaclient.WithClient(hc))
if err != nil {
return nil, err
}
// validate base url
region, err := tc.UserRegion()
if err != nil {
return nil, err
}
tc.SetBaseUrl(region.FleetApiBaseUrl)
vehicle, err := ensureVehicleEx(
cc.VIN, tc.Vehicles,
func(v *tesla.Vehicle) (string, error) {
return v.Vin, nil
},
)
if err != nil {
return nil, err
}
// proxy client
pc := request.NewClient(log)
pc.Transport = &transport.Decorator{
Decorator: transport.DecorateHeaders(map[string]string{
"X-Auth-Token": sponsor.Token,
}),
Base: hc.Transport,
}
tcc, err := teslaclient.NewClient(context.Background(), teslaclient.WithClient(pc))
if err != nil {
return nil, err
}
tcc.SetBaseUrl(cc.CommandProxy)
v := &Tesla{
embed: &cc.embed,
Provider: tesla.NewProvider(vehicle, cc.Cache),
Controller: tesla.NewController(vehicle.WithClient(tcc)),
}
v.fromVehicle(vehicle.DisplayName, 0)
return v, nil
}