Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/rivian/rivian.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,8 +1176,17 @@ async def subscribe_for_vehicle_updates(
vehicle_id: str,
callback: Callable[[dict[str, Any]], None],
properties: set[str] | None = None,
*,
on_error: Callable[[dict[str, Any]], None] | None = None,
) -> Callable | None:
"""Open a web socket connection to receive updates."""
"""Open a web socket connection to receive updates.

`on_error` gets the subscription's error messages, such as a field that
fails GraphQL validation (the subscription then ends without data);
without it they're only logged at debug level. It can be called more than
once, including after each reconnect, which subscribes again, until the
subscription is unsubscribed.
"""
if not properties:
properties = VEHICLE_STATES_SUBSCRIPTION_PROPERTIES

Expand All @@ -1187,37 +1196,55 @@ async def subscribe_for_vehicle_updates(
"variables": {"vehicleID": vehicle_id},
}
return await self._start_subscription(
payload, callback, f"{vehicle_id} vehicle updates"
payload, callback, f"{vehicle_id} vehicle updates", on_error
)

async def subscribe_for_departure_schedules(
self, vehicle_id: str, callback: Callable[[dict[str, Any]], None]
self,
vehicle_id: str,
callback: Callable[[dict[str, Any]], None],
*,
on_error: Callable[[dict[str, Any]], None] | None = None,
) -> Callable[[], Awaitable[None]] | None:
"""Open a web socket connection to receive departure schedules.

The current schedules are sent right after subscribing.

`on_error` gets the subscription's error messages, such as a field that
fails GraphQL validation (the subscription then ends without data);
without it they're only logged at debug level. It can be called more than
once, including after each reconnect, which subscribes again, until the
subscription is unsubscribed.
"""
payload = {
"operationName": "VehicleDepartureSchedules",
"query": "subscription VehicleDepartureSchedules($vehicleId: String!) { vehicleDepartureSchedules(vehicleId: $vehicleId) { id name isEnabled occurrence { __typename ... on RepeatsWeekly { days startsAtMin skippedOn } } departureSettings { shouldOverrideChargeSchedule comfortSettings { cabinTempCelsius frontDefogDefrost surfaceHeatVentLevels { frontLeftSeat frontRightSeat rearLeftSeat rearRightSeat steeringWheel } } } } }",
"variables": {"vehicleId": vehicle_id},
}
return await self._start_subscription(
payload, callback, f"{vehicle_id} departure schedules"
payload, callback, f"{vehicle_id} departure schedules", on_error
)

async def subscribe_for_parallax_messages(
self,
vehicle_id: str,
callback: Callable[[dict[str, Any]], None],
rvms: list[str] | None = None,
*,
on_error: Callable[[dict[str, Any]], None] | None = None,
) -> Callable[[], Awaitable[None]] | None:
"""Open a web socket connection to receive Parallax message updates.

With `rvms` left as None, the subscription has no topic filter and
the server sends every RVM topic for the vehicle, including ones with
no decoder yet (`decode_parallax_message` returns None for those).
Pass a list, e.g. `PARALLAX_RVMS`, to only receive those topics.

`on_error` gets the subscription's error messages, such as a field that
fails GraphQL validation (the subscription then ends without data);
without it they're only logged at debug level. It can be called more than
once, including after each reconnect, which subscribes again, until the
subscription is unsubscribed.
"""
variables: dict[str, Any] = {"vehicleId": vehicle_id}
if rvms is not None:
Expand All @@ -1229,7 +1256,7 @@ async def subscribe_for_parallax_messages(
}
topics = "all Parallax RVMs" if rvms is None else f"{len(rvms)} Parallax RVMs"
return await self._start_subscription(
payload, callback, f"{vehicle_id} {topics}"
payload, callback, f"{vehicle_id} {topics}", on_error
)

async def subscribe_for_parallax_command_state(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_ws_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,31 @@ async def test_cancelled_subscribe_keeps_another_waiting_for_the_ack(
assert await waiting
assert monitor.connected
assert len(monitor._subscriptions) == 1


@pytest.mark.parametrize(
"subscribe",
[
lambda rivian, on_error: rivian.subscribe_for_vehicle_updates(
"id", lambda _: None, {"gnssLocation"}, on_error=on_error
),
lambda rivian, on_error: rivian.subscribe_for_departure_schedules(
"id", lambda _: None, on_error=on_error
),
lambda rivian, on_error: rivian.subscribe_for_parallax_messages(
"id", lambda _: None, on_error=on_error
),
],
ids=["vehicle updates", "departure schedules", "parallax messages"],
)
async def test_subscriptions_pass_errors_to_on_error(
server: FakeServer, rivian: Rivian, subscribe: Any
) -> None:
"""A subscription's error, e.g. a field failing validation, reaches on_error."""
server.error_on_subscribe = True
errors: list[dict[str, Any]] = []

assert await subscribe(rivian, errors.append)
await wait_for(lambda: errors)

assert errors[0]["type"] == "error"
Loading