7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TeslaのAPIを触ってみた

Posted at

やりたいこと

TeslaのAPIが非公式ながら使えるとのことで,早速触ってみた
車両情報を取得できるので,とりあえずPythonで取得してSlackに通知してみる

準備

認証ID
TESLA_CLIENT_ID=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384
TESLA_CLIENT_SECRET=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3

動作環境など

  • Python 3.7系で確認(Windows)
  • 一応,GAEを使って動作もさせてみた

概要

  • 認証はOAuth2Service
  • ID/PASSを埋め込みたくないので別ファイルにしてconfigparserで取得
  • データの構造はrespの中身を見ればだいたい分かる
  • 車両を起こす処理を入れていないので予めアプリ等で起動させておいてからスクリプトを実行(リトライ等も入れていないのでそうしないと失敗して終了)

スクリプト

teslaapi_test.py
def access_vehicle():
    import json
    from rauth import OAuth2Service # 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 # 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 = 'ユーザID'
password = 'パスワード'
client_id = '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384'
client_secret = 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'

Slack通知

通知はIncoming-webhookを使って簡単に.
https://api.slack.com/messaging/webhooks#posting_with_webhooks

slack通知
import slackweb

slack = slackweb.Slack(url="WebhookのURL")
slack.notify(text="航続距離 : {}[km]".format(resp['response']['battery_range'] * 1.60934))

※ 距離単位が[mi]なので[Km]に換算

結果はこんな感じ
slack_incoming.png

その後

定期的にステータスを取得して通知を送信するため,GAEにデプロイしてcronでトリガしたり,車両がSleepの場合でも取得できるようにWake-Up処理を入れたりしてみるつもり.

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?