forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenault.go
100 lines (82 loc) · 2.56 KB
/
renault.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
package vehicle
import (
"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/vehicle/renault"
"github.com/evcc-io/evcc/vehicle/renault/gigya"
"github.com/evcc-io/evcc/vehicle/renault/kamereon"
"github.com/evcc-io/evcc/vehicle/renault/keys"
)
// Credits to
// https://github.com/hacf-fr/renault-api
// https://github.com/edent/Renault-Zoe-API/issues/18
// https://github.com/epenet/Renault-Zoe-API/blob/newapimockup/Test/MyRenault.py
// https://github.com/jamesremuscat/pyze
// https://muscatoxblog.blogspot.com/2019/07/delving-into-renaults-new-api.html
// https://renault-api.readthedocs.io/en/latest/index.html
// Renault is an api.Vehicle implementation for Renault cars
type Renault struct {
*embed
*renault.Provider
}
func init() {
registry.Add("dacia", func(other map[string]interface{}) (api.Vehicle, error) {
return NewRenaultDaciaFromConfig("dacia", other)
})
registry.Add("renault", func(other map[string]interface{}) (api.Vehicle, error) {
return NewRenaultDaciaFromConfig("renault", other)
})
}
// NewRenaultDaciaFromConfig creates a new Renault/Dacia vehicle
func NewRenaultDaciaFromConfig(brand string, other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, Region, VIN string
AlternativeWakeup bool
Cache time.Duration
Timeout time.Duration
}{
Region: "de_DE",
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
}
log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN)
v := &Renault{
embed: &cc.embed,
}
keys := keys.New(log)
keys.Load(cc.Region)
identity := gigya.NewIdentity(log, keys.Gigya)
if err := identity.Login(cc.User, cc.Password); err != nil {
return nil, err
}
api := kamereon.New(log, keys.Kamereon, identity, func() error {
return identity.Login(cc.User, cc.Password)
})
api.Client.Timeout = cc.Timeout
accountID, err := api.Person(identity.PersonID, brand)
if err != nil {
return nil, err
}
vehicle, err := ensureVehicleEx(cc.VIN,
func() ([]kamereon.Vehicle, error) {
return api.Vehicles(accountID)
},
func(v kamereon.Vehicle) string {
return v.VIN
},
)
if err == nil {
err = vehicle.Available()
}
v.Provider = renault.NewProvider(api, accountID, vehicle.VIN, cc.AlternativeWakeup, cc.Cache)
return v, err
}