|
12 | 12 | from datetime import datetime, timezone |
13 | 13 | from typing import Any |
14 | 14 |
|
15 | | -from teslemetry_stream.const import EnergyHistoryTotals, EnergyTotalsEvent |
| 15 | +from teslemetry_stream.const import ( |
| 16 | + EnergyHistoryTotals, |
| 17 | + EnergyTotalsEvent, |
| 18 | + WallConnectorTotalsEvent, |
| 19 | +) |
16 | 20 | from teslemetry_stream.stream import TeslemetryStream, recursive_match |
17 | 21 |
|
18 | 22 | SITE_A = "12345" |
|
75 | 79 | "site_id": int(SITE_A), |
76 | 80 | } |
77 | 81 |
|
| 82 | +WALL_CONNECTOR_TOTALS_EVENT: dict[str, Any] = { |
| 83 | + "id": SITE_A, |
| 84 | + "date": "2026-07-29", |
| 85 | + "createdAt": "2026-07-29T10:16:00.000Z", |
| 86 | + "totals": {"DIN-1": 12500.5, "DIN-2": 800}, |
| 87 | +} |
| 88 | + |
78 | 89 | OTHER_SITE_LIVE_STATUS: dict[str, Any] = { |
79 | 90 | "createdAt": "2026-07-28T10:16:00.000Z", |
80 | 91 | "site_id": SITE_B, |
@@ -384,6 +395,61 @@ def main() -> None: |
384 | 395 | ) |
385 | 396 | ) |
386 | 397 |
|
| 398 | + # listen_WallConnectorTotals delivers per-DIN totals with date, created_at, is_cache. |
| 399 | + stream = make_stream() |
| 400 | + site = stream.get_energysite(SITE_A) |
| 401 | + wc_received: list[WallConnectorTotalsEvent] = [] |
| 402 | + site.listen_WallConnectorTotals(wc_received.append) |
| 403 | + dispatch(stream, WALL_CONNECTOR_TOTALS_EVENT) |
| 404 | + dispatch( |
| 405 | + stream, |
| 406 | + { |
| 407 | + **WALL_CONNECTOR_TOTALS_EVENT, |
| 408 | + "id": int(SITE_A), |
| 409 | + "isCache": True, |
| 410 | + "date": "2026-07-28", |
| 411 | + }, |
| 412 | + ) |
| 413 | + dispatch(stream, {**WALL_CONNECTOR_TOTALS_EVENT, "id": SITE_B}) |
| 414 | + results.append( |
| 415 | + check( |
| 416 | + "listen_WallConnectorTotals parses totals, matches str/int id, filters other site", |
| 417 | + wc_received |
| 418 | + == [ |
| 419 | + WallConnectorTotalsEvent( |
| 420 | + date="2026-07-29", |
| 421 | + created_at=datetime(2026, 7, 29, 10, 16, 0, tzinfo=timezone.utc), |
| 422 | + is_cache=False, |
| 423 | + totals={"DIN-1": 12500.5, "DIN-2": 800}, |
| 424 | + ), |
| 425 | + WallConnectorTotalsEvent( |
| 426 | + date="2026-07-28", |
| 427 | + created_at=datetime(2026, 7, 29, 10, 16, 0, tzinfo=timezone.utc), |
| 428 | + is_cache=True, |
| 429 | + totals={"DIN-1": 12500.5, "DIN-2": 800}, |
| 430 | + ), |
| 431 | + ], |
| 432 | + f"got {wc_received}", |
| 433 | + ) |
| 434 | + ) |
| 435 | + |
| 436 | + # The two totals events share an envelope; each listener ignores the other's. |
| 437 | + stream = make_stream() |
| 438 | + site = stream.get_energysite(SITE_A) |
| 439 | + wc_received = [] |
| 440 | + totals_received = [] |
| 441 | + site.listen_WallConnectorTotals(wc_received.append) |
| 442 | + site.listen_EnergyTotals(totals_received.append) |
| 443 | + dispatch(stream, ENERGY_TOTALS_EVENT) |
| 444 | + dispatch(stream, {**WALL_CONNECTOR_TOTALS_EVENT, "totals": {}}) |
| 445 | + results.append( |
| 446 | + check( |
| 447 | + "energy_totals and wall_connector_totals do not cross-deliver", |
| 448 | + len(totals_received) == 1 and len(wc_received) == 1 and wc_received[0].totals == {}, |
| 449 | + f"energy={totals_received} wc={wc_received}", |
| 450 | + ) |
| 451 | + ) |
| 452 | + |
387 | 453 | # get_energysite is idempotent per id, mirroring get_vehicle. |
388 | 454 | stream = make_stream() |
389 | 455 | results.append( |
|
0 commit comments