Skip to content

HamblinSoft/TeslaKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TeslaKit

Platform Cocoapods compatible Version License Language Twitter

TeslaKit is a framework written in Swift that makes it easy for you to interface with Tesla's mobile API and communicate with your Tesla automobiles.

Features

  • Authenticate with Tesla's API to obtain an access token
  • Retrieve a list of vehicles associated with your Tesla account
  • Obtain all data on your vehicle
  • Send commands to your vehicle
  • Utilizes ObjectMapper for JSON mapping
  • Uses Structures to maintain thread safe operations
  • VIN Decoder
  • Summon and Homelink - Coming soon

Installation

TeslaKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'TeslaKit'

Usage

Add an ATS exception domain for owner-api.teslamotors.com in your info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>owner-api.teslamotors.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

Import TeslaKit

import TeslaKit

TeslaAPI

Create a new TeslaAPI instance

let teslaAPI = TeslaAPI()

Also see initialization configuration

Access Token

Obtain an Access Token by logging in with your Tesla account credentials

let email = "e.musk@teslakit.com"
let password = "M@R$R0X!"

teslaAPI.accessToken(email: email, password: password) { (httpResponse, dataOrNil, errorOrNil) in

    guard let accessToken = dataOrNil?.accessToken else { return }

    // Set the accessToken for use with future requests
    teslaAPI.setAccessToken(accessToken)
}

Vehicle List

Obtain a list of vehicles associated with your account

teslaAPI.vehicles { (httpResponse, dataOrNil, errorOrNil) in

    guard let vehicle = dataOrNil?.vehicles.first else { return }

    print("Hello, \(vehicle.displayName)")
}

Vehicle Data

Obtain all data for a vehicle

teslaAPI.data(for: vehicle) { (httpResponse, dataOrNil, errorOrNil) in

    guard let vehicle = dataOrNil else { return }

    print("Battery is at \(vehicle.chargeState.batteryLevel)%")
}

Send Command

Send a command to a vehicle

let command = TKCommand.unlockDoors

teslaAPI.send(command, to: vehicle) { response in
    if response.result {
        print("Command sent successfully!")
    }
}

Send a command to a vehicle with request parameters

let parameters = TKSetTemperature(driverTemp: 21.0, passengerTemp: 21.0)

teslaAPI.send(.setTemperature, to: vehicle, parameters: parameters) { response in
    if response.result {
        print("Command sent successfully!")
    }
}

Configuration

Default

let teslaAPI = TeslaAPI(configuration: .default) // same as TeslaAPI()

The default configuration points to Tesla's production owner API

Mock

let teslaAPI = TeslaAPI(configuration: .mock)

The mock configuration directs all requests to a custom server that returns a predefined responses for each of the owner API endpoints allowing you to work with some real-looking data to work with without having to own a Tesla vehicle or without having a Tesla account at all.

Note: Because this is a free service, requests are occasionally throttled.

Custom

let customConfig = TeslaAPI.Configuration(baseURL: URL(string: "SOME_URL")!,
                                          clientId: "SOME_CLIENT_ID",
                                          clientSecret: "SOME_CLIENT_SECRET",
                                          requestTimeout: 30)

let teslaAPI = TeslaAPI(configuration: customConfig)

The custom configuration allows you to specify your own baseURL. This is convenient if you would like to point to your own environment. You can also specify an alternate clientId and clientSecret value. You can also specify a different request timeout interval (default: 30)

Debug Mode

let teslaAPI = TeslaAPI(debugMode: true)

Enabling debug mode will print all the raw request and response information to the console (default: false)

VIN Decoder

let vin = VIN(vinString: "5YJSA1E2XHF999999")!
print(vin.make)         // Model S
print(vin.driveUnit)    // Dual Motor
print(vin.modelYear)    // TeslaKit.VINComponent.ModelYear.year2017
print(vin.serialNo)     // 999999
print(vin.vinString)    // 5YJSA1E2XHF999999

Author

jjjjaren, jjjjaren@gmail.com

License

This project is licensed under the terms of the MIT license. See the LICENSE file.

This project is in no way affiliated with Tesla Inc. This project is open source under the MIT license, which means you have full access to the source code and can modify it to fit your own needs.