forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseat.go
81 lines (66 loc) · 1.91 KB
/
seat.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
package vehicle
import (
"fmt"
"net/url"
"strings"
"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/vw"
)
// https://github.com/trocotronic/weconnect
// https://github.com/TA2k/ioBroker.vw-connect
// Seat is an api.Vehicle implementation for Seat cars
type Seat struct {
*embed
*vw.Provider // provides the api implementations
}
func init() {
registry.Add("seat", NewSeatFromConfig)
}
// NewSeatFromConfig creates a new vehicle
func NewSeatFromConfig(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
}
v := &Seat{
embed: &cc.embed,
}
log := util.NewLogger("seat").Redact(cc.User, cc.Password, cc.VIN)
identity := vw.NewIdentity(log)
query := url.Values(map[string][]string{
"response_type": {"code id_token"},
"client_id": {"50f215ac-4444-4230-9fb1-fe15cd1a9bcc@apps_vw-dilab_com"},
"redirect_uri": {"seatconnect://identity-kit/login"},
"scope": {"openid profile mbb"}, // cars birthdate nickname address phone
})
ts := vw.NewTokenSource(log, identity, "9dcc70f0-8e79-423a-a3fa-4065d99088b4", query, cc.User, cc.Password)
err := identity.Login(ts)
if err != nil {
return v, fmt.Errorf("login failed: %w", err)
}
api := vw.NewAPI(log, identity, "VW", "ES")
api.Client.Timeout = cc.Timeout
if cc.VIN == "" {
cc.VIN, err = findVehicle(api.Vehicles())
if err == nil {
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
}
}
if err == nil {
if err = api.HomeRegion(strings.ToUpper(cc.VIN)); err == nil {
v.Provider = vw.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
}
}
return v, err
}