Standardize how vehicles are detected (#347)
This commit is contained in:
parent
6d8d4b3954
commit
8a1a39d1c0
4 changed files with 51 additions and 43 deletions
|
|
@ -125,17 +125,10 @@ func NewAudiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
if err == nil && cc.VIN == "" {
|
||||
vehicles, err := v.vehicles()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get vehicles: %v", err)
|
||||
v.vin, err = findVehicle(v.vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
|
||||
if len(vehicles) != 1 {
|
||||
return nil, fmt.Errorf("cannot find vehicle: %v", vehicles)
|
||||
}
|
||||
|
||||
v.vin = vehicles[0]
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
|
||||
return v, err
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ type bmwDynamicResponse struct {
|
|||
}
|
||||
|
||||
type bmwVehiclesResponse []struct {
|
||||
Vin string `json:"vin"`
|
||||
VIN string `json:"vin"`
|
||||
}
|
||||
|
||||
// BMW is an api.Vehicle implementation for BMW cars
|
||||
|
|
@ -66,22 +66,16 @@ func NewBMWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
vin: cc.VIN,
|
||||
}
|
||||
|
||||
if cc.VIN == "" {
|
||||
vehicles, err := v.vehicles()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get vehicles: %v", err)
|
||||
}
|
||||
|
||||
if len(vehicles) != 1 {
|
||||
return nil, fmt.Errorf("cannot find vehicle: %v", vehicles)
|
||||
}
|
||||
|
||||
v.vin = vehicles[0].Vin
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
|
||||
v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
|
||||
|
||||
var err error
|
||||
if cc.VIN == "" {
|
||||
v.vin, err = findVehicle(v.vehicles())
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
|
|
@ -146,17 +140,24 @@ func (v *BMW) request(uri string) (*http.Request, error) {
|
|||
}
|
||||
|
||||
// vehicles implements returns the list of user vehicles
|
||||
func (v *BMW) vehicles() (bmwVehiclesResponse, error) {
|
||||
func (v *BMW) vehicles() ([]string, error) {
|
||||
var br bmwVehiclesResponse
|
||||
uri := fmt.Sprintf("%s/me/vehicles/v2/", bmwAPI)
|
||||
|
||||
var vehicles []string
|
||||
|
||||
req, err := v.request(uri)
|
||||
if err != nil {
|
||||
return br, err
|
||||
if err == nil {
|
||||
_, err = v.RequestJSON(req, &br)
|
||||
}
|
||||
|
||||
_, err = v.RequestJSON(req, &br)
|
||||
return br, err
|
||||
if err == nil {
|
||||
for _, v := range br {
|
||||
vehicles = append(vehicles, v.VIN)
|
||||
}
|
||||
}
|
||||
|
||||
return vehicles, err
|
||||
}
|
||||
|
||||
// chargeState implements the Vehicle.ChargeState interface
|
||||
|
|
|
|||
17
vehicle/helper.go
Normal file
17
vehicle/helper.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package vehicle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func findVehicle(vehicles []string, err error) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot get vehicles: %v", err)
|
||||
}
|
||||
|
||||
if len(vehicles) != 1 {
|
||||
return "", fmt.Errorf("cannot find vehicle: %v", vehicles)
|
||||
}
|
||||
|
||||
return vehicles[0], nil
|
||||
}
|
||||
|
|
@ -129,8 +129,12 @@ func NewRenaultFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
if err == nil {
|
||||
err = v.authFlow()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
if err == nil && cc.VIN == "" {
|
||||
v.vin, err = findVehicle(v.kamereonVehicles(v.accountID))
|
||||
if err == nil {
|
||||
log.DEBUG.Printf("found vehicle: %v", v.vin)
|
||||
}
|
||||
}
|
||||
|
||||
v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
|
||||
|
|
@ -174,14 +178,6 @@ func (v *Renault) authFlow() error {
|
|||
if v.accountID == "" {
|
||||
return errors.New("missing accountID")
|
||||
}
|
||||
|
||||
// find VIN
|
||||
if v.vin == "" {
|
||||
v.vin, err = v.kamereonVehicles(v.accountID)
|
||||
if v.vin == "" {
|
||||
return errors.New("missing vin")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -289,19 +285,20 @@ func (v *Renault) kamereonPerson(personID string) (string, error) {
|
|||
return kr.Accounts[0].AccountID, err
|
||||
}
|
||||
|
||||
func (v *Renault) kamereonVehicles(accountID string) (string, error) {
|
||||
func (v *Renault) kamereonVehicles(accountID string) ([]string, error) {
|
||||
uri := fmt.Sprintf("%s/commerce/v1/accounts/%s/vehicles", v.kamereon.Target, accountID)
|
||||
kr, err := v.kamereonRequest(uri)
|
||||
|
||||
var vehicles []string
|
||||
if err == nil {
|
||||
for _, v := range kr.VehicleLinks {
|
||||
if strings.ToUpper(v.Status) == "ACTIVE" {
|
||||
return v.VIN, nil
|
||||
vehicles = append(vehicles, v.VIN)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", err
|
||||
return vehicles, err
|
||||
}
|
||||
|
||||
// chargeState implements the Vehicle.ChargeState interface
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue