diff --git a/README.md b/README.md index ad5b0a5..07b5259 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,70 @@ -# Tesla Fleet Api -Python library for Tesla Fleet API and Tesla Command Protocol, including signed commands and encrypted local Bluetooth (BLE). Also provides interfaces for Teslemetry and Tessie. +# Tesla Fleet API -Based on [Tesla Developer documentation](https://developer.tesla.com/docs/fleet-api) and [Tesla Command Protocol](https://github.com/teslamotors/vehicle-command/blob/main/pkg/protocol/protocol.md) +Tesla Fleet API is a Python library that provides an interface to interact with Tesla's Fleet API, including signed commands and encrypted local Bluetooth (BLE) communication. It also supports interactions with Teslemetry and Tessie services. -**Documentation is currently outdated for V1.0.X** +## Features -## TeslaFleetApi -This is the base class, however can also be used directly if you have a valid user access_token. +- Fleet API for vehicles +- Fleet API for energy sites +- Fleet API with signed vehicle commands +- Bluetooth for vehicles +- Teslemetry integration +- Tessie integration +## Installation + +You can install the library using pip: + +```bash +pip install tesla-fleet-api ``` + +## Usage + +### Authentication + +The `TeslaFleetOAuth` class provides methods that help with authenticating to the Tesla Fleet API. Here's a basic example: + +```python import asyncio import aiohttp +from tesla_fleet_api import TeslaFleetOAuth + +async def main(): + async with aiohttp.ClientSession() as session: + oauth = TeslaFleetOAuth( + session=session, + client_id="", + client_secret="", + redirect_uri="", + ) + + # Get the login URL and navigate the user to it + login_url = oauth.get_login_url(scopes=["openid", "email", "offline_access"]) + print(f"Please go to {login_url} and authorize access.") + + # After the user authorizes access, they will be redirected to the redirect_uri with a code + code = input("Enter the code you received: ") + + # Exchange the code for a refresh token + await oauth.get_refresh_token(code) + print(f"Access token: {oauth.access_token}") + print(f"Refresh token: {oauth.refresh_token}") + # Dont forget to store the refresh token so you can use it again later + +asyncio.run(main()) +``` + +### Fleet API for Vehicles +The `TeslaFleetApi` class provides methods to interact with the Fleet API for vehicles. Here's a basic example: + +```python +import asyncio +import aiohttp from tesla_fleet_api import TeslaFleetApi from tesla_fleet_api.exceptions import TeslaFleetError - async def main(): async with aiohttp.ClientSession() as session: api = TeslaFleetApi( @@ -25,7 +74,7 @@ async def main(): ) try: - data = await api.vehicle.list() + data = await api.vehicles.list() print(data) except TeslaFleetError as e: print(e) @@ -33,56 +82,100 @@ async def main(): asyncio.run(main()) ``` -## TeslaFleetOAuth -This extends TeslaFleetApi to support OAuth, and requires a client_id, and either a refresh_token or initial authentication code. +For more detailed examples, see [Fleet API for Vehicles](docs/fleet_api_vehicles.md). -``` -from tesla_fleet_api import TeslaFleetOAuth +### Fleet API for Energy Sites + +The `EnergySites` class provides methods to interact with the Fleet API for energy sites. Here's a basic example: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi from tesla_fleet_api.exceptions import TeslaFleetError -import json async def main(): - with open("auth.json", "r") as f: - auth = json.load(f) async with aiohttp.ClientSession() as session: - api = TeslaFleetOAuth( - session, - client_id=, - access_token=auth["access_token"], - refresh_token=auth["refresh_token"], - expires=auth["expires"], + api = TeslaFleetApi( + access_token="", + session=session, region="na", ) + try: - data = await api.vehicle.list() - print(data) + energy_sites = await api.energySites.list() + print(energy_sites) except TeslaFleetError as e: print(e) - with open("auth.json", "w") as f: - json.dump( - { - "access_token": api.access_token, - "refresh_token": api.refresh_token, - "expires": api.expires, - }, - f, +asyncio.run(main()) +``` + +For more detailed examples, see [Fleet API for Energy Sites](docs/fleet_api_energy_sites.md). + +### Fleet API with Signed Vehicle Commands + +The `VehicleSigned` class provides methods to interact with the Fleet API using signed vehicle commands. Here's a basic example: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", ) + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + data = await vehicle.wake_up() + print(data) + except TeslaFleetError as e: + print(e) + asyncio.run(main()) ``` -## Teslemetry -This extends TeslaFleetApi to send requests through Teslemetry, which manages all aspects of Tesla OAuth. This class only requires an access_token from the Teslemetry console. +For more detailed examples, see [Fleet API with Signed Vehicle Commands](docs/fleet_api_signed_commands.md). +### Bluetooth for Vehicles + +The `TeslaBluetooth` class provides methods to interact with Tesla vehicles using Bluetooth. Here's a basic example: + +```python +import asyncio +from bleak import BleakScanner +from tesla_fleet_api import TeslaBluetooth + +async def main(): + scanner = BleakScanner() + devices = await scanner.discover() + for device in devices: + if TeslaBluetooth().valid_name(device.name): + print(f"Found Tesla vehicle: {device.name}") + +asyncio.run(main()) ``` + +For more detailed examples, see [Bluetooth for Vehicles](docs/bluetooth_vehicles.md). + +### Teslemetry + +The `Teslemetry` class provides methods to interact with the Teslemetry service. Here's a basic example: + +```python import asyncio import aiohttp - from tesla_fleet_api import Teslemetry from tesla_fleet_api.exceptions import TeslaFleetError - async def main(): async with aiohttp.ClientSession() as session: api = Teslemetry( @@ -91,7 +184,7 @@ async def main(): ) try: - data = await api.vehicle.list() + data = await api.vehicles.list() print(data) except TeslaFleetError as e: print(e) @@ -99,17 +192,18 @@ async def main(): asyncio.run(main()) ``` -## Tessie -This extends TeslaFleetApi to send requests through Tessie, which manages all aspects of Tesla OAuth. This class only requires an access_token from [Tessie](https://dash.tessie.com/settings/api). +For more detailed examples, see [Teslemetry](docs/teslemetry.md). -``` +### Tessie + +The `Tessie` class provides methods to interact with the Tessie service. Here's a basic example: + +```python import asyncio import aiohttp - from tesla_fleet_api import Tessie from tesla_fleet_api.exceptions import TeslaFleetError - async def main(): async with aiohttp.ClientSession() as session: api = Tessie( @@ -118,10 +212,12 @@ async def main(): ) try: - data = await api.vehicle.list() + data = await api.vehicles.list() print(data) except TeslaFleetError as e: print(e) asyncio.run(main()) ``` + +For more detailed examples, see [Tessie](docs/tessie.md). diff --git a/docs/bluetooth_vehicles.md b/docs/bluetooth_vehicles.md new file mode 100644 index 0000000..ab84992 --- /dev/null +++ b/docs/bluetooth_vehicles.md @@ -0,0 +1,98 @@ +# Bluetooth for Vehicles + +This document provides detailed examples for using Bluetooth for vehicles. + +## Initialize TeslaBluetooth + +The `TeslaBluetooth` class provides methods to interact with Tesla vehicles using Bluetooth. Here's a basic example to initialize the `TeslaBluetooth` class and discover nearby Tesla vehicles: + +```python +import asyncio +from bleak import BleakScanner +from tesla_fleet_api import TeslaBluetooth + +async def main(): + scanner = BleakScanner() + devices = await scanner.discover() + for device in devices: + if TeslaBluetooth().valid_name(device.name): + print(f"Found Tesla vehicle: {device.name}") + +asyncio.run(main()) +``` + +## Create VehicleBluetooth Instance + +You can create a `VehicleBluetooth` instance using the `TeslaBluetooth` class. Here's a basic example to create a `VehicleBluetooth` instance and set the private key from a file: + +```python +import asyncio +from tesla_fleet_api import TeslaBluetooth + +async def main(): + tesla_bluetooth = TeslaBluetooth() + tesla_bluetooth.get_private_key("path/to/private_key.pem") + vehicle = tesla_bluetooth.vehicles.create("") + vehicle.find_vehicle() + print(f"Created VehicleBluetooth instance for VIN: {vehicle.vin}") + +asyncio.run(main()) +``` + +## Pair Vehicle + +You can pair a `VehicleBluetooth` instance using the `pair` method. Here's a basic example to pair a `VehicleBluetooth` instance: + +```python +import asyncio +from tesla_fleet_api import TeslaBluetooth + +async def main(): + tesla_bluetooth = TeslaBluetooth() + device = await tesla_bluetooth.find_vehicle() + private_key = tesla_bluetooth.get_private_key("path/to/private_key.pem") + vehicle = tesla_bluetooth.vehicles.create("") + await vehicle.pair() + print(f"Paired with VehicleBluetooth instance for VIN: {vehicle.vin}") + +asyncio.run(main()) +``` + +## Wake Up Vehicle + +You can wake up a `VehicleBluetooth` instance using the `wake_up` method. Here's a basic example to wake up a `VehicleBluetooth` instance: + +```python +import asyncio +from tesla_fleet_api import TeslaBluetooth + +async def main(): + tesla_bluetooth = TeslaBluetooth() + device = await tesla_bluetooth.find_vehicle() + private_key = tesla_bluetooth.get_private_key("path/to/private_key.pem") + vehicle = tesla_bluetooth.vehicles.create("") + await vehicle.wake_up() + print(f"Woke up VehicleBluetooth instance for VIN: {vehicle.vin}") + +asyncio.run(main()) +``` + +## Get Vehicle Data + +You can get data from a `VehicleBluetooth` instance using the `vehicle_data` method. Here's a basic example to get data from a `VehicleBluetooth` instance: + +```python +import asyncio +from tesla_fleet_api import TeslaBluetooth, BluetoothVehicleData + +async def main(): + tesla_bluetooth = TeslaBluetooth() + device = await tesla_bluetooth.find_vehicle() + private_key = tesla_bluetooth.get_private_key("path/to/private_key.pem") + vehicle = tesla_bluetooth.vehicles.create("") + data = await vehicle.vehicle_data([BluetoothVehicleData.CHARGE_STATE, BluetoothVehicleData.CLIMATE_STATE]) + print(f"Vehicle data for VIN: {vehicle.vin}") + print(data) + +asyncio.run(main()) +``` diff --git a/docs/fleet_api_energy_sites.md b/docs/fleet_api_energy_sites.md new file mode 100644 index 0000000..a0d3b4b --- /dev/null +++ b/docs/fleet_api_energy_sites.md @@ -0,0 +1,365 @@ +# Fleet API for Energy Sites + +This document provides detailed examples for using the Fleet API for energy sites. + +## List Energy Sites + +The `TeslaFleetApi` class provides methods to interact with the Fleet API for energy sites. Here's a basic example to list all energy sites: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + data = await api.energySites.list() + print(data) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Get Energy Site Data + +You can get detailed data for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site_data = await api.energySites.get("") + print(energy_site_data) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Backup Reserve + +You can adjust the backup reserve for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + backup_reserve_response = await energy_site.backup(backup_reserve_percent=20) + print(backup_reserve_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Backup History + +You can get the backup (off-grid) event history of a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + backup_history_response = await energy_site.backup_history(period="day") + print(backup_history_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Charge History + +You can get the charging history of a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + charge_history_response = await energy_site.charge_history(start_date="2022-01-01", end_date="2022-01-31") + print(charge_history_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Energy History + +You can get the energy measurements of a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + energy_history_response = await energy_site.energy_history(period="day") + print(energy_history_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Grid Import/Export + +You can allow or disallow charging from the grid and exporting energy to the grid for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + grid_import_export_response = await energy_site.grid_import_export(disallow_charge_from_grid_with_solar_installed=True) + print(grid_import_export_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Live Status + +You can get the live status of a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + live_status_response = await energy_site.live_status() + print(live_status_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Off-Grid Vehicle Charging Reserve + +You can adjust the off-grid vehicle charging backup reserve for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + off_grid_vehicle_charging_reserve_response = await energy_site.off_grid_vehicle_charging_reserve(off_grid_vehicle_charging_reserve_percent=10) + print(off_grid_vehicle_charging_reserve_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Operation Mode + +You can set the operation mode for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + operation_mode_response = await energy_site.operation(default_real_mode="self_consumption") + print(operation_mode_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Site Info + +You can get information about a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + site_info_response = await energy_site.site_info() + print(site_info_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Storm Mode + +You can update the storm watch participation for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + storm_mode_response = await energy_site.storm_mode(enabled=True) + print(storm_mode_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Time of Use Settings + +You can update the time of use settings for a specific energy site using its ID: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + energy_site = await api.energySites.get("") + time_of_use_settings_response = await energy_site.time_of_use_settings(settings={"tou_settings": {"tariff_content_v2": {}}}) + print(time_of_use_settings_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` diff --git a/docs/fleet_api_signed_commands.md b/docs/fleet_api_signed_commands.md new file mode 100644 index 0000000..c14b08e --- /dev/null +++ b/docs/fleet_api_signed_commands.md @@ -0,0 +1,246 @@ +# Fleet API with Signed Vehicle Commands + +This document provides detailed examples for using the Fleet API with signed vehicle commands. + +## Handshake + +The `VehicleSigned` class provides methods to interact with the Fleet API using signed vehicle commands. Here's a basic example to perform a handshake: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + print("Handshake successful") + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Wake Up Vehicle + +You can wake up a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + wake_up_response = await vehicle.wake_up() + print(wake_up_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Lock/Unlock Vehicle + +You can lock or unlock a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + lock_response = await vehicle.lock() + print(lock_response) + unlock_response = await vehicle.unlock() + print(unlock_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Start/Stop Charging + +You can start or stop charging a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + start_charging_response = await vehicle.start_charging() + print(start_charging_response) + stop_charging_response = await vehicle.stop_charging() + print(stop_charging_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Set Charge Limit + +You can set the charge limit for a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + set_charge_limit_response = await vehicle.set_charge_limit(80) + print(set_charge_limit_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Flash Lights + +You can flash the lights of a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + flash_lights_response = await vehicle.flash_lights() + print(flash_lights_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Honk Horn + +You can honk the horn of a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + honk_horn_response = await vehicle.honk_horn() + print(honk_horn_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Remote Start + +You can remotely start a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = VehicleSigned(api, "") + await vehicle.handshake() + remote_start_response = await vehicle.remote_start() + print(remote_start_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` diff --git a/docs/fleet_api_vehicles.md b/docs/fleet_api_vehicles.md new file mode 100644 index 0000000..bf61560 --- /dev/null +++ b/docs/fleet_api_vehicles.md @@ -0,0 +1,257 @@ +# Fleet API for Vehicles + +This document provides detailed examples for using the Fleet API for vehicles. + +## List Vehicles + +The `TeslaFleetApi` class provides methods to interact with the Fleet API for vehicles. Here's a basic example to list all vehicles: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + data = await api.vehicles.list() + print(data) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Get Vehicle Data + +You can get detailed data for a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle_data = await api.vehicles.get("") + print(vehicle_data) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Wake Up Vehicle + +You can wake up a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + wake_up_response = await vehicle.wake_up() + print(wake_up_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Lock/Unlock Vehicle + +You can lock or unlock a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + lock_response = await vehicle.lock() + print(lock_response) + unlock_response = await vehicle.unlock() + print(unlock_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Start/Stop Charging + +You can start or stop charging a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + start_charging_response = await vehicle.start_charging() + print(start_charging_response) + stop_charging_response = await vehicle.stop_charging() + print(stop_charging_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Set Charge Limit + +You can set the charge limit for a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + set_charge_limit_response = await vehicle.set_charge_limit(80) + print(set_charge_limit_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Flash Lights + +You can flash the lights of a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + flash_lights_response = await vehicle.flash_lights() + print(flash_lights_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Honk Horn + +You can honk the horn of a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + honk_horn_response = await vehicle.honk_horn() + print(honk_horn_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` + +## Remote Start + +You can remotely start a specific vehicle using its VIN: + +```python +import asyncio +import aiohttp +from tesla_fleet_api import TeslaFleetApi +from tesla_fleet_api.exceptions import TeslaFleetError + +async def main(): + async with aiohttp.ClientSession() as session: + api = TeslaFleetApi( + access_token="", + session=session, + region="na", + ) + + try: + vehicle = await api.vehicles.get("") + remote_start_response = await vehicle.remote_start() + print(remote_start_response) + except TeslaFleetError as e: + print(e) + +asyncio.run(main()) +``` diff --git a/docs/teslemetry.md b/docs/teslemetry.md new file mode 100644 index 0000000..fb8edef --- /dev/null +++ b/docs/teslemetry.md @@ -0,0 +1,161 @@ +# Teslemetry + +Teslemetry is a service that provides additional telemetry data for Tesla vehicles. This document provides detailed examples of how to use the Teslemetry class in the Tesla Fleet API library. + +## Initialization + +To use the Teslemetry class, you need to initialize it with an aiohttp ClientSession and an access token. + +```python +import asyncio +import aiohttp +from tesla_fleet_api import Teslemetry + +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + +asyncio.run(main()) +``` + +## Ping + +The `ping` method sends a ping request to the Teslemetry server. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + response = await teslemetry.ping() + print(response) + +asyncio.run(main()) +``` + +## Test API Authentication + +The `test` method tests the API authentication. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + response = await teslemetry.test() + print(response) + +asyncio.run(main()) +``` + +## Get User Data + +The `userdata` method retrieves user data. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + response = await teslemetry.userdata() + print(response) + +asyncio.run(main()) +``` + +## Get Metadata + +The `metadata` method retrieves user metadata, including scopes. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + response = await teslemetry.metadata() + print(response) + +asyncio.run(main()) +``` + +## Get Scopes + +The `scopes` method retrieves user scopes. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + response = await teslemetry.scopes() + print(response) + +asyncio.run(main()) +``` + +## Server-Side Polling + +The `server_side_polling` method gets or sets the server-side polling mode for a vehicle. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + vin = "" + + # Get the current server-side polling mode + response = await teslemetry.server_side_polling(vin) + print(response) + + # Enable server-side polling + response = await teslemetry.server_side_polling(vin, value=True) + print(response) + + # Disable server-side polling + response = await teslemetry.server_side_polling(vin, value=False) + print(response) + +asyncio.run(main()) +``` + +## Force Vehicle Data Refresh + +The `vehicle_data_refresh` method forces a refresh of the vehicle data. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + teslemetry = Teslemetry( + session=session, + access_token="", + ) + + vin = "" + + response = await teslemetry.vehicle_data_refresh(vin) + print(response) + +asyncio.run(main()) +``` diff --git a/docs/tessie.md b/docs/tessie.md new file mode 100644 index 0000000..0ffe1d5 --- /dev/null +++ b/docs/tessie.md @@ -0,0 +1,118 @@ +# Tessie + +Tessie is a service that provides additional telemetry data for Tesla vehicles. This document provides detailed examples of how to use the Tessie class in the Tesla Fleet API library. + +## Initialization + +To use the Tessie class, you need to initialize it with an aiohttp ClientSession and an access token. + +```python +import asyncio +import aiohttp +from tesla_fleet_api import Tessie + +async def main(): + async with aiohttp.ClientSession() as session: + tessie = Tessie( + session=session, + access_token="", + ) + +asyncio.run(main()) +``` + +## Get Vehicles + +The `vehicles` method retrieves the list of vehicles associated with the account. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + tessie = Tessie( + session=session, + access_token="", + ) + + response = await tessie.vehicles() + print(response) + +asyncio.run(main()) +``` + +## Get Vehicle State + +The `state` method retrieves the state of a specific vehicle. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + tessie = Tessie( + session=session, + access_token="", + ) + + vin = "" + + response = await tessie.state(vin) + print(response) + +asyncio.run(main()) +``` + +## Get Battery Data + +The `battery` method retrieves the battery data of a specific vehicle. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + tessie = Tessie( + session=session, + access_token="", + ) + + vin = "" + + response = await tessie.battery(vin) + print(response) + +asyncio.run(main()) +``` + +## Get Battery Health Data + +The `battery_health` method retrieves the battery health data of a specific vehicle. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + tessie = Tessie( + session=session, + access_token="", + ) + + vin = "" + + response = await tessie.battery_health(vin) + print(response) + +asyncio.run(main()) +``` + +## Get All Battery Health Data + +The `all_battery_health` method retrieves the battery health data of all vehicles associated with the account. + +```python +async def main(): + async with aiohttp.ClientSession() as session: + tessie = Tessie( + session=session, + access_token="", + ) + + response = await tessie.all_battery_health() + print(response) + +asyncio.run(main()) +```