|
| 1 | +"""Checks energy site listener filtering against PR 310's SSE event shapes. |
| 2 | +
|
| 3 | +Fixtures mirror the `liveStatusSchema`/`siteInfoSchema` from Teslemetry/api |
| 4 | +PR 310: a flat envelope of `createdAt`, `site_id`, optional `isCache`, and |
| 5 | +the full document under `live_status`/`site_info` (opaque, not a delta). |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +from teslemetry_stream.stream import TeslemetryStream, recursive_match |
| 12 | + |
| 13 | +SITE_A = "12345" |
| 14 | +SITE_B = "67890" |
| 15 | + |
| 16 | +LIVE_STATUS_SNAPSHOT: dict[str, Any] = { |
| 17 | + "createdAt": "2026-07-28T10:15:30.000Z", |
| 18 | + "site_id": SITE_A, |
| 19 | + "isCache": True, |
| 20 | + "live_status": { |
| 21 | + "battery_power": 1200, |
| 22 | + "load_power": 900, |
| 23 | + "grid_power": -300, |
| 24 | + "solar_power": 2400, |
| 25 | + "percentage_charged": 82.5, |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +LIVE_STATUS_LIVE: dict[str, Any] = { |
| 30 | + "createdAt": "2026-07-28T10:16:00.000Z", |
| 31 | + "site_id": SITE_A, |
| 32 | + "live_status": { |
| 33 | + "battery_power": 1100, |
| 34 | + "load_power": 950, |
| 35 | + "grid_power": -150, |
| 36 | + "solar_power": 2200, |
| 37 | + "percentage_charged": 82.6, |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +SITE_INFO_SNAPSHOT: dict[str, Any] = { |
| 42 | + "createdAt": "2026-07-28T10:15:30.000Z", |
| 43 | + "site_id": SITE_A, |
| 44 | + "isCache": True, |
| 45 | + "site_info": { |
| 46 | + "site_name": "Home", |
| 47 | + "backup_reserve_percent": 20, |
| 48 | + "default_real_mode": "self_consumption", |
| 49 | + }, |
| 50 | +} |
| 51 | + |
| 52 | +OTHER_SITE_LIVE_STATUS: dict[str, Any] = { |
| 53 | + "createdAt": "2026-07-28T10:16:00.000Z", |
| 54 | + "site_id": SITE_B, |
| 55 | + "live_status": {"battery_power": 0}, |
| 56 | +} |
| 57 | + |
| 58 | +CREDITS_EVENT: dict[str, Any] = { |
| 59 | + "credits": {"type": "snapshot", "cost": 0, "name": "snapshot", "balance": 5, "quota": {}}, |
| 60 | + "createdAt": "2026-07-28T10:16:00.000Z", |
| 61 | + "isCache": True, |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +def make_stream() -> TeslemetryStream: |
| 66 | + """Build a stream that never actually connects.""" |
| 67 | + return TeslemetryStream(session=None, access_token="test-token", manual=True) # type: ignore[arg-type] |
| 68 | + |
| 69 | + |
| 70 | +def dispatch(stream: TeslemetryStream, event: dict[str, Any]) -> None: |
| 71 | + """Replicate stream.listen()'s per-event dispatch without a live connection.""" |
| 72 | + for listener, filters in list(stream._listeners.values()): |
| 73 | + if recursive_match(filters, event): |
| 74 | + listener(event) |
| 75 | + |
| 76 | + |
| 77 | +def check(label: str, ok: bool, detail: str = "") -> bool: |
| 78 | + print(f"{label:<64} {'PASS' if ok else 'FAIL'}{' ' + detail if detail else ''}") |
| 79 | + return ok |
| 80 | + |
| 81 | + |
| 82 | +def main() -> None: |
| 83 | + results = [] |
| 84 | + |
| 85 | + # listen_LiveStatus receives the unwrapped live_status document, snapshot and live alike. |
| 86 | + stream = make_stream() |
| 87 | + site = stream.get_energysite(SITE_A) |
| 88 | + received: list[dict[str, Any]] = [] |
| 89 | + site.listen_LiveStatus(received.append) |
| 90 | + dispatch(stream, LIVE_STATUS_SNAPSHOT) |
| 91 | + dispatch(stream, LIVE_STATUS_LIVE) |
| 92 | + results.append( |
| 93 | + check( |
| 94 | + "listen_LiveStatus unwraps the live_status document", |
| 95 | + received == [LIVE_STATUS_SNAPSHOT["live_status"], LIVE_STATUS_LIVE["live_status"]], |
| 96 | + f"got {received}", |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + # listen_SiteInfo receives the unwrapped site_info document. |
| 101 | + stream = make_stream() |
| 102 | + site = stream.get_energysite(SITE_A) |
| 103 | + received = [] |
| 104 | + site.listen_SiteInfo(received.append) |
| 105 | + dispatch(stream, SITE_INFO_SNAPSHOT) |
| 106 | + results.append( |
| 107 | + check( |
| 108 | + "listen_SiteInfo unwraps the site_info document", |
| 109 | + received == [SITE_INFO_SNAPSHOT["site_info"]], |
| 110 | + f"got {received}", |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + # A live_status event for another site is not delivered. |
| 115 | + stream = make_stream() |
| 116 | + site = stream.get_energysite(SITE_A) |
| 117 | + received = [] |
| 118 | + site.listen_LiveStatus(received.append) |
| 119 | + dispatch(stream, OTHER_SITE_LIVE_STATUS) |
| 120 | + results.append( |
| 121 | + check( |
| 122 | + "a different site's live_status is filtered out", |
| 123 | + received == [], |
| 124 | + f"got {received}", |
| 125 | + ) |
| 126 | + ) |
| 127 | + |
| 128 | + # A site_info listener does not receive live_status events for the same site. |
| 129 | + stream = make_stream() |
| 130 | + site = stream.get_energysite(SITE_A) |
| 131 | + received = [] |
| 132 | + site.listen_SiteInfo(received.append) |
| 133 | + dispatch(stream, LIVE_STATUS_SNAPSHOT) |
| 134 | + results.append( |
| 135 | + check( |
| 136 | + "listen_SiteInfo ignores live_status events", |
| 137 | + received == [], |
| 138 | + f"got {received}", |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + # Unrelated account-wide events (e.g. credits) are not delivered to energy listeners. |
| 143 | + stream = make_stream() |
| 144 | + site = stream.get_energysite(SITE_A) |
| 145 | + live_received: list[dict[str, Any]] = [] |
| 146 | + info_received: list[dict[str, Any]] = [] |
| 147 | + site.listen_LiveStatus(live_received.append) |
| 148 | + site.listen_SiteInfo(info_received.append) |
| 149 | + dispatch(stream, CREDITS_EVENT) |
| 150 | + results.append( |
| 151 | + check( |
| 152 | + "credits events are not delivered to energy site listeners", |
| 153 | + live_received == [] and info_received == [], |
| 154 | + f"live={live_received} info={info_received}", |
| 155 | + ) |
| 156 | + ) |
| 157 | + |
| 158 | + # Removing a listener stops further delivery. |
| 159 | + stream = make_stream() |
| 160 | + site = stream.get_energysite(SITE_A) |
| 161 | + received = [] |
| 162 | + remove = site.listen_LiveStatus(received.append) |
| 163 | + dispatch(stream, LIVE_STATUS_SNAPSHOT) |
| 164 | + remove() |
| 165 | + dispatch(stream, LIVE_STATUS_LIVE) |
| 166 | + results.append( |
| 167 | + check( |
| 168 | + "removing a listener stops further delivery", |
| 169 | + received == [LIVE_STATUS_SNAPSHOT["live_status"]], |
| 170 | + f"got {received}", |
| 171 | + ) |
| 172 | + ) |
| 173 | + |
| 174 | + # get_energysite is idempotent per id, mirroring get_vehicle. |
| 175 | + stream = make_stream() |
| 176 | + results.append( |
| 177 | + check( |
| 178 | + "get_energysite returns the same instance for the same id", |
| 179 | + stream.get_energysite(SITE_A) is stream.get_energysite(SITE_A), |
| 180 | + ) |
| 181 | + ) |
| 182 | + results.append( |
| 183 | + check( |
| 184 | + "get_energysite normalizes int and str ids to the same instance", |
| 185 | + stream.get_energysite(12345) is stream.get_energysite("12345"), |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + print("-" * 72) |
| 190 | + print("ALL PASS" if all(results) else "FAILURES PRESENT") |
| 191 | + if not all(results): |
| 192 | + raise SystemExit(1) |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + main() |
0 commit comments