Skip to content

Commit

Permalink
BMW: fix multiple vehicles in account
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 5, 2021
1 parent 79d6388 commit 111d499
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions vehicle/bmw/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (v *API) Vehicles() ([]string, error) {
// Status implements the /user/vehicles/<vin>/status api
func (v *API) Status(vin string) (VehicleStatus, error) {
var resp VehiclesStatusResponse
uri := fmt.Sprintf("%s/eadrax-vcs/v1/vehicles?apptimezone=60&appDateTime=%d&vin=%s", CocoApiURI, time.Now().Unix(), vin)
uri := fmt.Sprintf("%s/eadrax-vcs/v1/vehicles?apptimezone=60&appDateTime=%d", CocoApiURI, time.Now().Unix())

req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"X-User-Agent": v.xUserAgent,
Expand All @@ -71,9 +72,15 @@ func (v *API) Status(vin string) (VehicleStatus, error) {
err = v.DoJSON(req, &resp)
}

if l := len(resp); l != 1 {
return VehicleStatus{}, fmt.Errorf("unexpected length: %d", l)
if err == nil {
for _, res := range resp {
if res.VIN == vin {
return res, nil
}
}

err = api.ErrNotAvailable
}

return resp[0], err
return VehicleStatus{}, err
}
4 changes: 3 additions & 1 deletion vehicle/bmw/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type Vehicle struct {
type VehiclesStatusResponse []VehicleStatus

type VehicleStatus struct {
Properties struct {
VIN, Model, Brand string
Year int
Properties struct {
ChargingState struct {
ChargePercentage int
State string // CHARGING, ERROR, FINISHED_FULLY_CHARGED, FINISHED_NOT_FULL, INVALID, NOT_CHARGING, WAITING_FOR_CHARGING, COMPLETED
Expand Down

0 comments on commit 111d499

Please sign in to comment.