Skip to content

Commit 9ba742f

Browse files
authored
fix: normalise site_id to str before matching energy-site events (#47)
The server's command-patch site_info event carries site_id as a JSON number while polled events carry a string; energysite.py's listener filters compared it via exact dict equality against str(site_id), silently dropping numeric events. Compare with str(x[site_id]) inside each listener instead of relying on the filter's exact match.
1 parent 6e2e3b6 commit 9ba742f

2 files changed

Lines changed: 113 additions & 19 deletions

File tree

‎teslemetry_stream/energysite.py‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ def listen_LiveStatus(
3636
`isCache` set, matching the same snapshot-then-live semantics as
3737
vehicle state.
3838
"""
39-
return self.stream.async_add_listener(
40-
lambda x: callback(x[Key.LIVE_STATUS]),
41-
{Key.SITE_ID: self.site_id, Key.LIVE_STATUS: None},
42-
)
39+
def _handler(x: dict[str, Any]) -> None:
40+
if str(x[Key.SITE_ID]) == self.site_id:
41+
callback(x[Key.LIVE_STATUS])
42+
43+
return self.stream.async_add_listener(_handler, {Key.LIVE_STATUS: None})
4344

4445
def listen_SiteInfo(
4546
self, callback: Callable[[dict[str, Any]], None]
@@ -54,10 +55,11 @@ def listen_SiteInfo(
5455
initial event is delivered with `isCache` set, matching the same
5556
snapshot-then-live semantics as vehicle state.
5657
"""
57-
return self.stream.async_add_listener(
58-
lambda x: callback(x[Key.SITE_INFO]),
59-
{Key.SITE_ID: self.site_id, Key.SITE_INFO: None},
60-
)
58+
def _handler(x: dict[str, Any]) -> None:
59+
if str(x[Key.SITE_ID]) == self.site_id:
60+
callback(x[Key.SITE_INFO])
61+
62+
return self.stream.async_add_listener(_handler, {Key.SITE_INFO: None})
6163

6264
def listen_TariffContentV2(
6365
self, callback: Callable[[dict[str, Any] | None], None]
@@ -69,10 +71,11 @@ def listen_TariffContentV2(
6971
site's V2 tariff was cleared). Published only when it changes -
7072
silence means no change, never staleness, matching `listen_SiteInfo`.
7173
"""
72-
return self.stream.async_add_listener(
73-
lambda x: callback(x[Key.TARIFF_CONTENT_V2]),
74-
{Key.SITE_ID: self.site_id, Key.TARIFF_CONTENT_V2: None},
75-
)
74+
def _handler(x: dict[str, Any]) -> None:
75+
if str(x[Key.SITE_ID]) == self.site_id:
76+
callback(x[Key.TARIFF_CONTENT_V2])
77+
78+
return self.stream.async_add_listener(_handler, {Key.TARIFF_CONTENT_V2: None})
7679

7780
def listen_EnergyTotals(
7881
self, callback: Callable[[EnergyHistoryTotals], None]
@@ -85,10 +88,8 @@ def listen_EnergyTotals(
8588
detects a change; silence between events means no change, never
8689
staleness. This listener only exposes the totals.
8790
"""
88-
return self.stream.async_add_listener(
89-
lambda x: callback(EnergyHistoryTotals.from_dict(x[Key.TOTALS])),
90-
{
91-
Key.ID: self.site_id,
92-
Key.TOTALS: None,
93-
},
94-
)
91+
def _handler(x: dict[str, Any]) -> None:
92+
if str(x[Key.ID]) == self.site_id:
93+
callback(EnergyHistoryTotals.from_dict(x[Key.TOTALS]))
94+
95+
return self.stream.async_add_listener(_handler, {Key.TOTALS: None})

‎tests/test_energysite_events.py‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@
5353
},
5454
}
5555

56+
LIVE_STATUS_NUMERIC_ID: dict[str, Any] = {
57+
**LIVE_STATUS_LIVE,
58+
"site_id": int(SITE_A),
59+
}
60+
61+
SITE_INFO_NUMERIC_ID: dict[str, Any] = {
62+
**SITE_INFO_SNAPSHOT,
63+
"site_id": int(SITE_A),
64+
}
65+
66+
TARIFF_CONTENT_V2_EVENT: dict[str, Any] = {
67+
"createdAt": "2026-07-28T10:16:00.000Z",
68+
"site_id": SITE_A,
69+
"tariff_content_v2": {"code": "TOU-1"},
70+
}
71+
72+
TARIFF_CONTENT_V2_NUMERIC_ID: dict[str, Any] = {
73+
**TARIFF_CONTENT_V2_EVENT,
74+
"site_id": int(SITE_A),
75+
}
76+
5677
OTHER_SITE_LIVE_STATUS: dict[str, Any] = {
5778
"createdAt": "2026-07-28T10:16:00.000Z",
5879
"site_id": SITE_B,
@@ -94,6 +115,11 @@
94115
"id": SITE_B,
95116
}
96117

118+
ENERGY_TOTALS_NUMERIC_ID: dict[str, Any] = {
119+
**ENERGY_TOTALS_EVENT,
120+
"id": int(SITE_A),
121+
}
122+
97123
CREDITS_EVENT: dict[str, Any] = {
98124
"credits": {"type": "snapshot", "cost": 0, "name": "snapshot", "balance": 5, "quota": {}},
99125
"createdAt": "2026-07-28T10:16:00.000Z",
@@ -252,6 +278,73 @@ def main() -> None:
252278
)
253279
)
254280

281+
# A numeric site_id (e.g. from a command-patch event) still matches str-typed listeners.
282+
stream = make_stream()
283+
site = stream.get_energysite(SITE_A)
284+
received = []
285+
site.listen_LiveStatus(received.append)
286+
dispatch(stream, LIVE_STATUS_NUMERIC_ID)
287+
results.append(
288+
check(
289+
"listen_LiveStatus matches a numeric site_id",
290+
received == [LIVE_STATUS_NUMERIC_ID["live_status"]],
291+
f"got {received}",
292+
)
293+
)
294+
295+
stream = make_stream()
296+
site = stream.get_energysite(SITE_A)
297+
received = []
298+
site.listen_SiteInfo(received.append)
299+
dispatch(stream, SITE_INFO_NUMERIC_ID)
300+
results.append(
301+
check(
302+
"listen_SiteInfo matches a numeric site_id",
303+
received == [SITE_INFO_NUMERIC_ID["site_info"]],
304+
f"got {received}",
305+
)
306+
)
307+
308+
stream = make_stream()
309+
site = stream.get_energysite(SITE_A)
310+
tariff_received: list[dict[str, Any] | None] = []
311+
site.listen_TariffContentV2(tariff_received.append)
312+
dispatch(stream, TARIFF_CONTENT_V2_NUMERIC_ID)
313+
results.append(
314+
check(
315+
"listen_TariffContentV2 matches a numeric site_id",
316+
tariff_received == [TARIFF_CONTENT_V2_NUMERIC_ID["tariff_content_v2"]],
317+
f"got {tariff_received}",
318+
)
319+
)
320+
321+
stream = make_stream()
322+
site = stream.get_energysite(SITE_A)
323+
totals_received = []
324+
site.listen_EnergyTotals(totals_received.append)
325+
dispatch(stream, ENERGY_TOTALS_NUMERIC_ID)
326+
results.append(
327+
check(
328+
"listen_EnergyTotals matches a numeric id",
329+
totals_received == [EnergyHistoryTotals(**ENERGY_TOTALS_FIXTURE)],
330+
f"got {totals_received}",
331+
)
332+
)
333+
334+
# A mismatched id (numeric or not) is still filtered out.
335+
stream = make_stream()
336+
site = stream.get_energysite(SITE_A)
337+
received = []
338+
site.listen_LiveStatus(received.append)
339+
dispatch(stream, {**LIVE_STATUS_NUMERIC_ID, "site_id": int(SITE_B)})
340+
results.append(
341+
check(
342+
"listen_LiveStatus still filters a mismatched numeric site_id",
343+
received == [],
344+
f"got {received}",
345+
)
346+
)
347+
255348
# get_energysite is idempotent per id, mirroring get_vehicle.
256349
stream = make_stream()
257350
results.append(

0 commit comments

Comments
 (0)