92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package vehicle
|
|
|
|
import (
|
|
"context"
|
|
"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/audi"
|
|
"github.com/evcc-io/evcc/vehicle/vag/idkproxy"
|
|
"github.com/evcc-io/evcc/vehicle/vag/service"
|
|
"github.com/evcc-io/evcc/vehicle/vag/vwidentity"
|
|
"github.com/evcc-io/evcc/vehicle/vw/weconnect"
|
|
)
|
|
|
|
// 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
|
|
*weconnect.Provider // provides the api implementations
|
|
}
|
|
|
|
func init() {
|
|
registry.Add("audi", NewAudiFromConfig)
|
|
}
|
|
|
|
// NewAudiFromConfig creates a new vehicle
|
|
func NewAudiFromConfig(other map[string]any) (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, audi.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, audi.IDKParams)
|
|
ats, its, err := service.AAZSTokenSource(log, idk, audi.AZSConfig, q)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// use the etron API for list of vehicles
|
|
api := audi.NewAPI(log, ats)
|
|
|
|
vehicle, err := ensureVehicleEx(
|
|
cc.VIN, func() ([]audi.Vehicle, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), cc.Timeout)
|
|
defer cancel()
|
|
return api.Vehicles(ctx)
|
|
},
|
|
func(v audi.Vehicle) (string, error) {
|
|
return v.VIN, nil
|
|
},
|
|
)
|
|
|
|
if err == nil {
|
|
api := weconnect.NewAPI(log, its)
|
|
api.Client.Timeout = cc.Timeout
|
|
|
|
v.fromVehicle(vehicle.Nickname, 0)
|
|
v.Provider = weconnect.NewProvider(api, vehicle.VIN, cc.Cache)
|
|
}
|
|
|
|
return v, err
|
|
}
|