Bluelink: make language configurable (#2597)

This commit is contained in:
andig 2022-02-14 14:51:44 +01:00 • committed by GitHub
parent 9f4148400e
commit 7d5f8982c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 111 deletions

View file

@ -10,33 +10,19 @@ import (
"github.com/evcc-io/evcc/vehicle/bluelink"
)
// Hyundai is an api.Vehicle implementation
type Hyundai struct {
// Bluelink is an api.Vehicle implementation
type Bluelink struct {
*embed
*bluelink.Provider
}
func init() {
registry.Add("kia", NewKiaFromConfig)
registry.Add("hyundai", NewHyundaiFromConfig)
}
// NewHyundaiFromConfig creates a new Vehicle
// NewHyundaiFromConfig creates a new vehicle
func NewHyundaiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password string
VIN string
Expiry time.Duration
Cache time.Duration
}{
Expiry: expiry,
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
settings := bluelink.Config{
URI: "https://prd.eu-ccapi.hyundai.com:8080",
BasicToken: "NmQ0NzdjMzgtM2NhNC00Y2YzLTk1NTctMmExOTI5YTk0NjU0OktVeTQ5WHhQekxwTHVvSzB4aEJDNzdXNlZYaG10UVI5aVFobUlGampvWTRJcHhzVg==",
@ -46,10 +32,46 @@ 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",
}
log := util.NewLogger("hyundai").Redact(cc.User, cc.Password, cc.VIN)
return newBluelinkFromConfig("hyundai", other, settings)
}
// NewKiaFromConfig creates a new vehicle
func NewKiaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
settings := bluelink.Config{
URI: "https://prd.eu-ccapi.kia.com:8080",
BasicToken: "ZmRjODVjMDAtMGEyZi00YzY0LWJjYjQtMmNmYjE1MDA3MzBhOnNlY3JldA==",
CCSPServiceID: "fdc85c00-0a2f-4c64-bcb4-2cfb1500730a",
CCSPApplicationID: bluelink.KiaAppID,
AuthClientID: "572e0304-5f8d-4b4c-9dd5-41aa84eed160",
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",
}
return newBluelinkFromConfig("kia", other, settings)
}
// newBluelinkFromConfig creates a new Vehicle
func newBluelinkFromConfig(brand string, other map[string]interface{}, settings bluelink.Config) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password string
VIN string
Language string
Expiry time.Duration
Cache time.Duration
}{
Language: "en",
Expiry: expiry,
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN)
identity := bluelink.NewIdentity(log, settings)
if err := identity.Login(cc.User, cc.Password); err != nil {
if err := identity.Login(cc.User, cc.Password, cc.Language); err != nil {
return nil, err
}
@ -68,7 +90,6 @@ func NewHyundaiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
for _, v := range vehicles {
if v.VIN == strings.ToUpper(cc.VIN) {
vehicle = v
log.DEBUG.Printf("found vehicle: %v", vehicle.VIN)
}
}
}
@ -77,7 +98,7 @@ func NewHyundaiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, fmt.Errorf("cannot find vehicle: %v", vehicles)
}
v := &Hyundai{
v := &Bluelink{
embed: &cc.embed,
Provider: bluelink.NewProvider(api, vehicle.VehicleID, cc.Expiry, cc.Cache),
}

View file

@ -120,9 +120,9 @@ func (v *Identity) getCookies() (cookieClient *request.Helper, err error) {
return cookieClient, err
}
func (v *Identity) setLanguage(cookieClient *request.Helper) error {
func (v *Identity) setLanguage(cookieClient *request.Helper, language string) error {
data := map[string]interface{}{
"lang": "en",
"lang": language,
}
req, err := request.New(http.MethodPost, v.config.URI+LanguageURL, request.MarshalJSON(data), request.JSONEncoding)
@ -322,7 +322,7 @@ func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
return (*oauth2.Token)(&res), err
}
func (v *Identity) Login(user, password string) (err error) {
func (v *Identity) Login(user, password, language string) (err error) {
if user == "" || password == "" {
return api.ErrMissingCredentials
}
@ -335,7 +335,7 @@ func (v *Identity) Login(user, password string) (err error) {
}
if err == nil {
err = v.setLanguage(cookieClient)
err = v.setLanguage(cookieClient, language)
}
var code string

View file

@ -1,85 +0,0 @@
package vehicle
import (
"fmt"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/bluelink"
)
// Kia is an api.Vehicle implementation
type Kia struct {
*embed
*bluelink.Provider // provides the api implementations
}
func init() {
registry.Add("kia", NewKiaFromConfig)
}
// NewKiaFromConfig creates a new Vehicle
func NewKiaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password string
VIN string
Expiry time.Duration
Cache time.Duration
}{
Expiry: expiry,
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
settings := bluelink.Config{
URI: "https://prd.eu-ccapi.kia.com:8080",
BasicToken: "ZmRjODVjMDAtMGEyZi00YzY0LWJjYjQtMmNmYjE1MDA3MzBhOnNlY3JldA==",
CCSPServiceID: "fdc85c00-0a2f-4c64-bcb4-2cfb1500730a",
CCSPApplicationID: bluelink.KiaAppID,
AuthClientID: "572e0304-5f8d-4b4c-9dd5-41aa84eed160",
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",
}
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
}
api := bluelink.NewAPI(log, settings.URI, identity, cc.Cache)
vehicles, err := api.Vehicles()
if err != nil {
return nil, err
}
var vehicle bluelink.Vehicle
if cc.VIN == "" && len(vehicles) == 1 {
vehicle = vehicles[0]
log.DEBUG.Printf("found vehicle: %v", cc.VIN)
} else {
for _, v := range vehicles {
if v.VIN == strings.ToUpper(cc.VIN) {
vehicle = v
}
}
}
if len(vehicle.VIN) == 0 {
return nil, fmt.Errorf("cannot find vehicle: %v", vehicles)
}
v := &Kia{
embed: &cc.embed,
Provider: bluelink.NewProvider(api, vehicle.VehicleID, cc.Expiry, cc.Cache),
}
return v, nil
}