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

Inspiration

I have been a long time fan of Tesla ever since the original Roadster. When the Model 3 was announced, I actually camped out overnight so I could place my reservation. I am also a big Apple fan and really enjoy my Apple Watch, although wish there were more interesting complications I could add to my watch faces. So I decided to make my own app while I wait for my Model 3.

Fast foward to mid-2018, I have officially released my app, called AutoMate for Tesla, on the App Store and have also received my Model 3! During development of AutoMate, I decided to separate the core Tesla API interactions into a separate project and open source it so others could also build really cool apps too.

I am looking forward to seeing what people do with TeslaKit and if you're anything like me and thoroughly enjoy your car and own an Apple Watch, I think you will also enjoy AutoMate so please give it a try!

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>

Add an import statement for TeslaKit into your file

import TeslaKit

TeslaAPI

Create a new TeslaAPI instance

let teslaAPI = TeslaAPI()

Also see other initialization configuration options

Access Token

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

let email = "[email protected]"
let password = "M@R$R0X!"

teslaAPI.getAccessToken(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.getVehicles { (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.getData(vehicle.id) { (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 = Command.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 = SetTemperature(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)    // 2017
print(vin.serialNo)     // 999999
print(vin.vinString)    // 5YJSA1E2XHF999999

Apps Built With TeslaKit

Is your app using TeslaKit? Let me know and I'll feature it here too!

Tesla Referral Program

Buying a new Tesla? Use my referral code and receive 1,000 free Supercharger miles!

Author

jjjjaren, [email protected]

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.