I tried to touch Tesla's API

Thing you want to do

I heard that Tesla's API can be used unofficially, so I immediately touched it Since you can get vehicle information, let's get it with Python for the time being and notify Slack

Preparation

--See below for API documentation (unofficial)
https://tesla-api.timdorr.com/api-basics/authentication

--Authentication ID is as described below
https://pastebin.com/pS7Z6yyP

Authentication ID


TESLA_CLIENT_ID=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384
TESLA_CLIENT_SECRET=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3

Operating environment, etc.

--Confirmed with Python 3.7 series (Windows) ――For the time being, I tried to make it work using GAE

Overview

--Authentication is ʻOAuth2Service --I don't want to embed ID / PASS, so make it a separate file and get it withconfig parser` --The structure of the data can be roughly understood by looking at the contents of resp. --Since the process to wake up the vehicle is not included, start it with the application etc. in advance and then execute the script (Since it does not include retry etc., it will fail and end)

script

teslaapi_test.py


def access_vehicle():
    import json
    from rauth import OAuth2Service #For TESLA API
    import configparser

    config = configparser.ConfigParser()
    config.read('config.conf')
    section = 'development'
    
    teslaID = config.get(section,'teslaID')
    password = config.get(section,'password')

    tesla = OAuth2Service(
        client_id = config.get(section,'client_id'),
        client_secret = config.get(section,'client_secret'),
        access_token_url = "https://owner-api.teslamotors.com/oauth/token",
        authorize_url = "https://owner-api.teslamotors.com/oauth/token",
        base_url = "https://owner-api.teslamotors.com/",
        )
    data = {"grant_type": "password",
        "client_id": config.get(section,'client_id'),
        "client_secret": config.get(section,'client_secret'),
        "email": teslaID,
        "password": password}

    session = tesla.get_auth_session(data=data, decoder=json.loads)
    access_token = session.access_token
    my_session = tesla.get_session(token=access_token)

    url = 'https://owner-api.teslamotors.com/api/1/vehicles/'
    vehicles = my_session.get(url).json()['response'][0]
    mycar = vehicles['id_s']

    return mycar,my_session

def get_vehicle_status():
    import json
    import datetime
    from rauth import OAuth2Service #For TESLA API

    mycar,my_session = access_vehicle()

    url_vehicle_state = "https://owner-api.teslamotors.com/api/1/vehicles/{:}/vehicle_data".format(mycar)
    resp = my_session.get(url_vehicle_state).json()

    return resp

Create the following as a configuration file

config.conf


[development]
#tesla
teslaID = 'User ID'
password = 'password'
client_id = '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384'
client_secret = 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'

Slack notification

Notifications are easy using Incoming-webhook. https://api.slack.com/messaging/webhooks#posting_with_webhooks

slack notification


import slackweb

slack = slackweb.Slack(url="Webhook URL")
slack.notify(text="Cruising distance: {}[km]".format(resp['response']['battery_range'] * 1.60934))

The result looks like this slack_incoming.png

afterwards

In order to get the status and send the notification periodically, I will deploy it to GAE and trigger it with cron, or add Wake-Up processing so that it can be obtained even if the vehicle is Sleep.

Recommended Posts

I tried to touch Tesla's API
I tried to touch the COTOHA API
I tried to touch jupyter
I tried to touch the API of ebay
I tried to touch Python (installation)
I tried to make a Web API
I tried to touch Python (basic syntax)
I tried to debug.
I tried to paste
I tried to touch Python's GUI library "PySimple GUI"
I tried to learn PredNet
I tried to organize SVM.
I tried to reintroduce Linux
I tried to introduce Pylint
I tried to summarize SparseMatrix
I tried to implement StarGAN (1)
I tried to touch the CSV file with Python
I tried to uncover our darkness with Chatwork API
I tried to implement Deep VQE
[First COTOHA API] I tried to summarize the old story
I tried to create API list.csv in Python from swagger.yaml
I tried the Naro novel API 2
I tried to implement adversarial validation
I tried to explain Pytorch dataset
I tried Watson Speech to Text
I tried to implement hierarchical clustering
I tried to organize about MCMC.
I tried the Naruro novel API
I tried to implement Realness GAN
I tried to get various information from the codeforces API
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
I tried to analyze my favorite singer (SHISHAMO) using Spotify API
[Python] I tried to get various information using YouTube Data API!
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried using Azure Speech to Text.
I tried using Twitter api and Line api
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to implement permutation in Python
I tried to create a linebot (preparation)
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried to get started with Hy
I tried using YOUTUBE Data API V3
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to classify text using TensorFlow
I tried using UnityCloudBuild API from Python
I tried to summarize the graphical modeling.
I tried adding post-increment to CPython Implementation
I tried to let optuna solve Sudoku
I tried to estimate the pi stochastically
I tried to implement PPO in Python
I tried to implement CVAE with PyTorch
I tried to solve TSP with QAOA
[Python] I tried to calculate TF-IDF steadily
API explanation to touch mastodon from python