Skip to content

Commit

Permalink
Renault: error if not paired or connected to driver (evcc-io#3668)
Browse files Browse the repository at this point in the history
  • Loading branch information
ickeundso authored Jul 3, 2022
1 parent e15bee4 commit 85ef96c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ server-key.pem
ca-key.pem
cmd/wip
fly.toml
.idea
55 changes: 36 additions & 19 deletions vehicle/renault.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,26 @@ type kamereonAccount struct {
}

type kamereonVehicle struct {
Brand string `json:"brand"`
VIN string `json:"vin"`
Status string `json:"status"`
Brand string `json:"brand"`
VIN string `json:"vin"`
Status string `json:"status"`
ConnectedDriver connectedDriver `json:"ConnectedDriver"`
}

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

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

return nil
}

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

type kamereonData struct {
Expand Down Expand Up @@ -145,10 +162,20 @@ func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
err = v.authFlow()
}

var car kamereonVehicle
if err == nil {
v.vin, err = ensureVehicle(cc.VIN, func() ([]string, error) {
return v.kamereonVehicles(v.accountID)
})
v.vin, car, err = ensureVehicleWithFeature(cc.VIN,
func() ([]kamereonVehicle, error) {
return v.kamereonVehicles(v.accountID)
},
func(v kamereonVehicle) (string, kamereonVehicle) {
return v.VIN, v
},
)
}

if err == nil {
err = car.Available()
}

v.batteryG = provider.Cached(v.batteryAPI, cc.Cache)
Expand Down Expand Up @@ -308,20 +335,10 @@ func (v *Renault) kamereonPerson(personID string) (string, error) {
return res.Accounts[0].AccountID, err
}

func (v *Renault) kamereonVehicles(accountID string) ([]string, error) {
uri := fmt.Sprintf("%s/commerce/v1/accounts/%s/vehicles", v.kamereon.Target, accountID)
func (v *Renault) kamereonVehicles(configVIN string) ([]kamereonVehicle, error) {
uri := fmt.Sprintf("%s/commerce/v1/accounts/%s/vehicles", v.kamereon.Target, v.accountID)
res, err := v.kamereonRequest(uri, nil)

var vehicles []string
if err == nil {
for _, v := range res.VehicleLinks {
if strings.ToUpper(v.Status) == "ACTIVE" {
vehicles = append(vehicles, v.VIN)
}
}
}

return vehicles, err
return res.VehicleLinks, err
}

// batteryAPI provides battery-status api response
Expand Down

0 comments on commit 85ef96c

Please sign in to comment.