forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audi.go
96 lines (78 loc) · 2.27 KB
/
audi.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
package vehicle
import (
"context"
"time"
"github.com/connectorjs/evcm/api"
"github.com/connectorjs/evcm/util"
"github.com/connectorjs/evcm/util/request"
"github.com/connectorjs/evcm/vehicle/audi/etron"
"github.com/connectorjs/evcm/vehicle/vag/idkproxy"
"github.com/connectorjs/evcm/vehicle/vag/service"
"github.com/connectorjs/evcm/vehicle/vag/vwidentity"
"github.com/connectorjs/evcm/vehicle/vw/id"
)
// https://github.com/TA2k/ioBroker.vw-connect
// https://github.com/arjenvrh/audi_connect_ha/blob/master/custom_components/audiconnect/audi_services.py
// Audi is an api.Vehicle implementation for Audi cars
type Audi struct {
*embed
*id.Provider // provides the api implementations
}
func init() {
registry.Add("audi", NewAudiFromConfig)
registry.Add("etron", NewAudiFromConfig)
}
// NewAudiFromConfig creates a new vehicle
func NewAudiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Cache time.Duration
Timeout time.Duration
}{
Cache: interval,
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
v := &Audi{
embed: &cc.embed,
}
log := util.NewLogger("audi").Redact(cc.User, cc.Password, cc.VIN)
// get initial VW identity id_token
q, err := vwidentity.Login(log, etron.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
// exchange initial VW identity id_token for Audi AAZS token
idk := idkproxy.New(log, etron.IDKParams)
ats, its, err := service.AAZSTokenSource(log, idk, etron.AZSConfig, q)
if err != nil {
return nil, err
}
// use the etron API for list of vehicles
api := etron.NewAPI(log, ats)
vehicle, err := ensureVehicleEx(
cc.VIN, func() ([]etron.Vehicle, error) {
ctx, cancel := context.WithTimeout(context.Background(), cc.Timeout)
defer cancel()
return api.Vehicles(ctx)
},
func(v etron.Vehicle) string {
return v.VIN
},
)
if err == nil {
if v.Title_ == "" {
v.Title_ = vehicle.Nickname
}
api := id.NewAPI(log, its)
api.Client.Timeout = cc.Timeout
v.Provider = id.NewProvider(api, vehicle.VIN, cc.Cache)
}
return v, err
}