Skip to content

Commit

Permalink
Add support for the new vehicle_data endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasman committed Mar 7, 2024
1 parent 70a7c88 commit fce960b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
21 changes: 19 additions & 2 deletions Sources/TeslaSwift/TeslaEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum Endpoint {
case vehicles
case vehicleSummary(vehicleID: VehicleId)
case mobileAccess(vehicleID: VehicleId)
case allStates(vehicleID: VehicleId)
case allStates(vehicleID: VehicleId, endpoints: [AllStatesEndpoints])
case chargeState(vehicleID: VehicleId)
case climateState(vehicleID: VehicleId)
case driveState(vehicleID: VehicleId)
Expand All @@ -46,6 +46,21 @@ enum Endpoint {
case getBatteryPowerHistory(batteryID: BatteryId)
}

public enum AllStatesEndpoints: String {
case chargeState = "charge_state"
case climateState = "climate_state"
case closuresState = "closures_state"
case driveState = "drive_state"
case guiSettings = "gui_settings"
case locationData = "location_data" // Same as driveState but with location
case vehicleConfig = "vehicle_config"
case vehicleState = "vehicle_state"
case vehicleDataCombo = "vehicle_data_combo"

public static var all: [AllStatesEndpoints] = [.chargeState, .climateState, .closuresState, .driveState, .guiSettings, .vehicleConfig, .vehicleState]
public static var allWithLocation: [AllStatesEndpoints] = [.chargeState, .climateState, .closuresState, .locationData, .guiSettings, .vehicleConfig, .vehicleState]
}

extension Endpoint {

var path: String {
Expand Down Expand Up @@ -73,7 +88,7 @@ extension Endpoint {
return "/api/1/vehicles/\(vehicleID.id)"
case .mobileAccess(let vehicleID):
return "/api/1/vehicles/\(vehicleID.id)/mobile_enabled"
case .allStates(let vehicleID):
case .allStates(let vehicleID, _):
return "/api/1/vehicles/\(vehicleID.id)/vehicle_data"
case .chargeState(let vehicleID):
return "/api/1/vehicles/\(vehicleID.id)/data_request/charge_state"
Expand Down Expand Up @@ -135,6 +150,8 @@ extension Endpoint {
return [URLQueryItem(name: "token", value: token)]
case let .getEnergySiteHistory(_, period):
return [URLQueryItem(name: "period", value: period.rawValue), URLQueryItem(name: "kind", value: "energy")]
case let .allStates(_, endpoints):
return [URLQueryItem(name: "endpoints", value: endpoints.map({ $0.rawValue }).joined(separator: ";"))]
default:
return []
}
Expand Down
9 changes: 6 additions & 3 deletions Sources/TeslaSwift/TeslaSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ extension TeslaSwift {

- returns: A completion handler with all the data
*/
public func getAllData(_ vehicle: Vehicle) async throws -> VehicleExtended {
public func getAllData(_ vehicle: Vehicle, endpoints: [AllStatesEndpoints]) async throws -> VehicleExtended {
_ = try await checkAuthentication()
let vehicleID = vehicle.id!
let response: Response<VehicleExtended> = try await request(.allStates(vehicleID: vehicleID))
let response: Response<VehicleExtended> = try await request(.allStates(vehicleID: vehicleID, endpoints: endpoints))
return response.response
}

Expand Down Expand Up @@ -679,11 +679,14 @@ extension TeslaSwift {
var urlComponents = URLComponents(url: URL(string: endpoint.baseURL(teslaAPI: teslaAPI))!, resolvingAgainstBaseURL: true)
urlComponents?.path = endpoint.path
urlComponents?.queryItems = endpoint.queryParameters
let percentEncodedQuery = urlComponents?.percentEncodedQuery
// Tesla requires semicolons to be encoded in the query, so we force .urlQueryAllowed on the query params to remove the semicolon
urlComponents?.percentEncodedQuery = percentEncodedQuery?.replacingOccurrences(of: ";", with: "%3B")
var request = URLRequest(url: urlComponents!.url!)
request.httpMethod = endpoint.method

request.setValue("TeslaSwift", forHTTPHeaderField: "User-Agent")
request.setValue("TeslaApp/4.9.2", forHTTPHeaderField: "x-tesla-user-agent")
request.setValue("TeslaApp/4.30.6", forHTTPHeaderField: "x-tesla-user-agent")

if let token = self.token?.accessToken {
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
Expand Down
2 changes: 1 addition & 1 deletion TeslaSwiftDemo/VehicleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class VehicleViewController: UIViewController {
@IBAction func gettAll(_ sender: Any) {
guard let vehicle = vehicle else { return }
Task { @MainActor in
let extendedVehicle = try await api.getAllData(vehicle)
let extendedVehicle = try await api.getAllData(vehicle, endpoints: AllStatesEndpoints.allWithLocation)
self.textView.text = "All data:\n" +
extendedVehicle.jsonString!
}
Expand Down

0 comments on commit fce960b

Please sign in to comment.