Validate that vehicle is visible in vendor api on startup
This commit is contained in:
parent
0afc82401f
commit
3c9b6a263d
17 changed files with 82 additions and 116 deletions
|
|
@ -47,8 +47,8 @@ func NewAudiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("audi").Redact(cc.User, cc.Password, cc.VIN)
|
||||
|
||||
identity := vw.NewIdentity(log, audi.AuthClientID, audi.AuthParams, cc.User, cc.Password)
|
||||
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
|
|
@ -57,12 +57,7 @@ func NewAudiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
api := vw.NewAPI(log, identity, "Audi", "DE")
|
||||
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)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
if err == nil {
|
||||
if err = api.HomeRegion(strings.ToUpper(cc.VIN)); err == nil {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/oauth"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
|
|
@ -47,7 +48,7 @@ type Identity struct {
|
|||
}
|
||||
|
||||
// NewIdentity creates BlueLink Identity
|
||||
func NewIdentity(log *util.Logger, config Config) (*Identity, error) {
|
||||
func NewIdentity(log *util.Logger, config Config) *Identity {
|
||||
v := &Identity{
|
||||
log: log,
|
||||
Helper: request.NewHelper(log),
|
||||
|
|
@ -57,7 +58,7 @@ func NewIdentity(log *util.Logger, config Config) (*Identity, error) {
|
|||
// fetch updated stamps
|
||||
updateStamps(log, config.CCSPApplicationID)
|
||||
|
||||
return v, nil
|
||||
return v
|
||||
}
|
||||
|
||||
// Credits to https://openwb.de/forum/viewtopic.php?f=5&t=1215&start=10#p11877
|
||||
|
|
@ -322,6 +323,10 @@ func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
|||
}
|
||||
|
||||
func (v *Identity) Login(user, password string) (err error) {
|
||||
if user == "" || password == "" {
|
||||
return api.ErrMissingCredentials
|
||||
}
|
||||
|
||||
v.deviceID, err = v.getDeviceID()
|
||||
|
||||
var cookieClient *request.Helper
|
||||
|
|
|
|||
|
|
@ -55,21 +55,18 @@ func NewBMWMiniFromConfig(brand string, other map[string]interface{}) (api.Vehic
|
|||
log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := bmw.NewIdentity(log)
|
||||
|
||||
if err := identity.Login(cc.User, cc.Password); err != nil {
|
||||
err := identity.Login(cc.User, cc.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api := bmw.NewAPI(log, brand, identity)
|
||||
|
||||
var err error
|
||||
if cc.VIN == "" {
|
||||
cc.VIN, err = findVehicle(api.Vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
v.Provider = bmw.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
|
||||
if err == nil {
|
||||
v.Provider = bmw.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,10 +55,7 @@ func NewEnyaqFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
api := skoda.NewAPI(log, ts)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
cc.VIN, err = findVehicle(api.Vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
|
|
|
|||
|
|
@ -56,14 +56,11 @@ func NewFiatFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
|
||||
api := fiat.NewAPI(log, identity)
|
||||
|
||||
if cc.VIN == "" {
|
||||
cc.VIN, err = findVehicle(api.Vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
v.Provider = fiat.NewProvider(api, strings.ToUpper(cc.VIN), cc.PIN, cc.Expiry, cc.Cache)
|
||||
if err == nil {
|
||||
v.Provider = fiat.NewProvider(api, strings.ToUpper(cc.VIN), cc.PIN, cc.Expiry, cc.Cache)
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,14 +63,11 @@ func NewFordFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
|
||||
api := ford.NewAPI(log, identity)
|
||||
|
||||
if cc.VIN == "" {
|
||||
cc.VIN, err = findVehicle(api.Vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
v.Provider = ford.NewProvider(api, strings.ToUpper(cc.VIN), cc.Expiry, cc.Cache)
|
||||
if err == nil {
|
||||
v.Provider = ford.NewProvider(api, strings.ToUpper(cc.VIN), cc.Expiry, cc.Cache)
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@ package vehicle
|
|||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
)
|
||||
|
||||
// findVehicle finds the first vehicle in the list of VINs or returns an error
|
||||
func findVehicle(vehicles []string, err error) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot get vehicles: %w", err)
|
||||
|
|
@ -21,3 +24,23 @@ func findVehicle(vehicles []string, err error) (string, error) {
|
|||
|
||||
return vin, nil
|
||||
}
|
||||
|
||||
// ensureVehicle ensures that the vehicle is available on the api and returns the VIN
|
||||
func ensureVehicle(vin string, fun func() ([]string, error)) (string, error) {
|
||||
vehicles, err := fun()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot get vehicles: %w", err)
|
||||
}
|
||||
|
||||
if vin = strings.ToUpper(vin); vin != "" {
|
||||
// vin defined but doesn't exist
|
||||
if !funk.ContainsString(vehicles, vin) {
|
||||
err = fmt.Errorf("cannot find vehicle: %s", vin)
|
||||
}
|
||||
} else {
|
||||
// vin empty
|
||||
vin, err = findVehicle(vehicles, nil)
|
||||
}
|
||||
|
||||
return vin, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,12 +37,6 @@ func NewHyundaiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if cc.User == "" || cc.Password == "" {
|
||||
return nil, api.ErrMissingCredentials
|
||||
}
|
||||
|
||||
log := util.NewLogger("hyundai").Redact(cc.User, cc.Password, cc.VIN)
|
||||
|
||||
settings := bluelink.Config{
|
||||
URI: "https://prd.eu-ccapi.hyundai.com:8080",
|
||||
BasicToken: "NmQ0NzdjMzgtM2NhNC00Y2YzLTk1NTctMmExOTI5YTk0NjU0OktVeTQ5WHhQekxwTHVvSzB4aEJDNzdXNlZYaG10UVI5aVFobUlGampvWTRJcHhzVg==",
|
||||
|
|
@ -52,10 +46,8 @@ func NewHyundaiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
BrandAuthUrl: "https://eu-account.hyundai.com/auth/realms/euhyundaiidm/protocol/openid-connect/auth?client_id=%s&scope=openid%%20profile%%20email%%20phone&response_type=code&hkid_session_reset=true&redirect_uri=%s/api/v1/user/integration/redirect/login&ui_locales=%s&state=%s:%s",
|
||||
}
|
||||
|
||||
identity, err := bluelink.NewIdentity(log, settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log := util.NewLogger("hyundai").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := bluelink.NewIdentity(log, settings)
|
||||
|
||||
if err := identity.Login(cc.User, cc.Password); err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -44,24 +44,21 @@ func NewIDFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("id").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := id.NewIdentity(log, cc.User, cc.Password)
|
||||
|
||||
ts := id.NewIdentity(log, cc.User, cc.Password)
|
||||
err := ts.Login()
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := id.NewAPI(log, ts)
|
||||
api := id.NewAPI(log, identity)
|
||||
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)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
v.Provider = id.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
|
||||
if err == nil {
|
||||
v.Provider = id.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,12 +37,6 @@ func NewKiaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if cc.User == "" || cc.Password == "" {
|
||||
return nil, api.ErrMissingCredentials
|
||||
}
|
||||
|
||||
log := util.NewLogger("kia").Redact(cc.User, cc.Password, cc.VIN)
|
||||
|
||||
settings := bluelink.Config{
|
||||
URI: "https://prd.eu-ccapi.kia.com:8080",
|
||||
BasicToken: "ZmRjODVjMDAtMGEyZi00YzY0LWJjYjQtMmNmYjE1MDA3MzBhOnNlY3JldA==",
|
||||
|
|
@ -52,10 +46,8 @@ func NewKiaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
BrandAuthUrl: "https://eu-account.kia.com/auth/realms/eukiaidm/protocol/openid-connect/auth?client_id=%s&scope=openid%%20profile%%20email%%20phone&response_type=code&hkid_session_reset=true&redirect_uri=%s/api/v1/user/integration/redirect/login&ui_locales=%s&state=%s:%s",
|
||||
}
|
||||
|
||||
identity, err := bluelink.NewIdentity(log, settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log := util.NewLogger("kia").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := bluelink.NewIdentity(log, settings)
|
||||
|
||||
if err := identity.Login(cc.User, cc.Password); err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -55,21 +55,18 @@ func NewNissanFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
log := util.NewLogger("nissan").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := nissan.NewIdentity(log)
|
||||
|
||||
if err := identity.Login(cc.User, cc.Password); err != nil {
|
||||
err := identity.Login(cc.User, cc.Password)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := nissan.NewAPI(log, identity)
|
||||
|
||||
var err error
|
||||
if cc.VIN == "" {
|
||||
cc.VIN, err = findVehicle(api.Vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
v.Provider = nissan.NewProvider(api, strings.ToUpper(cc.VIN), cc.Expiry, cc.Cache)
|
||||
if err == nil {
|
||||
v.Provider = nissan.NewProvider(api, strings.ToUpper(cc.VIN), cc.Expiry, cc.Cache)
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,23 +42,22 @@ func NewPorscheFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
log := util.NewLogger("porsche").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := porsche.NewIdentity(log)
|
||||
|
||||
if err := identity.Login(cc.User, cc.Password); err != nil {
|
||||
err := identity.Login(cc.User, cc.Password)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := porsche.NewAPI(log, identity.DefaultSource)
|
||||
|
||||
if cc.VIN == "" {
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, func() ([]string, error) {
|
||||
vehicles, err := api.Vehicles()
|
||||
cc.VIN, err = findVehicle(funk.Map(vehicles, func(v porsche.Vehicle) string {
|
||||
return funk.Map(vehicles, func(v porsche.Vehicle) string {
|
||||
return v.VIN
|
||||
}).([]string), err)
|
||||
}).([]string), err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// check if vehicle is paired
|
||||
|
|
|
|||
|
|
@ -137,7 +137,6 @@ func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
Helper: request.NewHelper(log),
|
||||
user: cc.User,
|
||||
password: cc.Password,
|
||||
vin: strings.ToUpper(cc.VIN),
|
||||
}
|
||||
|
||||
err := v.apiKeys(cc.Region)
|
||||
|
|
@ -145,11 +144,10 @@ func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
err = v.authFlow()
|
||||
}
|
||||
|
||||
if err == nil && cc.VIN == "" {
|
||||
v.vin, err = findVehicle(v.kamereonVehicles(v.accountID))
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
if err == nil {
|
||||
v.vin, err = ensureVehicle(cc.VIN, func() ([]string, error) {
|
||||
return v.kamereonVehicles(v.accountID)
|
||||
})
|
||||
}
|
||||
|
||||
v.batteryG = provider.NewCached(v.batteryAPI, cc.Cache).InterfaceGetter()
|
||||
|
|
|
|||
|
|
@ -46,8 +46,8 @@ func NewSeatFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("seat").Redact(cc.User, cc.Password, cc.VIN)
|
||||
|
||||
identity := vw.NewIdentity(log, seat.AuthClientID, seat.AuthParams, cc.User, cc.Password)
|
||||
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
|
|
@ -56,12 +56,7 @@ func NewSeatFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
api := vw.NewAPI(log, identity, seat.Brand, seat.Country)
|
||||
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)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
if err == nil {
|
||||
if err = api.HomeRegion(strings.ToUpper(cc.VIN)); err == nil {
|
||||
|
|
|
|||
|
|
@ -46,8 +46,8 @@ func NewSkodaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("skoda").Redact(cc.User, cc.Password, cc.VIN)
|
||||
|
||||
identity := vw.NewIdentity(log, skoda.AuthClientID, skoda.AuthParams, cc.User, cc.Password)
|
||||
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
|
|
@ -56,12 +56,7 @@ func NewSkodaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
api := vw.NewAPI(log, identity, skoda.Brand, skoda.Country)
|
||||
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)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
if err == nil {
|
||||
if err = api.HomeRegion(strings.ToUpper(cc.VIN)); err == nil {
|
||||
|
|
|
|||
|
|
@ -65,12 +65,7 @@ func NewVolvoFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}, cc.Cache).InterfaceGetter()
|
||||
|
||||
var err error
|
||||
if cc.VIN == "" {
|
||||
v.vin, err = findVehicle(v.vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, v.vehicles)
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,12 +55,7 @@ func NewVWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
api := vw.NewAPI(log, identity, vw.Brand, vw.Country)
|
||||
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)
|
||||
}
|
||||
}
|
||||
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
||||
|
||||
if err == nil {
|
||||
if err = api.HomeRegion(strings.ToUpper(cc.VIN)); err == nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue