collect: fix stale vehicle ref after token re-login; increase update verbosity
After a full re-login (doLogin), the library rebuilds its vehicle list with new objects. The old vehicle reference in the collect loop was never updated again, causing data to silently freeze. Re-select the vehicle from wc.vehicles after every update() call to stay on the current object. Also: update() now returns and logs its bool result; the record log line shows which top-level keys changed each cycle (or "none" when the backend returned identical data). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+20
-2
@@ -84,7 +84,19 @@ def run(args: argparse.Namespace) -> None:
|
|||||||
log.info("Midnight UTC rotation → %s", out)
|
log.info("Midnight UTC rotation → %s", out)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
we_connect.update(wc)
|
ok = we_connect.update(wc)
|
||||||
|
# Re-select vehicle after every update: if doLogin() was triggered
|
||||||
|
# (rare token-expiry full re-login), the library rebuilds its vehicle
|
||||||
|
# list with new objects, making our old reference permanently stale.
|
||||||
|
vehicle, err = we_connect.select_vehicle(wc, args.vin)
|
||||||
|
if err:
|
||||||
|
log.warning("Vehicle re-selection after update failed: %s", err)
|
||||||
|
continue
|
||||||
|
log.debug(
|
||||||
|
"update ok=%s vehicle._states keys: %s",
|
||||||
|
ok,
|
||||||
|
list(vehicle.attrs.keys()),
|
||||||
|
)
|
||||||
snapshot = extract_all(vehicle)
|
snapshot = extract_all(vehicle)
|
||||||
if snapshot is None:
|
if snapshot is None:
|
||||||
log.warning("extract_all returned None — skipping this cycle")
|
log.warning("extract_all returned None — skipping this cycle")
|
||||||
@@ -95,7 +107,13 @@ def run(args: argparse.Namespace) -> None:
|
|||||||
payload = jay_diff_full(last_broadcast, snapshot, combine_upd_add=True)
|
payload = jay_diff_full(last_broadcast, snapshot, combine_upd_add=True)
|
||||||
last_broadcast = snapshot
|
last_broadcast = snapshot
|
||||||
network.broadcast(payload, push_clients, push_lock)
|
network.broadcast(payload, push_clients, push_lock)
|
||||||
log.info("Record #%d saved at %s", len(store["records"]), snapshot["ts"])
|
changed = [k for k in payload if k != "ts"]
|
||||||
|
log.info(
|
||||||
|
"Record #%d at %s changed=%s",
|
||||||
|
len(store["records"]),
|
||||||
|
snapshot["ts"],
|
||||||
|
changed or "none",
|
||||||
|
)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
raise
|
raise
|
||||||
except AuthenticationError:
|
except AuthenticationError:
|
||||||
|
|||||||
@@ -60,5 +60,11 @@ def select_vehicle(wc: _Session, vin: str | None):
|
|||||||
return vehicles[0], None
|
return vehicles[0], None
|
||||||
|
|
||||||
|
|
||||||
def update(wc: _Session) -> None:
|
def update(wc: _Session) -> bool:
|
||||||
wc.run(wc.connection.update())
|
"""Run one update cycle. Returns True if the library reported success."""
|
||||||
|
ok = wc.run(wc.connection.update())
|
||||||
|
if not ok:
|
||||||
|
log.warning("connection.update() returned False — data may not have refreshed")
|
||||||
|
else:
|
||||||
|
log.debug("connection.update() succeeded")
|
||||||
|
return bool(ok)
|
||||||
|
|||||||
Reference in New Issue
Block a user