Skip to content

Commit 3fa7720

Browse files
committed
* Update README.md example to use explicit connect and disconnect methods
* **Example Update** - Replace `async with` context syntax with explicit `connect` and `disconnect` methods - Add `stream.connect()` and `stream.disconnect()` calls - Adjust indentation and remove `async with` block - Add `stream` variable assignment outside the context block
1 parent a05abe5 commit 3fa7720

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,28 @@ You can also write your own listener that listens to multiple signals. Here is a
8888
```python
8989
async def main():
9090
async with aiohttp.ClientSession() as session:
91-
async with TeslemetryStream(
91+
stream = TeslemetryStream(
9292
access_token="<token>",
9393
vin="<vin>", # for single vehicles
9494
server="na.teslemetry.com" # or "eu.teslemetry.com"
9595
session=session,
96-
) as stream:
96+
)
9797

98-
vehicle = stream.get_vehicle("<vin>")
98+
await stream.connect()
9999

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']}")
100+
vehicle = stream.get_vehicle("<vin>")
105101

106-
remove_custom_listener = stream.async_add_listener(custom_listener, {"vin": "<vin>", "data": {"BatteryLevel": None, "VehicleSpeed": None}})
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']}")
107107

108-
print("Running")
109-
await asyncio.sleep(60)
110-
remove_custom_listener()
108+
remove_custom_listener = stream.async_add_listener(custom_listener, {"vin": "<vin>", "data": {"BatteryLevel": None, "VehicleSpeed": None}})
109+
110+
print("Running")
111+
await asyncio.sleep(60)
112+
remove_custom_listener()
113+
114+
await stream.disconnect()
111115
```

0 commit comments

Comments
 (0)