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
--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
--Confirmed with Python 3.7 series (Windows) ――For the time being, I tried to make it work using GAE
--Authentication is ʻOAuth2Service --I don't want to embed ID / PASS, so make it a separate file and get it with
config 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)
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
config.conf
[development]
#tesla
teslaID = 'User ID'
password = 'password'
client_id = '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384'
client_secret = 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
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
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