基於Tesla Motors所有者API的客戶端接口的非官方文檔的Python實現,該文檔提供了遠程監視和控制Tesla產品的功能。
所有者API將停止使用Tesla車輛命令協議的車輛開始需要端到端命令身份驗證。 2021年以前的S型和X車輛不支持該新協議,並使用Teslapy可以控制。
該模塊取決於Python請求,requests_oauthlib和websocket-client。使用Urllib3 2.0時,它需要Python 3.10+,該請求隨附2.30.0+,也可以通過安裝urllib3<2
將Urllib3固定在1.26.x。
Tesla
類擴展了requests_oauthlib.OAuth2Session
,該sessess擴展了requests.Session
。因此繼承了可用於執行API調用的方法,例如get()
和post()
。模塊特徵:
TESLAPY 2.0.0+不再實施無頭認證。構造函數有所不同,並採用了這些論點:
爭論 | 描述 |
---|---|
email | SSO身份 |
verify | (可選)驗證SSL證書 |
proxy | (可選)代理服務器的URL |
retry | (可選)連接重試或Retry 實例的數量 |
timeout | (可選)連接/讀取超時 |
user_agent | (可選)用戶代理字符串 |
authenticator | (可選的)函數使用一個參數,即授權URL返回重定向的URL |
cache_file | (可選的)默認加載程序和Dumper使用的緩存文件的路徑 |
cache_loader | (可選)返回緩存dict的功能 |
cache_dumper | (可選)一個參數,緩存dict的函數 |
sso_base_url | (可選) https://auth.tesla.cn/ 服務的URL,如果您的電子郵件在另一個區域註冊 |
state | (可選)CSRF保護狀態字符串 |
code_verifier | (可選)PKCE代碼驗證者字符串 |
app_user_agent | (可選)X-Tesla-user-Agent String |
TESLAPY 2.1.0+不再實現RFC 7523,並且使用SSO令牌來滿足所有API請求。
該類將在系統的默認Web瀏覽器中打開Tesla的SSO頁面以進行身份驗證。成功身份驗證後,將顯示一個未找到的頁面,URL應從https://auth.tesla.com/void/callback
開始,這是重定向的URL。該類將使用stdio
默認情況下從Web瀏覽器中獲取完整的重定向URL。您需要將完整的URL從Web瀏覽器複製並粘貼到控制台,以繼續播放API令牌。您可以使用可插入的身份驗證方法來自動化此功能,例如使用Pywebview或Selenium。也可以使用第三方身份驗證應用程序獲得的SSO刷新令牌。
便利方法api()
使用端點中列出的命名端點。 api()
替換URI中的路徑變量,並在需要時調用fetch_token()
。 API返回的任何錯誤消息都作為HTTPError
異常提出。此外,該類實現以下方法:
稱呼 | 描述 |
---|---|
request() | 使用相對或絕對URL,序列化和錯誤消息處理執行API調用 |
new_code_verifier() | 生成PKCE的代碼驗證程序 |
authorization_url() | 帶有PKCE擴展名的表單授權URL,並試圖檢測註冊的帳戶 |
fetch_token() | 使用PKCE擴展名請求使用授權代碼授予的SSO令牌 |
refresh_token() | 請求使用刷新令牌贈款的SSO令牌 |
close() | 刪除所有請求適配器實例 |
logout() | 從緩存中刪除令牌,返回註銷URL並使用System的默認Web瀏覽器登錄 |
vehicle_list() | 返回車輛對象列表 |
battery_list() | 返回電池對象列表 |
solar_list() | 返回Solarpanel對象的列表 |
Vehicle
類擴展了dict
,並存儲由所有者API返回的車輛數據,該數據是拉動API。 get_vehicle_summary()
和get_vehicle_data()
調用更新Vehicle
實例,合併數據。流媒體API在訂閱後將車輛數據推動。 stream()
方法採用可選參數,一個帶有一個參數調用的回調函數,一個持有更改的數據的dict。 Vehicle
對象始終使用推送數據更新。如果在10秒內沒有更改,則車輛停止流媒體數據。 stream()
方法還有兩個可選的參數可以控制重新啟動。此外,該類實現以下方法:
稱呼 | 在線的 | 描述 |
---|---|---|
api() | 是的 | 執行對命名端點的API調用,需要使用可選參數 |
get_vehicle_summary() | 不 | 獲取車輛狀態(在線,入睡,離線) |
available() | 不 | 檢查車輛在衰老時是否基於緩存的數據或刷新狀態進行檢查 |
sync_wake_up() | 不 | 醒來,等待車輛上網 |
decode_option() | 不 | 查找選項代碼描述(從option_codes.json讀取) |
option_code_list() 1 | 不 | 列出了車輛選項代碼的已知描述 |
get_vehicle_data() | 是的 | 獲取選定端點的車輛數據,默認為所有端點 |
get_vehicle_location_data() | 是的 | 獲取車輛的基本和位置數據 |
get_nearby_charging_sites() | 是的 | 列表特斯拉經營的充電站附近 |
get_service_scheduling_data() | 不 | 檢索該車輛的下一個服務約會 |
get_charge_history() 2 | 不 | 列出車輛充電歷史數據點 |
mobile_enabled() | 是的 | 檢查在汽車中是否啟用了移動訪問設置 |
compose_image() 3 | 不 | 根據車輛期權代碼組成車輛圖像 |
dist_units() | 不 | 將距離或速度單元轉換為車輛的GUI設置 |
temp_units() | 不 | 將溫度單位轉換為車輛的GUI設置 |
gui_time() | 不 | 返回時間戳或格式為GUI設置的當前時間 |
last_seen() | 不 | 返回車輛最後一次自然時間 |
decode_vin() | 不 | 將車輛識別號解碼為dict |
command() | 是的 | 圍繞api() 包裝器,用於車輛命令響應錯誤處理 |
1個選項代碼似乎被棄用。
2 CAR軟件版本2021.44.25或更高要求,必須啟用數據共享,並且必須是主要車主。
3將車輛選項代碼傳遞給此方法現在,將選項代碼棄用。
當車輛入睡或離線時,只有在線列中沒有沒有的方法。這些方法不會阻止您的車輛睡覺。其他方法和API呼叫要求使用sync_wake_up()
在線上進行車輛,並且如果在太短的一段時間內被調用,則可以防止車輛睡覺。
Product
類擴展了dict
,並使用API返回的PowerWalls和太陽能電池板的產品數據初始化。此外,該類實現以下方法:
稱呼 | 描述 |
---|---|
api() | 執行對需要電池_ID或SITE_ID的命名端點的API調用,並具有可選參數 |
get_history_data() | 檢索產品的現場狀態 |
get_calendar_history_data() | 檢索產品的現場狀態 |
command() | 電池命令響應錯誤處理圍繞api() 包裝器 |
Battery
類擴展Product
,並存儲由API返回的PowerWall數據,由get_battery_data()
更新。此外,該類實現以下方法:
稱呼 | 描述 |
---|---|
get_battery_data() | 檢索電池的詳細狀態和配置 |
set_operation() | 將電池操作設置為self_cosmapting,備份或自動 |
set_backup_reserve_percent() | 設置該電池的最低備份儲備百分比 |
set_import_export() | 設置電池電網導入和導出設置 |
get_tariff() | 獲取關稅率數據 |
set_tariff() | 設置關稅數據。數據可以手動創建或由Create_tariff生成 |
create_tariff() | 創建正確格式的關稅數據字典 |
SolarPanel
類擴展Product
,並存儲由API返回的太陽能電池板數據,由get_site_data()
更新。此外,該類實現以下方法:
稱呼 | 描述 |
---|---|
get_site_data() | 檢索當前的現場生成數據 |
模塊的基本用法:
import teslapy
with teslapy . Tesla ( '[email protected]' ) as tesla :
vehicles = tesla . vehicle_list ()
vehicles [ 0 ]. sync_wake_up ()
vehicles [ 0 ]. command ( 'ACTUATE_TRUNK' , which_trunk = 'front' )
vehicles [ 0 ]. get_vehicle_data ()
print ( vehicles [ 0 ][ 'vehicle_state' ][ 'car_version' ])
Teslapy 2.4.0和2.5.0自動調用get_vehicle_data()
和Teslapy 2.6.0+在找不到鍵時自動調用get_latest_vehicle_data()
。此示例適用於Teslapy 2.6.0+:
import teslapy
with teslapy . Tesla ( '[email protected]' ) as tesla :
vehicles = tesla . vehicle_list ()
print ( vehicles [ 0 ][ 'display_name' ] + ' last seen ' + vehicles [ 0 ]. last_seen () +
' at ' + str ( vehicles [ 0 ][ 'charge_state' ][ 'battery_level' ]) + '% SoC' )
示例輸出:
Tim's Tesla last seen 6 hours ago at 87% SoC
Tesla
類實施了一種可插入的身份驗證方法。如果要實現自己的方法來處理SSO頁面並在身份驗證後檢索重定向的URL,則可以將函數作為參數傳遞給構造函數,該函數將身份驗證URL作為參數並返回重定向的URL。 authenticator
參數也可以作為屬性訪問。
示例使用WebView組件在其自己的本機GUI窗口中顯示SSO頁面。
import teslapy
import webview
def custom_auth ( url ):
result = [ '' ]
window = webview . create_window ( 'Login' , url )
def on_loaded ():
result [ 0 ] = window . get_current_url ()
if 'void/callback' in result [ 0 ]. split ( '?' )[ 0 ]:
window . destroy ()
window . loaded += on_loaded
webview . start ()
return result [ 0 ]
with teslapy . Tesla ( '[email protected]' , authenticator = custom_auth ) as tesla :
tesla . fetch_token ()
示例使用硒來自動化Web瀏覽器交互。設置navigator.webdriver
時,SSO頁面將返回403,目前只有Chrome,Opera和Edge Chromium可以防止這種情況。
import teslapy
from selenium import webdriver
from selenium . webdriver . support import expected_conditions as EC
from selenium . webdriver . support . ui import WebDriverWait
def custom_auth ( url ):
options = webdriver . ChromeOptions ()
options . add_argument ( "--disable-blink-features=AutomationControlled" )
with webdriver . Chrome ( chrome_options = options ) as browser :
browser . get ( url )
WebDriverWait ( browser , 300 ). until ( EC . url_contains ( 'void/callback' ))
return browser . current_url
with teslapy . Tesla ( '[email protected]' , authenticator = custom_auth ) as tesla :
tesla . fetch_token ()
Teslapy 2.2.0引入了authorization_url()
方法,以獲取SSO頁面URL,並在身份驗證後將重定向URL作為關鍵字參數authorization_response
提供給fetch_token()
的選項。
import teslapy
tesla = teslapy . Tesla ( '[email protected]' )
if not tesla . authorized :
print ( 'Use browser to login. Page Not Found will be shown at success.' )
print ( 'Open this URL: ' + tesla . authorization_url ())
tesla . fetch_token ( authorization_response = input ( 'Enter URL after authentication: ' ))
vehicles = tesla . vehicle_list ()
print ( vehicles [ 0 ])
tesla . close ()
對分階段授權的支持已添加到Teslapy 2.5.0中。 Tesla
類構造函數, authorization_url()
方法和fetch_token()
方法接受了關鍵字參數state
和code_verifier
。
import teslapy
# First stage
tesla = teslapy . Tesla ( '[email protected]' )
if not tesla . authorized :
state = tesla . new_state ()
code_verifier = tesla . new_code_verifier ()
print ( 'Use browser to login. Page Not Found will be shown at success.' )
print ( 'Open: ' + tesla . authorization_url ( state = state , code_verifier = code_verifier ))
tesla . close ()
# Second stage
tesla = teslapy . Tesla ( '[email protected]' , state = state , code_verifier = code_verifier )
if not tesla . authorized :
tesla . fetch_token ( authorization_response = input ( 'Enter URL after authentication: ' ))
vehicles = tesla . vehicle_list ()
print ( vehicles [ 0 ])
tesla . close ()
TESLAPY 2.4.0+支持使用第三方身份驗證應用程序獲得的刷新令牌。刷新令牌用於獲得訪問令牌,並且兩者都經過持久性,因此您只需要僅提供一次刷新令牌即可。
import teslapy
with teslapy . Tesla ( '[email protected]' ) as tesla :
if not tesla . authorized :
tesla . refresh_token ( refresh_token = input ( 'Enter SSO refresh token: ' ))
vehicles = tesla . vehicle_list ()
print ( vehicles [ 0 ])
要使用系統的默認Web瀏覽器登錄SSO頁面並清除CACHE的令牌:
tesla . logout ( sign_out = True )
如果使用pywebview,則可以從緩存中清除令牌,並獲取註銷URL以顯示一個簽名窗口:
window = webview . create_window ( 'Logout' , tesla . logout ())
window . start ()
硒不存儲cookie,只需清除cache的令牌:
tesla . logout ()
Tesla
類實現了可插的緩存方法。如果您不想使用默認磁盤緩存,則可以傳遞函數以加載和返回緩存dict,以及將dict作為參數丟棄緩存dict的函數,作為對構造函數的參數。 cache_loader
和cache_dumper
參數也可以作為屬性訪問。
import json
import sqlite3
import teslapy
def db_load ():
con = sqlite3 . connect ( 'cache.db' )
cur = con . cursor ()
cache = {}
try :
for row in cur . execute ( 'select * from teslapy' ):
cache [ row [ 0 ]] = json . loads ( row [ 1 ])
except sqlite3 . OperationalError :
pass
con . close ()
return cache
def db_dump ( cache ):
con = sqlite3 . connect ( 'cache.db' )
con . execute ( 'create table if not exists teslapy (email text primary key, data json)' )
for email , data in cache . items ():
con . execute ( 'replace into teslapy values (?, ?)' , [ email , json . dumps ( data )])
con . commit ()
con . close ()
with teslapy . Tesla ( '[email protected]' , cache_loader = db_load , cache_dumper = db_dump ) as tesla :
tesla . fetch_token ()
安全得分是通過另一個API獲得的。 Teslapy 2.1.0引入了絕對URL支持,以訪問非所有者API端點。
from teslapy import Tesla
with Tesla ( '[email protected]' ) as tesla :
vehicles = tesla . vehicle_list ()
url = 'https://akamai-apigateway-vfx.tesla.com/safety-rating/daily-metrics'
print ( tesla . get ( url , params = { 'vin' : vehicles [ 0 ][ 'vin' ], 'deviceLanguage' : 'en' ,
'deviceCountry' : 'US' , 'timezone' : 'UTC' }))
TESLAPY默認情況下使用可調節的連接和讀取10秒的超時。有關更多詳細信息,請參閱超時部分。默認情況下,Teslapy不會重試連接失敗或定時連接,可以通過retry
參數啟用。
tesla = teslapy . Tesla ( '[email protected]' , retry = 2 , timeout = 15 )
對網絡失敗的強大程序會計,具有重試策略,其中包括重試嘗試的總數,HTTP響應代碼重試的代碼以及可選的退縮因素。有關更多詳細信息,請參閱重試模塊。
請注意,如果您過多地敲擊服務器,則特斯拉可能會暫時阻止您的帳戶。
import teslapy
retry = teslapy . Retry ( total = 2 , status_forcelist = ( 500 , 502 , 503 , 504 ))
with teslapy . Tesla ( '[email protected]' , retry = retry ) as tesla :
vehicles = tesla . vehicle_list ()
print ( vehicles [ 0 ])
vehicles [ 0 ]. command ( 'FLASH_LIGHTS' ) # Raises exception
上下文管理器使用Teslapy 2.5.0+時,上下文管理器之後的行將引起異常,因為它在上下文管理器退出時明確關閉了請求連接處理程序。較早的版本不會引起例外,並且檢索將不起作用。
查看cli.py,menu.py或gui.py的更多代碼示例。
這些是主要命令:
端點 | 參數 | 價值 |
---|---|---|
開鎖 | ||
鎖 | ||
Honk_horn | ||
flash_lights | ||
climate_on | ||
climate_off | ||
max_defrost | on | true 還是false |
change_climate_temperature_setting | driver_temp , passenger_temp | Celcius的溫度 |
set_climate_keeper_mode | climate_keeper_mode | 0 = off,1 = on,2 =狗,3 =營地 |
HVAC_BIOWEAPON_MODE | on | true 還是false |
計劃_departure 1 | enable , departure_time , preconditioning_enabled , preconditioning_weekdays_only , off_peak_charging_enabled , off_peak_charging_weekdays_only , end_off_peak_time | true ,或false ,午夜後幾分鐘 |
計劃_Charging 1 | enable , time | true ,或false ,午夜後幾分鐘 |
charging_amp 1 | charging_amps | 在0-32之間 |
set_cabin_overheat_protection | on , fan_only | true 還是false |
change_charge_limit | percent | 百分比 |
set_vehicle_name | vehicle_name | 姓名 |
change_sunroof_state | state | vent 或close |
window_control 2 | command , lat , lon | vent 或close 0 0 |
Actuate_trunk | which_trunk | rear 或front |
遠程_start | ||
trigger_homelink | lat , lon | 當前的頻率和邏輯 |
chard_port_door_open | ||
chard_port_door_close | ||
start_charge | ||
stop_charge | ||
set_cop_temp | temp | Celcius的溫度 |
Media_toggle_playback | ||
Media_next_track | ||
Media_previous_track | ||
Media_next_favorite | ||
Media_previous_favorite | ||
Media_volume_up | ||
Media_volume_down | ||
set_valet_mode | on , password | true 或false ,4位數別針 |
reset_valet_pin | ||
speed_limit_activate | pin | 4位數銷 |
speed_limit_deactivate | pin | 4位數銷 |
speed_limit_set_limit | limit_mph | 在50-90之間 |
speed_limit_clear_pin | pin | 4位數銷 |
Schedue_software_update | offset_sec | 秒 |
cancel_software_update | ||
set_sentry_mode | on | true 還是false |
遠程_seat_heater_request | heater , level | 座位0-5,級別0-3 |
遠程_auto_seat_climate_request | auto_seat_position , auto_climate_on | 1-2, true 還是false |
遠程_seat_cooling_request | seat_position , seat_cooler_level | |
遠程_STEERIN_WHEEL_HEATER_REQUEST | on | true 還是false |
1需要汽車版本2021.36或更高。將charging_amps
設置為2或3個結果,結果為3A,並設置為0或1的結果2a。
2 close
需要lat
和lon
值接近汽車的當前位置。
基本異常處理:
try :
vehicles [ 0 ]. command ( 'HONK_HORN' )
except teslapy . HTTPError as e :
print ( e )
所有requests.exceptions
oauthlib.oauth2.rfc6749.errors
當車輛入睡或離線時,要執行API端點時,車輛需要在線上進行在線時,請提高以下例外: requests.exceptions.HTTPError: 408 Client Error: vehicle unavailable
。例外可以被視為teslapy.HTTPError
。
此外, sync_wake_up()
提出了teslapy.VehicleError
,當車輛在指定的超時內未在線時。和command()
也會False
teslapy.VehicleError
。例如,如果調用了媒體端點之一,並且車輛中沒有用戶,則提出以下例外: VehicleError: user_not_present
。
截至2021年1月29日,特斯拉更新了此端點以遵循RFC 7523,並需要使用SSO服務(auth.tesla.com)進行身份驗證。如果您獲得了requests.exceptions.HTTPError: 400 Client Error: endpoint_deprecated:_please_update_your_app for url: https://owner-api.teslamotors.com/oauth/token
。
截至2021年9月3日,特斯拉已向登錄表添加了recaptcha。這導致Teslapy實施的無頭登錄破裂。如果您得到一個ValueError: Credentials rejected. Recaptcha is required
,並且您使用的是正確的憑據,然後您可能正在使用此模塊的舊版本。
截至2022年1月12日,特斯拉已經棄用了RFC 7523令牌的使用,並要求SSO令牌用於API訪問。如果您收到requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://owner-api.teslamotors.com/api/1/vehicles
,您使用的是正確的憑據,那麼您可能正在使用舊版本的舊版本這個模塊。
截至2024年1月7日,特斯拉已刪除了車輛列表端點。如果您獲得了requests.exceptions.HTTPError: 412 Client Error: Endpoint is only available on fleetapi. Visit https://developer.tesla.com/docs for more info
然後您可能正在使用此模塊的舊版本。
源存儲庫包含三個演示應用程序,可選地使用Pywebview版本3.0或更高版本或硒版3.13.0或更高版本以自動化Weblogin。邊緣鉻所需的硒4.0.0或更高。
CLI.PY是一個簡單的CLI應用程序,可以使用Teslapy模塊的幾乎所有功能。過濾器選項允許您選擇一個產品,如果多個產品鏈接到您的帳戶。 API輸出是JSON格式:
usage: cli.py [-h] -e EMAIL [-f FILTER] [-a API [KEYVALUE ...]] [-k KEYVALUE]
[-c COMMAND] [-t TIMEOUT] [-p PROXY] [-R REFRESH] [-U URL] [-l]
[-o] [-v] [-w] [-g] [-b] [-n] [-m] [-s] [-d] [-r] [-S] [-H] [-V]
[-L] [-u] [--chrome] [--edge]
Tesla Owner API CLI
optional arguments:
-h, --help show this help message and exit
-e EMAIL login email
-f FILTER filter on id, vin, etc.
-a API [KEYVALUE ...]
API call endpoint name
-k KEYVALUE API parameter (key=value)
-c COMMAND product command endpoint
-t TIMEOUT connect/read timeout
-p PROXY proxy server URL
-R REFRESH use this refresh token
-U URL SSO service base URL
-l, --list list all selected vehicles/batteries
-o, --option list vehicle option codes
-v, --vin vehicle identification number decode
-w, --wake wake up selected vehicle(s)
-g, --get get rollup of all vehicle data
-b, --battery get detailed battery state and config
-n, --nearby list nearby charging sites
-m, --mobile get mobile enabled state
-s, --site get current site generation data
-d, --debug set logging level to debug
-r, --stream receive streaming vehicle data on-change
-S, --service get service self scheduling eligibility
-H, --history get charging history data
-B, --basic get basic vehicle data only
-G, --location get location (GPS) data, wake as needed
-V, --verify disable verify SSL certificate
-L, --logout clear token from cache and logout
-u, --user get user account details
--chrome use Chrome WebDriver
--edge use Edge WebDriver
cli.py的用法:
python cli.py -e [email protected] -w -a ACTUATE_TRUNK -k which_trunk=front
菜單。Py是一個基於菜單的控制台應用程序,該應用程序以表格格式顯示車輛數據。該應用程序取決於將GPS坐標轉換為人類可讀地址的地址:
GUI.PY是使用tkinter
圖形用戶界面。 API調用使用線程異步執行。 GUI支持自動刷新車輛數據並顯示組合的車輛圖像。請注意,如果啟用了自動刷新,車輛將不會入睡。該應用程序取決於地理的將GPS坐標轉換為人類可讀地址。如果您的Python安裝的TCL/TK GUI工具包低於8.6,則需要枕頭才能顯示車輛圖像。用戶首選項(例如要用於身份驗證的哪個Web瀏覽器)在應用程序重新啟動時會持續存在。
車輛充電歷史記錄也可以顯示在圖中。
演示應用程序可以使用提供的Dockerfile進行集裝。綁定量用於將CACH.JSON和GUI.INI存儲在主機機器上的當前目錄中:
sudo docker build -t teslapy .
xhost +local:*
sudo docker run -ti --net=host --privileged -v "$(pwd)":/home/tsla teslapy
get_vehicle_data()
或python cli.py -e [email protected] -w -g
下面的示例輸出:
{
"id" : 12345678901234567 ,
"vehicle_id" : 1234567890 ,
"vin" : " 5YJ3E111111111111 " ,
"display_name" : " Tim's Tesla " ,
"option_codes" : null ,
"color" : null ,
"tokens" : [
" 1234567890abcdef " ,
" abcdef1234567890 "
],
"state" : " online " ,
"in_service" : false ,
"id_s" : " 12345678901234567 " ,
"calendar_enabled" : true ,
"api_version" : 6 ,
"backseat_token" : null ,
"backseat_token_updated_at" : null ,
"user_id" : 123456 ,
"charge_state" : {
"battery_heater_on" : false ,
"battery_level" : 44 ,
"battery_range" : 99.84 ,
"charge_current_request" : 16 ,
"charge_current_request_max" : 16 ,
"charge_enable_request" : true ,
"charge_energy_added" : 14.54 ,
"charge_limit_soc" : 90 ,
"charge_limit_soc_max" : 100 ,
"charge_limit_soc_min" : 50 ,
"charge_limit_soc_std" : 90 ,
"charge_miles_added_ideal" : 66.5 ,
"charge_miles_added_rated" : 66.5 ,
"charge_port_cold_weather_mode" : false ,
"charge_port_door_open" : true ,
"charge_port_latch" : " Engaged " ,
"charge_rate" : 455.7 ,
"charge_to_max_range" : false ,
"charger_actual_current" : 0 ,
"charger_phases" : null ,
"charger_pilot_current" : 16 ,
"charger_power" : 100 ,
"charger_voltage" : 2 ,
"charging_state" : " Charging " ,
"conn_charge_cable" : " IEC " ,
"est_battery_range" : 78.13 ,
"fast_charger_brand" : " Tesla " ,
"fast_charger_present" : true ,
"fast_charger_type" : " Combo " ,
"ideal_battery_range" : 99.84 ,
"managed_charging_active" : false ,
"managed_charging_start_time" : null ,
"managed_charging_user_canceled" : false ,
"max_range_charge_counter" : 1 ,
"minutes_to_full_charge" : 15 ,
"not_enough_power_to_heat" : null ,
"scheduled_charging_pending" : false ,
"scheduled_charging_start_time" : null ,
"time_to_full_charge" : 0.25 ,
"timestamp" : 1569952097456 ,
"trip_charging" : true ,
"usable_battery_level" : 44 ,
"user_charge_enable_request" : null
},
"climate_state" : {
"battery_heater" : false ,
"battery_heater_no_power" : null ,
"climate_keeper_mode" : " off " ,
"driver_temp_setting" : 21.0 ,
"fan_status" : 3 ,
"inside_temp" : 21.0 ,
"is_auto_conditioning_on" : true ,
"is_climate_on" : true ,
"is_front_defroster_on" : false ,
"is_preconditioning" : false ,
"is_rear_defroster_on" : false ,
"left_temp_direction" : 54 ,
"max_avail_temp" : 28.0 ,
"min_avail_temp" : 15.0 ,
"outside_temp" : 13.5 ,
"passenger_temp_setting" : 21.0 ,
"remote_heater_control_enabled" : true ,
"right_temp_direction" : 54 ,
"seat_heater_left" : 0 ,
"seat_heater_right" : 0 ,
"side_mirror_heaters" : false ,
"smart_preconditioning" : false ,
"timestamp" : 1569952097456 ,
"wiper_blade_heater" : false
},
"drive_state" : {
"gps_as_of" : 1569952096 ,
"heading" : 240 ,
"latitude" : 52.531951 ,
"longitude" : 6.156999 ,
"native_latitude" : 52.531951 ,
"native_location_supported" : 1 ,
"native_longitude" : 6.156999 ,
"native_type" : " wgs " ,
"power" : -100 ,
"shift_state" : null ,
"speed" : null ,
"timestamp" : 1569952097456
},
"gui_settings" : {
"gui_24_hour_time" : true ,
"gui_charge_rate_units" : " km/hr " ,
"gui_distance_units" : " km/hr " ,
"gui_range_display" : " Rated " ,
"gui_temperature_units" : " C " ,
"show_range_units" : false ,
"timestamp" : 1569952097456
},
"vehicle_config" : {
"can_accept_navigation_requests" : true ,
"can_actuate_trunks" : true ,
"car_special_type" : " base " ,
"car_type" : " model3 " ,
"charge_port_type" : " CCS " ,
"eu_vehicle" : true ,
"exterior_color" : " SolidBlack " ,
"has_air_suspension" : false ,
"has_ludicrous_mode" : false ,
"key_version" : 2 ,
"motorized_charge_port" : true ,
"plg" : false ,
"rear_seat_heaters" : 0 ,
"rear_seat_type" : null ,
"rhd" : false ,
"roof_color" : " Glass " ,
"seat_type" : null ,
"spoiler_type" : " None " ,
"sun_roof_installed" : null ,
"third_row_seats" : " <invalid> " ,
"timestamp" : 1569952097456 ,
"use_range_badging" : true ,
"wheel_type" : " Pinwheel18 "
},
"vehicle_state" : {
"api_version" : 6 ,
"autopark_state_v2" : " unavailable " ,
"calendar_supported" : true ,
"car_version" : " 2019.32.11.1 d39e85a " ,
"center_display_state" : 2 ,
"df" : 0 ,
"dr" : 0 ,
"fd_window" : 0 ,
"fp_window" : 0 ,
"ft" : 0 ,
"is_user_present" : true ,
"locked" : false ,
"media_state" : {
"remote_control_enabled" : true
},
"notifications_supported" : true ,
"odometer" : 6963.081561 ,
"parsed_calendar_supported" : true ,
"pf" : 0 ,
"pr" : 0 ,
"rd_window" : 0 ,
"remote_start" : false ,
"remote_start_enabled" : true ,
"remote_start_supported" : true ,
"rp_window" : 0 ,
"rt" : 0 ,
"sentry_mode" : false ,
"sentry_mode_available" : true ,
"software_update" : {
"expected_duration_sec" : 2700 ,
"status" : " "
},
"speed_limit_mode" : {
"active" : false ,
"current_limit_mph" : 85.0 ,
"max_limit_mph" : 90 ,
"min_limit_mph" : 50 ,
"pin_code_set" : false
},
"sun_roof_percent_open" : null ,
"sun_roof_state" : " unknown " ,
"timestamp" : 1569952097456 ,
"valet_mode" : false ,
"valet_pin_needed" : true ,
"vehicle_name" : " Tim's Tesla "
}
}
get_service_scheduling_data()
或python cli.py -e [email protected] --service
的示例輸出
{
"vin" : " 5YJ3E111111111111 " ,
"next_appt_timestamp" : " 2021-06-08T13:15:00 " ,
"next_appt_end_timestamp" : null ,
"show_badge" : false
}
示例輸出get_battery_data()
或python cli.py -e [email protected] -b
下面:
{
"energy_site_id" : 111110110110 ,
"resource_type" : " battery " ,
"site_name" : " Elon's House " ,
"id" : " STE10110111-00101 " ,
"gateway_id" : " 1111100-11-A--AAA11110A1A111 " ,
"asset_site_id" : " a1100111-1a11-1aaa-a111-1a0011aa1111 " ,
"energy_left" : 0 ,
"total_pack_energy" : 13746 ,
"percentage_charged" : 0 ,
"battery_type" : " ac_powerwall " ,
"backup_capable" : true ,
"battery_power" : 0 ,
"sync_grid_alert_enabled" : false ,
"breaker_alert_enabled" : false ,
"components" : {
"solar" : true ,
"solar_type" : " pv_panel " ,
"battery" : true ,
"grid" : true ,
"backup" : true ,
"gateway" : " teg " ,
"load_meter" : true ,
"tou_capable" : true ,
"storm_mode_capable" : true ,
"flex_energy_request_capable" : false ,
"car_charging_data_supported" : false ,
"off_grid_vehicle_charging_reserve_supported" : false ,
"vehicle_charging_performance_view_enabled" : false ,
"vehicle_charging_solar_offset_view_enabled" : false ,
"battery_solar_offset_view_enabled" : true ,
"show_grid_import_battery_source_cards" : true ,
"battery_type" : " ac_powerwall " ,
"configurable" : false ,
"grid_services_enabled" : false
},
"grid_status" : " Active " ,
"backup" : {
"backup_reserve_percent" : 0 ,
"events" : null
},
"user_settings" : {
"storm_mode_enabled" : false ,
"sync_grid_alert_enabled" : false ,
"breaker_alert_enabled" : false
},
"default_real_mode" : " self_consumption " ,
"operation" : " self_consumption " ,
"installation_date" : " 2020-01-01T10:10:00+08:00 " ,
"power_reading" : [
{
"timestamp" : " 2021-02-24T04:25:39+08:00 " ,
"load_power" : 5275 ,
"solar_power" : 3 ,
"grid_power" : 5262 ,
"battery_power" : 10 ,
"generator_power" : 0
}
],
"battery_count" : 1
}
示例輸出get_site_data()
或python cli.py -e [email protected] -s
下面:
{
"energy_site_id" : 111110110110 ,
"resource_type" : " solar " ,
"id" : " 111aaa00-111a-11a1-00aa-00a1aa1aa0aa " ,
"asset_site_id" : " a1100111-1a11-1aaa-a111-1a0011aa1111 " ,
"solar_power" : 11892.01953125 ,
"solar_type" : " pv_panel " ,
"storm_mode_enabled" : null ,
"powerwall_onboarding_settings_set" : null ,
"sync_grid_alert_enabled" : false ,
"breaker_alert_enabled" : false ,
"components" : {
"battery" : false ,
"solar" : true ,
"solar_type" : " pv_panel " ,
"grid" : true ,
"load_meter" : true ,
"market_type" : " residential "
},
"energy_left" : 0 ,
"total_pack_energy" : 1 ,
"percentage_charged" : 0 ,
"battery_power" : 0 ,
"load_power" : 0 ,
"grid_status" : " Unknown " ,
"grid_services_active" : false ,
"grid_power" : -11892.01953125 ,
"grid_services_power" : 0 ,
"generator_power" : 0 ,
"island_status" : " island_status_unknown " ,
"storm_mode_active" : false ,
"timestamp" : " 2022-08-15T17:12:26Z " ,
"wall_connectors" : null
}
TESLAPY在PYPI上可用:
python -m pip install teslapy 'urllib3<2'
確保系統上安裝了Python 2.7+或3.5+。或者,將存儲庫克隆到您的機器上並運行演示應用程序cli.py,菜單。 .0+(可選)和websocket-client 0.59+使用PIP如下:
python -m pip install requests_oauthlib geopy pywebview selenium websocket-client
並安裝Chromedriver以使用硒或在Ubuntu上,如下所示:
sudo apt-get install python3-requests-oauthlib python3-geopy python3-webview python3-selenium python3-websocket