From d13f5d2c42f5875f346936a00241dae453f71cb1 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 9 Dec 2024 08:26:43 +1000 Subject: [PATCH] v0.5.3 vehicles and renames --- setup.py | 2 +- teslemetry_stream/__init__.py | 6 +++--- teslemetry_stream/const.py | 8 ++++---- teslemetry_stream/stream.py | 11 ++++++----- teslemetry_stream/vehicle.py | 8 ++++---- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/setup.py b/setup.py index b3eddb3..a8695d0 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="teslemetry_stream", - version="0.5.2", + version="0.5.3", author="Brett Adams", author_email="hello@teslemetry.com", description="Teslemetry Streaming API library for Python", diff --git a/teslemetry_stream/__init__.py b/teslemetry_stream/__init__.py index 8184e22..bc72d04 100644 --- a/teslemetry_stream/__init__.py +++ b/teslemetry_stream/__init__.py @@ -6,7 +6,7 @@ TeslemetryStreamVehicleNotConfigured, TeslemetryStreamEnded ) -from .const import TelemetryFields, TelemetryAlerts +from .const import Signal, Alert __all__ = [ "TeslemetryStream", @@ -15,6 +15,6 @@ "TeslemetryStreamConnectionError", "TeslemetryStreamVehicleNotConfigured", "TeslemetryStreamEnded", - "TelemetryFields", - "TelemetryAlerts" + "Signal", + "Alert" ] diff --git a/teslemetry_stream/const.py b/teslemetry_stream/const.py index 8003765..5d21d8e 100644 --- a/teslemetry_stream/const.py +++ b/teslemetry_stream/const.py @@ -9,8 +9,8 @@ class StrEnum(str, Enum): """String Enum""" -class TelemetryFields(StrEnum): - """Fields available in telemetry streams""" +class Signal(StrEnum): + """Signals available in Fleet Telemetry streams""" AC_CHARGING_ENERGY_IN = "ACChargingEnergyIn" AC_CHARGING_POWER = "ACChargingPower" @@ -213,8 +213,8 @@ class TelemetryFields(StrEnum): WIPER_HEAT_ENABLED = "WiperHeatEnabled" -class TelemetryAlerts(StrEnum): - """Alerts available in telemetry streams""" +class Alert(StrEnum): + """Alerts available in Fleet Telemetry streams""" CUSTOMER = "Customer" SERVICE = "Service" diff --git a/teslemetry_stream/stream.py b/teslemetry_stream/stream.py index 19a55a6..cb6a086 100644 --- a/teslemetry_stream/stream.py +++ b/teslemetry_stream/stream.py @@ -18,6 +18,7 @@ class TeslemetryStream: delay: int active = None vehicle: TeslemetryStreamVehicle + vehicles: dict[str, TeslemetryStreamVehicle] = {} def __init__( self, @@ -39,15 +40,15 @@ def __init__( self.delay = DELAY if(self.vin): - self.vehicle = self.create_vehicle(self.vin) + self.vehicle = self.get_vehicle(self.vin) - def create_vehicle(self, vin: str) -> TeslemetryStreamVehicle: + def get_vehicle(self, vin: str) -> TeslemetryStreamVehicle: """Create a vehicle stream.""" if self.vin is not None and self.vin != vin: raise AttributeError("Stream started in single vehicle mode") - if hasattr(self, 'vehicle'): - return self.vehicle - return TeslemetryStreamVehicle(self, vin) + if vin not in self.vehicles: + self.vehicles[vin] = TeslemetryStreamVehicle(self, vin) + return self.vehicles[vin] @property def connected(self) -> bool: diff --git a/teslemetry_stream/vehicle.py b/teslemetry_stream/vehicle.py index 2c0db6c..926fa8f 100644 --- a/teslemetry_stream/vehicle.py +++ b/teslemetry_stream/vehicle.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING import asyncio import logging -from .const import TelemetryFields +from .const import Signal if TYPE_CHECKING: from .stream import TeslemetryStream @@ -13,7 +13,7 @@ class TeslemetryStreamVehicle: """Handle streaming field updates.""" - fields: dict[TelemetryFields, dict[str, int]] = {} + fields: dict[Signal, dict[str, int]] = {} preferTyped: bool | None = None _config: dict = {} @@ -73,9 +73,9 @@ async def update_config(self, config: dict) -> None: self.preferTyped = prefer_typed self._config.clear() - async def add_field(self, field: TelemetryFields | str, interval: int | None = None) -> None: + async def add_field(self, field: Signal | str, interval: int | None = None) -> None: """Handle vehicle data from the stream.""" - if isinstance(field, TelemetryFields): + if isinstance(field, Signal): field = field.value if field in self.fields and (interval is None or self.fields[field].get("interval_seconds") == interval):