Skip to content

Commit b756e41

Browse files
committed
Add metadata listeners
1 parent 3ecd686 commit b756e41

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ The TeslemetryStream class requires:
1818
- session: an aiohttp.ClientSession
1919
- access_token: an access token from the [Teslemetry console](https://teslemetry.com/console)
2020
- vin: If you only want to use a single vehicle, otherwise use `create_vehicle`
21-
- server: The Teslemetry server to connect to, otherwise use `find_server`
21+
- server: Override the Teslemetry server to connect to:
22+
- api.teslemetry.com (recommended and default)
2223
- na.teslemetry.com
2324
- eu.teslemetry.com
2425

@@ -60,7 +61,6 @@ async def main():
6061
async with TeslemetryStream(
6162
access_token="<token>",
6263
vin="<vin>", # for single vehicles
63-
server="na.teslemetry.com" # or "eu.teslemetry.com"
6464
session=session,
6565
) as stream:
6666

@@ -175,6 +175,25 @@ Replace Fleet Telemetry configuration for the vehicle.
175175
### `config(self) -> dict`
176176
Return current configuration for the vehicle.
177177

178+
### `listen_State(callback: Callable[[bool], None]) -> Callable[[],None]`
179+
Listen for vehicle online state polling. The callback receives a boolean value representing whether the vehicle is online.
180+
181+
### `listen_VehicleData(callback: Callable[[dict], None]) -> Callable[[],None]`
182+
Listen for vehicle data polling events. The callback receives a dictionary containing the complete vehicle data.
183+
184+
### `listen_Cellular(callback: Callable[[bool], None]) -> Callable[[],None]`
185+
Listen for cellular connectivity events. The callback receives a boolean value indicating whether the cellular connection is established.
186+
187+
### `listen_Wifi(callback: Callable[[bool], None]) -> Callable[[],None]`
188+
Listen for WiFi connectivity events. The callback receives a boolean value indicating whether the WiFi connection is established.
189+
190+
### `listen_Alerts(callback: Callable[[list[dict]], None]) -> Callable[[],None]`
191+
Listen for vehicle alert events. The callback receives a list of dictionaries containing alert information.
192+
193+
### `listen_Errors(callback: Callable[[list[dict]], None]) -> Callable[[],None]`
194+
Listen for vehicle error events. The callback receives a list of dictionaries containing error information.
195+
178196
### `listen_*` Methods
179197
The `TeslemetryStreamVehicle` class contains a `listen_*` methods for each telemetry signal.
180198
These methods allow you to listen to specific signals and handle their data in a type-safe manner.
199+
A full list of fields and metadata can be found at [api.teslemetry.com/fields.json](https://api.teslemetry.com/fields.json)

teslemetry_stream/const.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ class StrEnum(str, Enum):
1010
"""String Enum"""
1111

1212

13+
class Key(StrEnum):
14+
"""Topics available in Fleet Telemetry streams"""
15+
VIN = "vin"
16+
DATA = "data"
17+
ALERTS = "alerts"
18+
ERRORS = "errors"
19+
VEHICLE_DATA = "vehicle_data"
20+
STATE = "state"
21+
STATUS = "status"
22+
NETWORK_INTERFACE = "networkInterface"
23+
1324
class Signal(StrEnum):
1425
"""Signals available in Fleet Telemetry streams"""
1526

@@ -248,6 +259,24 @@ class Alert(StrEnum):
248259
SERVICE = "Service"
249260
SERVICE_FIX = "ServiceFix"
250261

262+
class State(StrEnum):
263+
"""States available in Fleet Telemetry streams"""
264+
265+
ONLINE = "online"
266+
OFFLINE = "offline"
267+
ASLEEP = "asleep"
268+
269+
class NetworkInterface(StrEnum):
270+
"""Network interfaces available in Fleet Telemetry streams"""
271+
272+
WIFI = "wifi"
273+
CELLULAR = "cellular"
274+
275+
class Status(StrEnum):
276+
"""Statuses available in Fleet Telemetry streams"""
277+
278+
CONNECTED = "CONNECTED"
279+
DISCONNECTED = "DISCONNECTED"
251280

252281
@dataclass
253282
class TeslaLocation:

teslemetry_stream/vehicle.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
HvacAutoModeState,
2929
HvacPowerState,
3030
HvilStatus,
31+
Key,
3132
LaneAssistLevel,
3233
MediaStatus,
3334
PowershareState,
@@ -38,6 +39,9 @@
3839
SentryModeState,
3940
ShiftState,
4041
Signal,
42+
State,
43+
Status,
44+
NetworkInterface,
4145
SunroofInstalledState,
4246
TemperatureUnit,
4347
TeslaLocation,
@@ -162,6 +166,49 @@ def _enable_field(self, field: Signal) -> None:
162166
"""Enable a field for streaming from a listener."""
163167
asyncio.create_task(self.add_field(field))
164168

169+
# Add listeners for other streaming fields
170+
def listen_State(self, callback: Callable[[bool], None]) -> Callable[[],None]:
171+
"""Listen for State polling."""
172+
return self.stream.async_add_listener(
173+
lambda x: callback(x[Key.STATE] == State.ONLINE),
174+
{Key.VIN: self.vin, Key.STATE: None}
175+
)
176+
177+
def listen_VehicleData(self, callback: Callable[[dict], None]) -> Callable[[],None]:
178+
"""Listen for Vehicle Data polling."""
179+
return self.stream.async_add_listener(
180+
lambda x: callback(x[Key.VEHICLE_DATA]),
181+
{Key.VIN: self.vin, Key.VEHICLE_DATA: None}
182+
)
183+
184+
def listen_Cellular(self, callback: Callable[[bool], None]) -> Callable[[],None]:
185+
"""Listen for Cellular connectivity."""
186+
return self.stream.async_add_listener(
187+
lambda x: callback(x[Key.STATUS] == Status.CONNECTED),
188+
{Key.VIN: self.vin, Key.NETWORK_INTERFACE: NetworkInterface.CELLULAR}
189+
)
190+
191+
def listen_Wifi(self, callback: Callable[[bool], None]) -> Callable[[],None]:
192+
"""Listen for WiFi connectivity."""
193+
return self.stream.async_add_listener(
194+
lambda x: callback(x[Key.STATUS] == Status.CONNECTED),
195+
{Key.VIN: self.vin, Key.NETWORK_INTERFACE: NetworkInterface.WIFI}
196+
)
197+
198+
def listen_Alerts(self, callback: Callable[[list[dict]], None]) -> Callable[[],None]:
199+
"""Listen for Alerts."""
200+
return self.stream.async_add_listener(
201+
lambda x: callback(x[Key.ALERTS]),
202+
{Key.VIN: self.vin, Key.ALERTS: None}
203+
)
204+
205+
def listen_Errors(self, callback: Callable[[list[dict]], None]) -> Callable[[],None]:
206+
"""Listen for Errors."""
207+
return self.stream.async_add_listener(
208+
lambda x: callback(x[Key.ERRORS]),
209+
{Key.VIN: self.vin, Key.ERRORS: None}
210+
)
211+
165212
# Add listeners for each signal
166213
def listen_ACChargingEnergyIn(self, callback: Callable[[float | None], None]) -> Callable[[],None]:
167214
"""Listen for AC Charging Energy In."""

0 commit comments

Comments
 (0)