This document provides detailed examples for using Bluetooth for vehicles.
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:
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())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:
import asyncio
from tesla_fleet_api import TeslaBluetooth
async def main():
tesla_bluetooth = TeslaBluetooth()
await tesla_bluetooth.get_private_key("path/to/private_key.pem")
vehicle = tesla_bluetooth.vehicles.create("<vin>")
vehicle.find_vehicle()
print(f"Created VehicleBluetooth instance for VIN: {vehicle.vin}")
asyncio.run(main())You can pair a VehicleBluetooth instance using the pair method. Here's a basic example to pair a VehicleBluetooth instance:
import asyncio
from tesla_fleet_api import TeslaBluetooth
async def main():
tesla_bluetooth = TeslaBluetooth()
device = await tesla_bluetooth.find_vehicle()
private_key = await tesla_bluetooth.get_private_key("path/to/private_key.pem")
vehicle = tesla_bluetooth.vehicles.create("<vin>")
await vehicle.pair()
print(f"Paired with VehicleBluetooth instance for VIN: {vehicle.vin}")
asyncio.run(main())You can wake up a VehicleBluetooth instance using the wake_up method. Here's a basic example to wake up a VehicleBluetooth instance:
import asyncio
from tesla_fleet_api import TeslaBluetooth
async def main():
tesla_bluetooth = TeslaBluetooth()
device = await tesla_bluetooth.find_vehicle()
private_key = await tesla_bluetooth.get_private_key("path/to/private_key.pem")
vehicle = tesla_bluetooth.vehicles.create("<vin>")
await vehicle.wake_up()
print(f"Woke up VehicleBluetooth instance for VIN: {vehicle.vin}")
asyncio.run(main())The individual door closure commands are Bluetooth-only and are not available via Fleet API signed commands.
Available commands:
open_front_driver_door()close_front_driver_door()open_front_passenger_door()close_front_passenger_door()open_rear_driver_door()close_rear_driver_door()open_rear_passenger_door()close_rear_passenger_door()
Example:
import asyncio
from tesla_fleet_api import TeslaBluetooth
async def main():
tesla_bluetooth = TeslaBluetooth()
await tesla_bluetooth.get_private_key("path/to/private_key.pem")
vehicle = tesla_bluetooth.vehicles.create("<vin>")
await vehicle.connect()
await vehicle.open_front_driver_door()
await vehicle.close_front_driver_door()
await vehicle.disconnect()
asyncio.run(main())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:
import asyncio
from tesla_fleet_api import TeslaBluetooth, BluetoothVehicleData
async def main():
tesla_bluetooth = TeslaBluetooth()
device = await tesla_bluetooth.find_vehicle()
private_key = await tesla_bluetooth.get_private_key("path/to/private_key.pem")
vehicle = tesla_bluetooth.vehicles.create("<vin>")
data = await vehicle.vehicle_data([BluetoothVehicleData.CHARGE_STATE, BluetoothVehicleData.CLIMATE_STATE])
print(f"Vehicle data for VIN: {vehicle.vin}")
print(data)
asyncio.run(main())