Skip to content

Commit

Permalink
VW ID/ Audi etron: fix handling of partial errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Aug 21, 2022
1 parent bea6544 commit 373e538
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
8 changes: 3 additions & 5 deletions vehicle/vw/id/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package id

import (
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -73,16 +72,15 @@ func (v *API) Vehicles() (res []string, err error) {
return res, err
}

// Status implements the /status response
// Status implements the /status response.
// It is callers responsibility to check for embedded (partial) errors.
func (v *API) Status(vin string) (res Status, err error) {
uri := fmt.Sprintf("%s/vehicles/%s/status", BaseURL, vin)

req, err := request.New(http.MethodGet, uri, nil, request.AcceptJSON)

if err == nil {
if err = v.DoJSON(req, &res); err == nil && len(res.Error) > 0 {
err = errors.New("unknown error")
}
err = v.DoJSON(req, &res)
}

return res, err
Expand Down
24 changes: 24 additions & 0 deletions vehicle/vw/id/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var _ api.Battery = (*Provider)(nil)
// SoC implements the api.Vehicle interface
func (v *Provider) SoC() (float64, error) {
res, err := v.statusG()
if err == nil {
err = res.Error.Extract("batteryStatus")
}

if err == nil {
return float64(res.Data.BatteryStatus.CurrentSOCPercent), nil
}
Expand All @@ -46,6 +50,10 @@ func (v *Provider) Status() (api.ChargeStatus, error) {
status := api.StatusA // disconnected

res, err := v.statusG()
if err == nil {
err = res.Error.Extract("chargingStatus")
}

if err == nil {
if res.Data.PlugStatus.PlugConnectionState == "connected" {
status = api.StatusB
Expand All @@ -63,6 +71,10 @@ var _ api.VehicleFinishTimer = (*Provider)(nil)
// FinishTime implements the api.VehicleFinishTimer interface
func (v *Provider) FinishTime() (time.Time, error) {
res, err := v.statusG()
if err == nil {
err = res.Error.Extract("chargingStatus")
}

if err == nil {
cst := res.Data.ChargingStatus
return cst.CarCapturedTimestamp.Add(time.Duration(cst.RemainingChargingTimeToCompleteMin) * time.Minute), err
Expand All @@ -76,6 +88,10 @@ var _ api.VehicleRange = (*Provider)(nil)
// Range implements the api.VehicleRange interface
func (v *Provider) Range() (int64, error) {
res, err := v.statusG()
if err == nil {
err = res.Error.Extract("batteryStatus")
}

if err == nil {
return int64(res.Data.BatteryStatus.CruisingRangeElectricKm), nil
}
Expand All @@ -88,6 +104,10 @@ var _ api.VehicleOdometer = (*Provider)(nil)
// Odometer implements the api.VehicleOdometer interface
func (v *Provider) Odometer() (float64, error) {
res, err := v.statusG()
if err == nil {
err = res.Error.Extract("maintenanceStatus")
}

if err == nil {
if res.Data.MaintenanceStatus == nil {
return 0, api.ErrNotAvailable
Expand All @@ -103,6 +123,10 @@ var _ api.VehicleClimater = (*Provider)(nil)
// Climater implements the api.VehicleClimater interface
func (v *Provider) Climater() (active bool, outsideTemp, targetTemp float64, err error) {
res, err := v.statusG()
if err == nil {
err = res.Error.Extract("climatisationStatus")
}

if err == nil {
state := strings.ToLower(res.Data.ClimatisationStatus.ClimatisationState)

Expand Down
15 changes: 14 additions & 1 deletion vehicle/vw/id/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package id

import (
"fmt"
"strings"
"time"
)
Expand All @@ -17,7 +18,19 @@ type Status struct {
ClimatisationStatus `json:"climatisationStatus"` // may be temporarily not available
*MaintenanceStatus `json:"maintenanceStatus"` // optional
}
Error map[string]Error
Error ErrorMap
}

type ErrorMap map[string]Error

func (e ErrorMap) Extract(key string) error {
for k, v := range e {
if k == key {
return fmt.Errorf("%s: %s", v.Info, v.Message)
}
}

return nil
}

// Error is the error status
Expand Down

2 comments on commit 373e538

@bedmanforever
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @andig,

would this fix apply for Cupra Born as well, because they share the same platform?

@andig
Copy link
Member Author

@andig andig commented on 373e538 Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope- the Cupra API is different...

Please sign in to comment.