Skip to content

Commit a05abe5

Browse files
committed
Update README.md with examples and documentation
--- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Teslemetry/python-teslemetry-stream?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 0221c9b commit a05abe5

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

README.md

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Teslemetry Stream Library
2-
This is an asynchronous Python 3 library that connects to the Teslemetry Stream server and provides Tesla Fleet Telemetry using server side events.
2+
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.
3+
4+
## Capabilities
5+
- Connect to the Teslemetry Stream service
6+
- Listen to various telemetry signals from Tesla vehicles
7+
- Handle signals using typed listen methods
8+
- Write custom listeners for multiple signals
39

410
## Installation
511

@@ -27,20 +33,79 @@ The following example puts the listening loop in the background, then stopping a
2733
```
2834
async def main():
2935
async with aiohttp.ClientSession() as session:
30-
stream = TeslemetryStream(
36+
async with TeslemetryStream(
37+
access_token="<token>",
38+
vin="<vin>", # for single vehicles
39+
server="na.teslemetry.com" # or "eu.teslemetry.com"
40+
session=session,
41+
) as stream:
42+
43+
def callback(event):
44+
print(event["data"])
45+
46+
remove = stream.async_add_listener(callback)
47+
48+
print("Running")
49+
await asyncio.sleep(60)
50+
remove()
51+
```
52+
53+
## Using Typed Listen Methods
54+
55+
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:
56+
57+
```python
58+
async def main():
59+
async with aiohttp.ClientSession() as session:
60+
async with TeslemetryStream(
61+
access_token="<token>",
62+
vin="<vin>", # for single vehicles
63+
server="na.teslemetry.com" # or "eu.teslemetry.com"
64+
session=session,
65+
) as stream:
66+
67+
vehicle = stream.get_vehicle("<vin>")
68+
69+
def battery_level_callback(battery_level):
70+
print(f"Battery Level: {battery_level}")
71+
72+
def vehicle_speed_callback(vehicle_speed):
73+
print(f"Vehicle Speed: {vehicle_speed}")
74+
75+
remove_battery_level_listener = vehicle.listen_BatteryLevel(battery_level_callback)
76+
remove_vehicle_speed_listener = vehicle.listen_VehicleSpeed(vehicle_speed_callback)
77+
78+
print("Running")
79+
await asyncio.sleep(60)
80+
remove_battery_level_listener()
81+
remove_vehicle_speed_listener()
82+
```
83+
84+
## Writing Your Own Listener with Multiple Signals
85+
86+
You can also write your own listener that listens to multiple signals. Here is an example of writing a custom listener:
87+
88+
```python
89+
async def main():
90+
async with aiohttp.ClientSession() as session:
91+
async with TeslemetryStream(
3192
access_token="<token>",
3293
vin="<vin>", # for single vehicles
3394
server="na.teslemetry.com" # or "eu.teslemetry.com"
3495
session=session,
35-
)
36-
await stream.connect()
96+
) as stream:
97+
98+
vehicle = stream.get_vehicle("<vin>")
3799

38-
def callback(event):
39-
print(event["data"])
100+
def custom_listener(event):
101+
if "BatteryLevel" in event["data"]:
102+
print(f"Battery Level: {event['data']['BatteryLevel']}")
103+
if "VehicleSpeed" in event["data"]:
104+
print(f"Vehicle Speed: {event['data']['VehicleSpeed']}")
40105

41-
remove = stream.async_add_listener(callback)
106+
remove_custom_listener = stream.async_add_listener(custom_listener, {"vin": "<vin>", "data": {"BatteryLevel": None, "VehicleSpeed": None}})
42107

43-
print("Running")
44-
await asyncio.sleep(60)
45-
remove()
108+
print("Running")
109+
await asyncio.sleep(60)
110+
remove_custom_listener()
46111
```

0 commit comments

Comments
 (0)