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 defaultConfig = TeslaAPI.Configuration.default

// Mock
let mockConfig = TeslaAPI.Configuration.mock

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

let teslaAPI = TeslaAPI(configuration: configuration, debugMode: true)

Base URL

Specify the base URL you want to use. This is useful if you want to connect to your own mock server for testing.

Client ID and Secret

If the default ID and Secret ever become invalid, you can specify an alernative set

Request Timeout

Specify request timeout interval (default: 30)

Debug Mode

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.