Skip to main content

Teslemetry Streaming API library for Python

Project description

Teslemetry Stream Library

This is an asynchronous Python 3 library that connects to the Teslemetry Stream service and provides Tesla Fleet Telemetry using server sent events. The library allows you to listen to various telemetry signals from Tesla vehicles, and provides a convenient way to handle these signals using typed listen methods.

Capabilities

  • Connect to the Teslemetry Stream service
  • Listen to various telemetry signals from Tesla vehicles
  • Handle signals using typed listen methods
  • Write custom listeners for multiple signals

Installation

pip install teslemetry-stream

Usage

The TeslemetryStream class requires:

  • session: an aiohttp.ClientSession
  • access_token: an access token from the Teslemetry console
  • vin: If you only want to use a single vehicle, otherwise use create_vehicle
  • server: Override the Teslemetry server to connect to:
    • api.teslemetry.com (recommended and default)
    • na.teslemetry.com
    • eu.teslemetry.com

The full list of possible values are provided in TelemetryFields and TelemetryAlerts

To connect, either use async with on the instance, call connect(), or register an callback with async_add_listener, which will connect when added and disconnect when removed.

Using connect() or listen() will require you to close the session manually using close().

Example

The following example puts the listening loop in the background, then stopping after 20 seconds.

async def main():
    async with aiohttp.ClientSession() as session:
        async with TeslemetryStream(
            access_token="<token>",
            vin="<vin>", # for single vehicles
            server="na.teslemetry.com" # or "eu.teslemetry.com"
            session=session,
        ) as stream:

            def callback(event):
                print(event["data"])

            remove = stream.async_add_listener(callback)

            print("Running")
            await asyncio.sleep(60)
            remove()

Using Typed Listen Methods

The library provides typed listen methods for various telemetry signals. These methods allow you to listen to specific signals and handle their data in a type-safe manner. Here is an example of using the typed listen methods:

async def main():
    async with aiohttp.ClientSession() as session:
        async with TeslemetryStream(
            access_token="<token>",
            vin="<vin>", # for single vehicles
            session=session,
        ) as stream:

            vehicle = stream.get_vehicle("<vin>")

            def battery_level_callback(battery_level):
                print(f"Battery Level: {battery_level}")

            def vehicle_speed_callback(vehicle_speed):
                print(f"Vehicle Speed: {vehicle_speed}")

            remove_battery_level_listener = vehicle.listen_BatteryLevel(battery_level_callback)
            remove_vehicle_speed_listener = vehicle.listen_VehicleSpeed(vehicle_speed_callback)

            print("Running")
            await asyncio.sleep(60)
            remove_battery_level_listener()
            remove_vehicle_speed_listener()

Writing Your Own Listener with Multiple Signals

You can also write your own listener that listens to multiple signals. Here is an example of writing a custom listener:

async def main():
    async with aiohttp.ClientSession() as session:
        stream = TeslemetryStream(
            access_token="<token>",
            vin="<vin>", # for single vehicles
            server="na.teslemetry.com" # or "eu.teslemetry.com"
            session=session,
        )

        await stream.connect()

        vehicle = stream.get_vehicle("<vin>")

        def custom_listener(event):
            if "BatteryLevel" in event["data"]:
                print(f"Battery Level: {event['data']['BatteryLevel']}")
            if "VehicleSpeed" in event["data"]:
                print(f"Vehicle Speed: {event['data']['VehicleSpeed']}")

        remove_custom_listener = stream.async_add_listener(custom_listener, {"vin": "<vin>", "data": {"BatteryLevel": None, "VehicleSpeed": None}})

        print("Running")
        await asyncio.sleep(60)
        remove_custom_listener()

        await stream.close()

Public Methods in TeslemetryStream Class

__init__(session: aiohttp.ClientSession, access_token: str, server: str | None = None, vin: str | None = None, parse_timestamp: bool = False)

Initialize the TeslemetryStream client.

get_vehicle(vin: str) -> TeslemetryStreamVehicle

Create a vehicle object to manage config and create listeners.

connected -> bool

Return if connected.

get_config(vin: str | None = None) -> None

Get the current stream config.

find_server(self) -> None

Find the server using metadata.

update_fields(fields: dict, vin: str) -> dict

Modify the Fleet Telemetry configuration.

replace_fields(fields: dict, vin: str) -> dict

Replace the Fleet Telemetry configuration.

config(self) -> dict

Return current configuration.

connect(self) -> None

Connect to the telemetry stream.

close(self) -> None

Close connection.

async_add_listener(callback: Callable, filters: dict | None = None) -> Callable[[], None]

Add listener for data updates.

listen(self)

Listen to the telemetry stream.

listen_Credits(callback: Callable[[dict[str, str | int]], None]) -> Callable[[], None]

Add listener for credit events.

listen_Balance(callback: Callable[[int], None]) -> Callable[[], None]

Add listener for credit balance.

Public Methods in TeslemetryStreamVehicle Class

__init__(stream: TeslemetryStream, vin: str)

Initialize the TeslemetryStreamVehicle instance.

get_config(self) -> None

Get the current vehicle config.

update_fields(fields: dict) -> dict

Update Fleet Telemetry configuration for the vehicle.

replace_fields(fields: dict) -> dict

Replace Fleet Telemetry configuration for the vehicle.

config(self) -> dict

Return current configuration for the vehicle.

listen_State(callback: Callable[[bool], None]) -> Callable[[],None]

Listen for vehicle online state polling. The callback receives a boolean value representing whether the vehicle is online.

listen_VehicleData(callback: Callable[[dict], None]) -> Callable[[],None]

Listen for vehicle data polling events. The callback receives a dictionary containing the complete vehicle data.

listen_Cellular(callback: Callable[[bool], None]) -> Callable[[],None]

Listen for cellular connectivity events. The callback receives a boolean value indicating whether the cellular connection is established.

listen_Wifi(callback: Callable[[bool], None]) -> Callable[[],None]

Listen for WiFi connectivity events. The callback receives a boolean value indicating whether the WiFi connection is established.

listen_Alerts(callback: Callable[[list[dict]], None]) -> Callable[[],None]

Listen for vehicle alert events. The callback receives a list of dictionaries containing alert information.

listen_Errors(callback: Callable[[list[dict]], None]) -> Callable[[],None]

Listen for vehicle error events. The callback receives a list of dictionaries containing error information.

listen_* Methods

The TeslemetryStreamVehicle class contains a listen_* methods for each telemetry signal. These methods allow you to listen to specific signals and handle their data in a type-safe manner. A full list of fields and metadata can be found at api.teslemetry.com/fields.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teslemetry_stream-0.9.1.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

teslemetry_stream-0.9.1-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file teslemetry_stream-0.9.1.tar.gz.

File metadata

  • Download URL: teslemetry_stream-0.9.1.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for teslemetry_stream-0.9.1.tar.gz
Algorithm Hash digest
SHA256 6d0256a975e3dbe3f07476e12f0b399a19c25ac4b3ea7ed18dd7999c33a0644d
MD5 65b9ea6867c9e3d0f7111b701327d6ca
BLAKE2b-256 cda5a1df31093ab1c251ade64db4eabcaf15fa4cafca26bc95dfd869385c8cfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for teslemetry_stream-0.9.1.tar.gz:

Publisher: python-publish.yml on Teslemetry/python-teslemetry-stream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file teslemetry_stream-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for teslemetry_stream-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fdf8a2d55df6fa210ba4ea52505cb0aca028eff4a607c6c68c54822c6cc6e41a
MD5 a95ad7f3f049d8fa91ed36fa4edb14de
BLAKE2b-256 7f1e9f9297753028788a61f5dbd190114d935026a7a7588d40e254d25ebbda85

See more details on using hashes here.

Provenance

The following attestation bundles were made for teslemetry_stream-0.9.1-py3-none-any.whl:

Publisher: python-publish.yml on Teslemetry/python-teslemetry-stream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page