Skip to content

Commit f70094e

Browse files
authored
Merge pull request #1 from Teslemetry/Bre77/update-readme
Update README.md with examples and documentation
2 parents 0221c9b + 3fa7720 commit f70094e

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

README.md

Lines changed: 74 additions & 5 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

@@ -25,6 +31,61 @@ Using `connect()` or `listen()` will require you to close the session manually u
2531
## Example
2632
The following example puts the listening loop in the background, then stopping after 20 seconds.
2733
```
34+
async def main():
35+
async with aiohttp.ClientSession() as session:
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
2889
async def main():
2990
async with aiohttp.ClientSession() as session:
3091
stream = TeslemetryStream(
@@ -33,14 +94,22 @@ async def main():
3394
server="na.teslemetry.com" # or "eu.teslemetry.com"
3495
session=session,
3596
)
97+
3698
await stream.connect()
3799

38-
def callback(event):
39-
print(event["data"])
100+
vehicle = stream.get_vehicle("<vin>")
40101

41-
remove = stream.async_add_listener(callback)
102+
def custom_listener(event):
103+
if "BatteryLevel" in event["data"]:
104+
print(f"Battery Level: {event['data']['BatteryLevel']}")
105+
if "VehicleSpeed" in event["data"]:
106+
print(f"Vehicle Speed: {event['data']['VehicleSpeed']}")
107+
108+
remove_custom_listener = stream.async_add_listener(custom_listener, {"vin": "<vin>", "data": {"BatteryLevel": None, "VehicleSpeed": None}})
42109

43110
print("Running")
44111
await asyncio.sleep(60)
45-
remove()
112+
remove_custom_listener()
113+
114+
await stream.disconnect()
46115
```

0 commit comments

Comments
 (0)