Audi etron: get nickname from vehicle (#4756)

This commit is contained in:
andig 2022-10-08 14:14:09 +02:00 • committed by GitHub
parent f38ac299ef
commit 4881aaa095
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 20 deletions

View file

@ -65,17 +65,26 @@ func NewEtronFromConfig(other map[string]interface{}) (api.Vehicle, error) {
// use the etron API for list of vehicles
api := etron.NewAPI(log, ats)
cc.VIN, err = ensureVehicle(cc.VIN, func() ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), cc.Timeout)
defer cancel()
return api.Vehicles(ctx)
})
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, cc.VIN, cc.Cache)
v.Provider = id.NewProvider(api, vehicle.VIN, cc.Cache)
}
return v, err

View file

@ -5,7 +5,6 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/samber/lo"
"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
)
@ -33,22 +32,12 @@ func NewAPI(log *util.Logger, ts oauth2.TokenSource) *API {
}
// Vehicles implements the /vehicles response
func (v *API) Vehicles(ctx context.Context) ([]string, error) {
type vehicle struct {
VIN, Type, Nickname string
}
func (v *API) Vehicles(ctx context.Context) ([]Vehicle, error) {
var res struct {
UserVehicles []vehicle `json:"userVehicles"`
UserVehicles []Vehicle
}
var vins []string
err := v.client.Query(ctx, &res, nil)
if err == nil {
vins = lo.Map(res.UserVehicles, func(v vehicle, _ int) string {
return v.VIN
})
}
return vins, err
return res.UserVehicles, err
}

View file

@ -0,0 +1,5 @@
package etron
type Vehicle struct {
VIN, Type, Nickname string
}