Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ Add listener for data updates.
### `listen(self)`
Listen to the telemetry stream.

### `listen_Credits(callback: Callable[[dict[str, str | int]], None]) -> Callable[[], None]`
### `listen_Credits(callback: Callable[[CreditsEvent], None]) -> Callable[[], None]`
Add listener for credit events.

### `listen_Balance(callback: Callable[[int], None]) -> Callable[[], None]`
Expand Down
27 changes: 27 additions & 0 deletions teslemetry_stream/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,33 @@ def from_dict(cls, data: dict[str, float | None]) -> EnergyHistoryTotals:
return cls(**{field: data.get(field) for field in cls.__dataclass_fields__})


@dataclass
class CreditsEvent:
"""A credits accounting event.

`quota` is carried through as the server sends it rather than typed
field-by-field - every observed payload has shipped it empty, so there
is no evidence yet of what it contains when populated.
"""

type: str
cost: int
name: str
balance: int
quota: dict[str, Any]

@classmethod
def from_dict(cls, data: dict[str, Any]) -> CreditsEvent:
"""Build from the event's `credits` dict."""
return cls(
type=data["type"],
cost=data["cost"],
name=data["name"],
balance=data["balance"],
quota=data.get("quota", {}),
)


@dataclass
class TeslaLocation:
"""Location data"""
Expand Down
5 changes: 3 additions & 2 deletions teslemetry_stream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import aiohttp

from .const import CreditsEvent
from .energysite import TeslemetryStreamEnergySite
from .exception import TeslemetryStreamEnded
from .vehicle import TeslemetryStreamVehicle
Expand Down Expand Up @@ -462,7 +463,7 @@ async def listen(self) -> None:
LOGGER.debug("Listen has finished")

def listen_Credits(
self, callback: Callable[[dict[str, str | int]], None]
self, callback: Callable[[CreditsEvent], None]
) -> Callable[[], None]:
"""
Listen for credits update.
Expand All @@ -471,7 +472,7 @@ def listen_Credits(
:return: Function to remove the listener.
"""
return self.async_add_listener(
lambda x: callback(x["credits"]), {"credits": None}
lambda x: callback(CreditsEvent.from_dict(x["credits"])), {"credits": None}
)

def listen_Balance(self, callback: Callable[[int], None]) -> Callable[[], None]:
Expand Down
Loading