Tesla의 API를 만져 보았습니다.
하고 싶은 일
Tesla의 API가 비공식이면서 사용할 수 있다는 점에서 조금 만져 보았습니다.
차량 정보를 취득할 수 있으므로 우선 Python으로 취득해 Slack에 통지해 본다
 준비
htps : ///ss-Ap. 치 m 드 r. 코 m / 아피 바시 cs / 아우테 펜치 카치온
htps : // 빠스테병. 이 m/pS7Z6~yP
인증 ID
TESLA_CLIENT_ID=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384
TESLA_CLIENT_SECRET=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3
동작환경 등
개요
OAuth2Service configparser 로 취득 스크립트
teslaapi_test.pydef 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을 사용하여 간단합니다.
 htps : // 아피. scck. 코 m / 메사 긴 g / ぇ b 호오 ks # 포스 친 g_ ぃ th_ ぇ b 호오 ks
슬랙 알림import slackweb
slack = slackweb.Slack(url="WebhookのURL")
slack.notify(text="航続距離 : {}[km]".format(resp['response']['battery_range'] * 1.60934))
※ 거리 단위가 [mi]이므로 [Km]로 환산
결과는 이런 느낌
 
 그 후
정기적으로 스테이터스를 취득해 통지를 송신하기 위해, GAE에 배치해 cron에서 트리거하거나 차량이 Sleep의 경우에서도 취득할 수 있도록 Wake-Up 처리를 넣거나 해 봅니다.
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
[development]
#tesla
teslaID = 'ユーザID'
password = 'パスワード'
client_id = '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384'
client_secret = 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
import slackweb
slack = slackweb.Slack(url="WebhookのURL")
slack.notify(text="航続距離 : {}[km]".format(resp['response']['battery_range'] * 1.60934))
정기적으로 스테이터스를 취득해 통지를 송신하기 위해, GAE에 배치해 cron에서 트리거하거나 차량이 Sleep의 경우에서도 취득할 수 있도록 Wake-Up 처리를 넣거나 해 봅니다.
Reference
이 문제에 관하여(Tesla의 API를 만져 보았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/306maxi/items/95178eef8d4af048345a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)