A Python module to use the Tesla Motors Owner API for monitoring and controlling Tesla vehicles, Powerwall batteries, and solar panels remotely
npx @tessl/cli install tessl/pypi-teslapy@2.9.0A Python implementation based on unofficial documentation of the client side interface to the Tesla Motors Owner API, which provides functionality to monitor and control Tesla products remotely including vehicles, Powerwall batteries, and solar panels.
pip install teslapy 'urllib3<2'import teslapyimport teslapy
# Create Tesla session with your email
with teslapy.Tesla('elon@tesla.com') as tesla:
# Get list of vehicles
vehicles = tesla.vehicle_list()
# Wake up the first vehicle
vehicles[0].sync_wake_up()
# Send a command (open front trunk)
vehicles[0].command('ACTUATE_TRUNK', which_trunk='front')
# Get vehicle data
vehicles[0].get_vehicle_data()
print(vehicles[0]['vehicle_state']['car_version'])TeslaPy follows a hierarchical object model that mirrors Tesla's product ecosystem:
The library implements Tesla's OAuth 2 Single Sign-On service with automatic token refresh, provides both synchronous API calls and streaming capabilities for real-time data, and includes comprehensive error handling with custom exception classes.
Core authentication functionality using Tesla's OAuth 2 SSO service with support for multiple authentication methods, token caching, and automatic refresh capabilities.
class Tesla(OAuth2Session):
def __init__(self, email, verify=True, proxy=None, retry=0, timeout=10,
user_agent=None, authenticator=None, cache_file='cache.json',
cache_loader=None, cache_dumper=None, sso_base_url=None,
code_verifier=None, app_user_agent=None, **kwargs): ...
def fetch_token(self, token_url='oauth2/v3/token', **kwargs): ...
def refresh_token(self, token_url='oauth2/v3/token', **kwargs): ...
def logout(self, sign_out=False): ...Comprehensive vehicle control functionality including wake-up, status monitoring, climate control, charging management, location tracking, and command execution.
class Vehicle(JsonDict):
def sync_wake_up(self, timeout=60, interval=2, backoff=1.15): ...
def get_vehicle_data(self, endpoints='location_data;charge_state;climate_state;vehicle_state;gui_settings;vehicle_config'): ...
def command(self, name, **kwargs): ...
def stream(self, callback=None, retry=0, indefinitely=False, **kwargs): ...Management functionality for Tesla energy products including Powerwall batteries and solar panel installations with status monitoring, configuration, and control capabilities.
class Battery(Product):
def set_operation(self, mode): ...
def set_backup_reserve_percent(self, percent): ...
def get_tariff(self): ...
def set_tariff(self, tariff_data): ...
class SolarPanel(Product):
def get_site_data(self): ...Utility functions for data processing, unit conversion, VIN decoding, option code lookup, and error handling with custom exception classes.
class JsonDict(dict):
def __str__(self): ...
class VehicleError(Exception): ...
class ProductError(Exception): ...
def decode_vin(self): ...
def decode_option(cls, code): ...class Tesla(OAuth2Session):
"""Main session manager for Tesla Motors Owner API"""
class Vehicle(JsonDict):
"""Vehicle class with dictionary access and API request support"""
class Product(JsonDict):
"""Base product class for energy products"""
class Battery(Product):
"""Powerwall battery class"""
class SolarPanel(Product):
"""Solar panel class"""
class JsonDict(dict):
"""Pretty printing dictionary with JSON string representation"""
class VehicleError(Exception):
"""Vehicle-specific exception class"""
class ProductError(Exception):
"""Product-specific exception class"""