Skip to content

Pass subscription errors to an on_error callback - #249

Merged
natekspencer merged 2 commits into
bretterer:mainfrom
walthowd:feat/subscription-errors
Nov 15, 2026
Merged

natekspencer merged 2 commits into
bretterer:mainfrom
walthowd:feat/subscription-errors

Conversation

@walthowd

@walthowd walthowd commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

When a subscription fails (e.g. the vehicle refuses a command it's waiting on), the server sends an error message that was only logged at debug, so callers could only time out. Subscriptions now take an optional on_error callback. Existing callers are unchanged.

walthowd added a commit to walthowd/rivian-python-client that referenced this pull request Nov 14, 2026
`start_gear_guard_live_stream` starts a live camera stream the way the
Rivian app does and returns its Kinesis Video WebRTC config (signaling
endpoint, ICE servers and role), for a viewer to join:

- It subscribes to `gearGuardLiveConfig` with a fresh command id, then sends
  the signed `security.video_monitoring.start_live_streaming` command with
  that same id; the cloud routes the config by it.
- `parallax.encode_live_streaming` builds the command's payload from a new
  `LiveStreamingRequest` schema; `GEAR_GUARD_CAMERAS` names the cameras.
- A refused start (e.g. a camera the vehicle doesn't have) raises
  RivianApiException straight away, through the subscription's `on_error`.

It needs Gear Guard video monitoring on and a paired phone. The vehicle
serves one session at a time, about 3 minutes each and an hour a day.

Builds on bretterer#249.

Verified against a 2026 R1S (Gen 2) running 2026.36.01: the config arrived
within seconds of the start when the vehicle was awake, and the front and
right cameras streamed in a WebRTC viewer. A sleeping vehicle can take
longer to wake than the default 40 second timeout. Not tested: the rear,
left and bed cameras, the R2's cabin camera.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxWHBTrHU6eTPotwMWgtJF
@walthowd
walthowd force-pushed the feat/subscription-errors branch from 4c27ea0 to 3adad09 Compare November 14, 2026 22:53

@natekspencer natekspencer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a useful hook and existing callers are unaffected. One change before merging:

on_error isn't guarded like the next callbacks. In _receiver, the next callback runs inside try/except Exception so a failing callback doesn't take the connection down, but subscription[2](data) is called bare. If it raises, the exception escapes the receive loop and the receiver task dies. _monitor replaces it about a second later without reading the exception, so it surfaces as "Task exception was never retrieved". #250's handler can hit this: it indexes errors[0], which raises if the server ever sends the error payload as an object instead of a list. Wrapping the call the same way (and supporting a coroutine on_error like next does, if you want them symmetric) would fix it:

try:
    if inspect.iscoroutinefunction(on_error):
        await on_error(data)
    else:
        on_error(data)
except Exception:  # pylint: disable=broad-except
    _LOGGER.exception("Error in a subscription error callback")

A test with an on_error that raises, checking the connection stays up, would cover it.

@walthowd

Copy link
Copy Markdown
Contributor Author

Good catch, fixed. next and on_error now both go through one _run_callback helper, so a failing on_error gets logged instead of killing the receiver, and coroutines work for both. Kept one log line for both since the traceback shows which callback it was. Added a test where on_error raises and a later error still comes through on the same connection.

@natekspencer

Copy link
Copy Markdown
Collaborator

Thanks, the _run_callback fix looks good. #252, #246 and #248 have been merged, so this now conflicts with main (tests/test_ws_monitor.py). Could you rebase onto main and resolve the conflicts?

They will land in this order, so rebasing in the same order keeps the later ones from conflicting again:

  1. Pass subscription errors to an on_error callback #249 Pass subscription errors to an on_error callback
  2. Add starting a Gear Guard live stream #250 Add starting a Gear Guard live stream
  3. Add searching for places #251 Add searching for places
  4. Add climate hold and pet mode commands #247 Add climate hold and pet mode commands
  5. Add Parallax equivalents of vehicle commands #245 Add Parallax equivalents of vehicle commands

I've moved this to draft for now; please mark it ready for review once it's rebased.

The server ends a subscription with an `error` message when it fails, for
example when the vehicle refuses a command the subscription waits on. The
web socket only logged those at debug, so a caller waiting on the
subscription could only time out. Subscriptions can now take an `on_error`
callback that gets the error message; existing callers are unchanged.
An `on_error` that raised escaped the receive loop and ended the receiver
task. Both callbacks now run through `_run_callback`, which logs a failure
instead of raising and awaits coroutine callbacks.
@walthowd
walthowd force-pushed the feat/subscription-errors branch from 4b2e11d to e8fe7d9 Compare November 15, 2026 20:31
walthowd added a commit to walthowd/rivian-python-client that referenced this pull request Nov 15, 2026
`start_gear_guard_live_stream` starts a live camera stream the way the
Rivian app does and returns its Kinesis Video WebRTC config (signaling
endpoint, ICE servers and role), for a viewer to join:

- It subscribes to `gearGuardLiveConfig` with a fresh command id, then sends
  the signed `security.video_monitoring.start_live_streaming` command with
  that same id; the cloud routes the config by it.
- `parallax.encode_live_streaming` builds the command's payload from a new
  `LiveStreamingRequest` schema; `GEAR_GUARD_CAMERAS` names the cameras.
- A refused start (e.g. a camera the vehicle doesn't have) raises
  RivianApiException straight away, through the subscription's `on_error`.

It needs Gear Guard video monitoring on and a paired phone. The vehicle
serves one session at a time, about 3 minutes each and an hour a day.

Builds on bretterer#249.

Verified against a 2026 R1S (Gen 2) running 2026.36.01: the config arrived
within seconds of the start when the vehicle was awake, and the front and
right cameras streamed in a WebRTC viewer. A sleeping vehicle can take
longer to wake than the default 40 second timeout. Not tested: the rear,
left and bed cameras, the R2's cabin camera.
@walthowd
walthowd marked this pull request as ready for review November 15, 2026 20:31
@walthowd

Copy link
Copy Markdown
Contributor Author

Rebased onto main, ready for review.

@natekspencer natekspencer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the shared _run_callback guard and the new test look good.

@natekspencer
natekspencer merged commit 27185aa into bretterer:main Nov 15, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants