Skip to content

Commit

Permalink
Dacia: use correct account id per brand (#4166)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 20, 2022
1 parent 1b1358c commit df1d3a7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion templates/definition/vehicle/dacia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ params:
default: 27.4
- preset: vehiclecloud
render: |
type: renault
type: dacia
{{ include "vehicle-base" . }}
{{ include "vehicle-identify" . }}
{{ include "vehicle-cloud" . }}
20 changes: 10 additions & 10 deletions vehicle/renault.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vehicle

import (
"errors"
"time"

"github.com/evcc-io/evcc/api"
Expand All @@ -26,12 +25,16 @@ type Renault struct {
}

func init() {
registry.Add("dacia", NewRenaultFromConfig)
registry.Add("renault", NewRenaultFromConfig)
registry.Add("dacia", func(other map[string]interface{}) (api.Vehicle, error) {
return NewRenaultDaciaFromConfig("dacia", other)
})
registry.Add("renault", func(other map[string]interface{}) (api.Vehicle, error) {
return NewRenaultDaciaFromConfig("renault", other)
})
}

// NewRenaultFromConfig creates a new vehicle
func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
// NewRenaultDaciaFromConfig creates a new Renault/Dacia vehicle
func NewRenaultDaciaFromConfig(brand string, other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, Region, VIN string
Expand All @@ -45,7 +48,7 @@ func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, err
}

log := util.NewLogger("renault").Redact(cc.User, cc.Password, cc.VIN)
log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN)

v := &Renault{
embed: &cc.embed,
Expand All @@ -63,10 +66,7 @@ func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return identity.Login(cc.User, cc.Password)
})

accountID, err := api.Person(identity.PersonID)
if err == nil && accountID == "" {
return nil, errors.New("missing accountID")
}
accountID, err := api.Person(identity.PersonID, brand)

var car kamereon.Vehicle
if err == nil {
Expand Down
15 changes: 11 additions & 4 deletions vehicle/renault/kamereon/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package kamereon

import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"

"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
Expand Down Expand Up @@ -62,15 +64,20 @@ func (v *API) request(uri string, body io.Reader) (Response, error) {
return res, err
}

func (v *API) Person(personID string) (string, error) {
func (v *API) Person(personID, brand string) (string, error) {
uri := fmt.Sprintf("%s/commerce/v1/persons/%s", v.keys.Target, personID)
res, err := v.request(uri, nil)

if len(res.Accounts) == 0 {
if err != nil {
return "", err
}

return res.Accounts[0].AccountID, err
for _, account := range res.Accounts {
if strings.Contains(strings.ToLower(account.AccountType), strings.ToLower(brand)) {
return account.AccountID, nil
}
}

return "", errors.New("account not found")
}

func (v *API) Vehicles(accountID string) ([]Vehicle, error) {
Expand Down
26 changes: 15 additions & 11 deletions vehicle/renault/kamereon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
)

type Response struct {
Accounts []Account `json:"accounts"` // /commerce/v1/persons/%s
AccessToken string `json:"accessToken"` // /commerce/v1/accounts/%s/kamereon/token
VehicleLinks []Vehicle `json:"vehicleLinks"` // /commerce/v1/accounts/%s/vehicles
Data Data `json:"data"` // /commerce/v1/accounts/%s/kamereon/kca/car-adapter/vX/cars/%s/...
Accounts []Account // /commerce/v1/persons/%s
AccessToken string // /commerce/v1/accounts/%s/kamereon/token
VehicleLinks []Vehicle // /commerce/v1/accounts/%s/vehicles
Data Data // /commerce/v1/accounts/%s/kamereon/kca/car-adapter/vX/cars/%s/...
}

type Account struct {
AccountID string `json:"accountId"`
AccountID string
AccountType string
AccountStatus string
Country string
RelationType string
}

type Vehicle struct {
Expand All @@ -23,24 +27,24 @@ type Vehicle struct {
ConnectedDriver connectedDriver
}

type connectedDriver struct {
Role string
}

func (v *Vehicle) Available() error {
if strings.ToUpper(v.Status) != "ACTIVE" {
return errors.New("vehicle is not active")
}

if len(v.ConnectedDriver.Role) == 0 {
if v.ConnectedDriver.Role == "" {
return errors.New("vehicle is not connected to driver")
}

return nil
}

type connectedDriver struct {
Role string `json:"role"`
}

type Data struct {
Attributes attributes `json:"attributes"`
Attributes attributes
}

type attributes struct {
Expand Down

0 comments on commit df1d3a7

Please sign in to comment.