Skip to content

Tesla api implementation in python

License

Notifications You must be signed in to change notification settings

fabianhu/tesla_api

Repository files navigation

tesla_api

  • Tesla 2024 API implementation in Python.
  • This is a library.
  • See the example implementation in tesla_api_example

Overview

  • Implements the 2024 interface including authentication and token handling
  • It is a part of the ElectronFluxBalancer project
  • Supports developer account registration
  • Supports customer registration and key install on car
  • Gets vehicle data as python structures
  • Uses Tesla Vehicle Commands for signed commands.

Installation

  • include into your project as a library / git submodule in lib/tesla_api/
  • use bash git submodule update --remote to update
  • For best usage, please read the comments in the main file tesla_api_2024.py at the beginning and at the end of the file.
  • Example implementation in tesla_api_example
  • Maybe you have a look at the implementation at ElectronFluxBalancer
  • For sending commands, you need to get and build tesla-control, see below and the readme in the subdirectory tesla-control

Registration Process - skip for BLE only usage!

  1. Set up a third-party account at Tesla Developer

  2. Complete registration of your account

    • Generate a public/private key pair for Tesla Vehicle Commands using the secp256r1 curve:
      openssl ecparam -name prime256v1 -genkey -noout -out privatekey.pem
    • Extract the corresponding public key and save it in PEM format:
      openssl ec -in privatekey.pem -pubout -out publickey.pem
    • Host your public key in the /.well-known section of your website.
    • Store the public key at https://your.domain/.well-known/appspecific/com.tesla.3p.public-key.pem
    • Store both keys in the TeslaKeys directory
  3. Now within your python implementation

    • Generate a partner authentication token:
      partner_token = tesla_get_partner_auth_token(config.tesla_client_id, config.tesla_client_secret, AUDIENCE)
    • Make a POST call to /api/1/partner_accounts with your partner authentication token:
      import config
      _r = tesla_register_partner_account(partner_token, config.tesla_audience) # call once per audience you want to register for.
      print("account registration", _r)

    Now your partner registration should be completed. Check with: python _r = tesla_partner_check_public_key(partner_token, audience)

  4. Request authorization permissions from a customer

    • Generate a third-party token on their behalf:
      tesla_register_customer(myTesla) # register new customer (customer must log in and enter code here!)
  5. Customer must allow key on vehicle(s)

    tesla_register_customer_key()

For sending commands, build the vehicle-command Go module

  1. Install Go environment on your PC
  2. Download Tesla Vehicle Commands source
  3. Read the build instructions there
  4. In the downloaded directory go to
    cd vehicle-command/cmd/tesla-control/
  5. Build for PC (testing)
    go build .
    This generates an elf file tesla_control, which can be directly executed on the PC.
  6. Cross-compile on the PC for Raspberry Pi 2/3 zero2W
    env GOOS=linux GOARCH=arm GOARM=7 go build .
    # one line!
    This generates an elf file tesla_control, which can be directly executed on the Pi.
  7. Put this file into the lib/tesla_api/vehicle_command/ directory of your project on the Pi.
  8. Grant BLE permission:
    sudo setcap 'cap_net_admin=eip' "$(which ./tesla-control)"

BLE

Setup for BLE commands not influenced by the Tesla API rate limit:

  1. You have to generate a new key pair - the fleet keys will NOT work.

    Maybe you have to compile tesla-keygen first - do as described for the tesla-command.

    ./tesla-keygen -key-file ./relay_priv.pem create > ./relay.pem

    will generate two files - public and private

    relay.pem

    relay_priv.pem

    for local operation put the private key to: ./lib/tesla_api_ble/TeslaKeys/BLEprivatekey.pem

  2. optional: copy both keys to the remote machine Setup of password-less SSH should be done at this point.

    scp relay*.pem user@192.168.1.77:~
  3. add the public! key to the vehicle

    Sends add-key request to your car.

    Confirm by tapping NFC card on center console.

    ./tesla-control -ble -vin LRWYGCEK9NC999999 add-key-request ./relay.pem owner cloud_key
  4. Send commands Now you cen test the setup:

    ./tesla-control -ble -debug -vin LRWYGCEK9NC999999 -key-file ./relay_priv.pem honk

    You will hear, when it works.

  5. optional: Execute remotely:

    ssh -p 2222 user@192.168.1.98 'da command'
  6. edit config.py

    tesla_ble = True # True if commands to be sent via BLE
    tesla_remote = "user@192.168.1.77" # user:host if BLE command should be done on another device via SSH

Use this library as a submodule

Be aware, that in a git submodule in default, any commit will not be to the main branch, if you do not check out main.

  • Inside the library=submodule directory:
  • to check out before commit: git checkout main
  • to update: git pull origin main

About

Tesla api implementation in python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published