Skip to content

Commit

Permalink
Drop vehicle ID
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Feb 9, 2024
1 parent b9a438a commit 44e49cc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 5 additions & 5 deletions states.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ type MobileEnabledResponse struct {
// MobileEnabled returns if the vehicle is mobile enabled for Tesla API control
func (v *Vehicle) MobileEnabled() (bool, error) {
r := &MobileEnabledResponse{}
if err := v.c.getJSON(v.c.baseURL+"/vehicles/"+strconv.FormatInt(v.ID, 10)+"/mobile_enabled", r); err != nil {
if err := v.c.getJSON(v.c.baseURL+"/vehicles/"+v.Vin+"/mobile_enabled", r); err != nil {
return false, err
}
return r.Bool, nil
Expand Down Expand Up @@ -288,7 +288,7 @@ type NearbyChargingSitesResponse struct {
// NearbyChargingSites returns the charging sites near the vehicle.
func (v *Vehicle) NearbyChargingSites() (*NearbyChargingSitesResponse, error) {
resp := &NearbyChargingSitesResponse{}
path := strings.Join([]string{v.c.baseURL, "vehicles", strconv.FormatInt(v.ID, 10), "nearby_charging_sites"}, "/")
path := strings.Join([]string{v.c.baseURL, "vehicles", v.Vin, "nearby_charging_sites"}, "/")
if err := v.c.getJSON(path, resp); err != nil {
return nil, err
}
Expand All @@ -307,9 +307,9 @@ func stateError(sr *VehicleData) error {
}

// A utility function to fetch the appropriate state of the vehicle
func (c *Client) fetchState(id int64) (*VehicleData, error) {
func (c *Client) fetchState(vin string) (*VehicleData, error) {
var res VehicleData
path := strings.Join([]string{c.baseURL, "vehicles", strconv.FormatInt(id, 10), "vehicle_data"}, "/")
path := strings.Join([]string{c.baseURL, "vehicles", vin, "vehicle_data"}, "/")
if err := c.getJSON(path, &res); err != nil {
return nil, err
}
Expand All @@ -321,5 +321,5 @@ func (c *Client) fetchState(id int64) (*VehicleData, error) {

// Data : Get data of the vehicle (calling this will not permit the car to sleep)
func (v Vehicle) Data() (*VehicleData, error) {
return v.c.fetchState(v.ID)
return v.c.fetchState(v.Vin)
}
9 changes: 3 additions & 6 deletions vehicles.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package tesla

import (
"strconv"
"strings"
)

// VehiclePartialResponse represents the vehicle response root data as returned from the Tesla API.
type VehiclePartialResponse struct {
Color interface{} `json:"color"`
DisplayName string `json:"display_name"`
ID int64 `json:"id"`
OptionCodes string `json:"option_codes"`
VehicleID uint64 `json:"vehicle_id"`
Vin string `json:"vin"`
Expand All @@ -31,7 +29,6 @@ type VehiclePartialResponse struct {
type Vehicle struct {
Color interface{} `json:"color"`
DisplayName string `json:"display_name"`
ID int64 `json:"id"`
OptionCodes string `json:"option_codes"`
VehicleID uint64 `json:"vehicle_id"`
Vin string `json:"vin"`
Expand Down Expand Up @@ -112,17 +109,17 @@ func (c *Client) Vehicles() ([]*Vehicle, error) {
}

// Vehicle fetches the vehicle by ID associated to a Tesla account via the API.
func (c *Client) Vehicle(vehicleID int64) (*Vehicle, error) {
func (c *Client) Vehicle(vin string) (*Vehicle, error) {
resp := &VehicleResponse{}
if err := c.getJSON(c.baseURL+"/vehicles/"+strconv.FormatInt(vehicleID, 10), resp); err != nil {
if err := c.getJSON(c.baseURL+"/vehicles/"+vin, resp); err != nil {
return nil, err
}
resp.Response.c = c
return resp.Response, nil
}

func (v *Vehicle) basePath() string {
return strings.Join([]string{v.c.baseURL, "vehicles", strconv.FormatInt(v.ID, 10)}, "/")
return strings.Join([]string{v.c.baseURL, "vehicles", v.Vin}, "/")
}

func (v *Vehicle) commandPath(command string) string {
Expand Down
8 changes: 4 additions & 4 deletions vehicles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

var (
VehiclesJSON = `{"response":[{"color":null,"display_name":"Macak","id":1234,"option_codes":"MS04,RENA,AU01,BC0R,BP01,BR01,BS00,CDM0,CH00,PBSB,CW02,DA02,DCF0,DRLH,DSH7,DV4W,FG02,HP00,IDPB,IX01,LP01,ME02,MI00,PA00,PF01,PI01,PK00,PS01,PX00,PX4D,QNEB,RFP2,SC01,SP00,SR01,SU01,TM00,TP03,TR01,UTAB,WTSG,WTX0,X001,X003,X007,X011,X013,X019,X024,X027,X028,X031,X037,X040,YF01,COUS","vehicle_id":456,"vin":"abc123","tokens":["1","2"],"state":"online","id_s":"789","remote_start_enabled":true,"calendar_enabled":true,"notifications_enabled":true,"backseat_token":null,"backseat_token_updated_at":null,"vehicle_config":{"timestamp":1614069716042}}],"count":1}`
VehicleJSON = `{"response":{"color":null,"display_name":"Macak","id":1234,"option_codes":"MS04,RENA,AU01,BC0R,BP01,BR01,BS00,CDM0,CH00,PBSB,CW02,DA02,DCF0,DRLH,DSH7,DV4W,FG02,HP00,IDPB,IX01,LP01,ME02,MI00,PA00,PF01,PI01,PK00,PS01,PX00,PX4D,QNEB,RFP2,SC01,SP00,SR01,SU01,TM00,TP03,TR01,UTAB,WTSG,WTX0,X001,X003,X007,X011,X013,X019,X024,X027,X028,X031,X037,X040,YF01,COUS","vehicle_id":456,"vin":"abc123","tokens":["1","2"],"state":"online","id_s":"789","remote_start_enabled":true,"calendar_enabled":true,"notifications_enabled":true,"backseat_token":null,"backseat_token_updated_at":null},"count":1}`
VehiclesJSON = `{"response":[{"color":null,"display_name":"Macak","vin":"1234","option_codes":"MS04,RENA,AU01,BC0R,BP01,BR01,BS00,CDM0,CH00,PBSB,CW02,DA02,DCF0,DRLH,DSH7,DV4W,FG02,HP00,IDPB,IX01,LP01,ME02,MI00,PA00,PF01,PI01,PK00,PS01,PX00,PX4D,QNEB,RFP2,SC01,SP00,SR01,SU01,TM00,TP03,TR01,UTAB,WTSG,WTX0,X001,X003,X007,X011,X013,X019,X024,X027,X028,X031,X037,X040,YF01,COUS","vehicle_id":456,"tokens":["1","2"],"state":"online","id_s":"789","remote_start_enabled":true,"calendar_enabled":true,"notifications_enabled":true,"backseat_token":null,"backseat_token_updated_at":null,"vehicle_config":{"timestamp":1614069716042}}],"count":1}`
VehicleJSON = `{"response":{"color":null,"display_name":"Macak","vin":"1234","option_codes":"MS04,RENA,AU01,BC0R,BP01,BR01,BS00,CDM0,CH00,PBSB,CW02,DA02,DCF0,DRLH,DSH7,DV4W,FG02,HP00,IDPB,IX01,LP01,ME02,MI00,PA00,PF01,PI01,PK00,PS01,PX00,PX4D,QNEB,RFP2,SC01,SP00,SR01,SU01,TM00,TP03,TR01,UTAB,WTSG,WTX0,X001,X003,X007,X011,X013,X019,X024,X027,X028,X031,X037,X040,YF01,COUS","vehicle_id":456,"tokens":["1","2"],"state":"online","id_s":"789","remote_start_enabled":true,"calendar_enabled":true,"notifications_enabled":true,"backseat_token":null,"backseat_token_updated_at":null},"count":1}`
)

func TestVehiclesSpec(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestVehicle(t *testing.T) {
client := NewTestClient(ts)

Convey("Should get vehicle", t, func() {
vehicle, err := client.Vehicle(1234)
vehicle, err := client.Vehicle("1234")
So(err, ShouldBeNil)
So(vehicle.DisplayName, ShouldEqual, "Macak")
So(vehicle.CalendarEnabled, ShouldBeTrue)
Expand All @@ -44,7 +44,7 @@ func TestVehicle_CommandPath(t *testing.T) {
defer ts.Close()

client := NewTestClient(ts)
v := &Vehicle{ID: 1, c: client}
v := &Vehicle{Vin: "1", c: client}

Convey("Should have a URL with /command/", t, func() {
So(v.commandPath("honk_horn"), ShouldEndWith, "/api/1/vehicles/1/command/honk_horn")
Expand Down

0 comments on commit 44e49cc

Please sign in to comment.