diff --git a/vehicle/toyota/provider.go b/vehicle/toyota/provider.go index 97eafe8be..99242efd6 100644 --- a/vehicle/toyota/provider.go +++ b/vehicle/toyota/provider.go @@ -27,5 +27,8 @@ func (v *Provider) Soc() (float64, error) { // Range implements the api.VehicleRange interface func (v *Provider) Range() (int64, error) { res, err := v.status() - return int64(res.Payload.EvRangeWithAc.Value), err + if err != nil { + return 0, err + } + return res.Payload.EvRangeWithAc.ValueInKilometers() } diff --git a/vehicle/toyota/types.go b/vehicle/toyota/types.go index c2fab81c9..353212d3f 100644 --- a/vehicle/toyota/types.go +++ b/vehicle/toyota/types.go @@ -22,11 +22,26 @@ type Status struct { } `json:"payload"` } +const kmPerMile = 1.609344 + type EvRange struct { Unit string `json:"unit"` Value float64 `json:"value"` } +func (e EvRange) ValueInKilometers() (int64, error) { + u := strings.ToLower(strings.TrimSpace(e.Unit)) + + switch u { + case "km": + return int64(e.Value), nil + case "mi": + return int64(e.Value * kmPerMile), nil + default: + return 0, fmt.Errorf("unsupported unit type: %s", e.Unit) + } +} + type Auth struct { AuthID string `json:"authId"` Callbacks []AuthCallback `json:"callbacks"`