From 530d7a1bdb3b04d8d42d4c04bd9e2c5bb8a709c2 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 27 Jul 2024 12:42:53 +0200 Subject: [PATCH] Cupra: fix api (#15081) --- vehicle/seat/cupra/api.go | 18 +++++++++++++++++- vehicle/seat/cupra/provider.go | 28 ++++++++++++++++++++++------ vehicle/seat/cupra/types.go | 16 +++++++++++----- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/vehicle/seat/cupra/api.go b/vehicle/seat/cupra/api.go index 3ba653033e..7d1a8eca9e 100644 --- a/vehicle/seat/cupra/api.go +++ b/vehicle/seat/cupra/api.go @@ -52,7 +52,23 @@ func (v *API) Vehicles(userID string) ([]Vehicle, error) { // Status implements the /status response func (v *API) Status(userID, vin string) (Status, error) { var res Status - uri := fmt.Sprintf("%s/v2/users/%s/vehicles/%s/mycar", BaseURL, userID, vin) + uri := fmt.Sprintf("%s/v5/users/%s/vehicles/%s/mycar", BaseURL, userID, vin) + err := v.GetJSON(uri, &res) + return res, err +} + +// ParkingPosition implements the /parkingposition response +func (v *API) ParkingPosition(vin string) (Position, error) { + var res Position + uri := fmt.Sprintf("%s/v1/vehicles/%s/parkingposition", BaseURL, vin) + err := v.GetJSON(uri, &res) + return res, err +} + +// Mileage implements the /mileage response +func (v *API) Mileage(vin string) (Mileage, error) { + var res Mileage + uri := fmt.Sprintf("%s/v1/vehicles/%s/mileage", BaseURL, vin) err := v.GetJSON(uri, &res) return res, err } diff --git a/vehicle/seat/cupra/provider.go b/vehicle/seat/cupra/provider.go index 404d216687..ee06d70e60 100644 --- a/vehicle/seat/cupra/provider.go +++ b/vehicle/seat/cupra/provider.go @@ -10,8 +10,10 @@ import ( // Provider is an api.Vehicle implementation for Seat Cupra cars type Provider struct { - statusG func() (Status, error) - action func(string, string) error + statusG func() (Status, error) + positionG func() (Position, error) + milageG func() (Mileage, error) + action func(string, string) error } // NewProvider creates a vehicle api provider @@ -20,6 +22,12 @@ func NewProvider(api *API, userID, vin string, cache time.Duration) *Provider { statusG: provider.Cached(func() (Status, error) { return api.Status(userID, vin) }, cache), + positionG: provider.Cached(func() (Position, error) { + return api.ParkingPosition(vin) + }, cache), + milageG: provider.Cached(func() (Mileage, error) { + return api.Mileage(vin) + }, cache), action: func(action, cmd string) error { return api.Action(vin, action, cmd) }, @@ -32,7 +40,7 @@ var _ api.Battery = (*Provider)(nil) // Soc implements the api.Vehicle interface func (v *Provider) Soc() (float64, error) { res, err := v.statusG() - return res.Engines.Primary.Level, err + return res.Engines.Primary.LevelPct, err } var _ api.ChargeState = (*Provider)(nil) @@ -81,15 +89,23 @@ var _ api.VehicleRange = (*Provider)(nil) // Range implements the api.VehicleRange interface func (v *Provider) Range() (int64, error) { res, err := v.statusG() - return int64(res.Engines.Primary.Range.Value), err + return int64(res.Engines.Primary.RangeKm), err } var _ api.VehicleOdometer = (*Provider)(nil) // Odometer implements the api.VehicleOdometer interface func (v *Provider) Odometer() (float64, error) { - res, err := v.statusG() - return res.Measurements.MileageKm, err + res, err := v.milageG() + return res.MileageKm, err +} + +var _ api.VehiclePosition = (*Provider)(nil) + +// Position implements the api.VehiclePosition interface +func (v *Provider) Position() (float64, float64, error) { + res, err := v.positionG() + return res.Lat, res.Lon, err } var _ api.VehicleClimater = (*Provider)(nil) diff --git a/vehicle/seat/cupra/types.go b/vehicle/seat/cupra/types.go index a65cb4a797..2177da6728 100644 --- a/vehicle/seat/cupra/types.go +++ b/vehicle/seat/cupra/types.go @@ -10,11 +10,8 @@ type Vehicle struct { type Engine struct { Type string FuelType string - Range struct { - Value float64 - Unit string - } - Level float64 + RangeKm float64 + LevelPct float64 } type Status struct { @@ -28,6 +25,7 @@ type Status struct { ChargeMode string Active bool RemainingTime int64 + CurrentPct float64 ProgressBarPct float64 } Climatisation struct { @@ -41,3 +39,11 @@ type Status struct { MileageKm float64 } } + +type Mileage struct { + MileageKm float64 +} + +type Position struct { + Lat, Lon float64 +}