diff --git a/src/rivian/parallax/__init__.py b/src/rivian/parallax/__init__.py index 179c169..2fe8dd5 100644 --- a/src/rivian/parallax/__init__.py +++ b/src/rivian/parallax/__init__.py @@ -32,7 +32,7 @@ vehicle_command_feature(command) -> str | None The supported feature that, when AVAILABLE, sends a `VehicleCommand` over Parallax. - encode_vehicle_command(command, params=None) -> (rvm, payload) + encode_vehicle_command(command, params=None, *, model=None) -> (rvm, payload) The command's Parallax equivalent, for `send_parallax_command`. Layout: diff --git a/src/rivian/parallax/vehicle_command.py b/src/rivian/parallax/vehicle_command.py index 7e79d70..cfb3ee9 100644 --- a/src/rivian/parallax/vehicle_command.py +++ b/src/rivian/parallax/vehicle_command.py @@ -173,10 +173,17 @@ def _seat( CLOSE_CLOSURES_RVM, lambda _: encode_closures(body_pb2.FRUNK), ), - VehicleCommand.OPEN_LIFTGATE_UNLATCH_TAILGATE: ( + # The vehicle reports a timeout if the released bin doesn't clear its latch, + # though body.closures.states then reports it open + VehicleCommand.RELEASE_LEFT_SIDE_BIN: ( "PVS_BD_CMD", OPEN_CLOSURES_RVM, - lambda _: encode_closures(body_pb2.LIFTGATE), + lambda _: encode_closures(body_pb2.SIDE_BIN_LEFT), + ), + VehicleCommand.RELEASE_RIGHT_SIDE_BIN: ( + "PVS_BD_CMD", + OPEN_CLOSURES_RVM, + lambda _: encode_closures(body_pb2.SIDE_BIN_RIGHT), ), VehicleCommand.CLOSE_LIFTGATE: ( "PVS_BD_CMD", @@ -263,24 +270,47 @@ def _seat( } +# Commands that open a closure depending on the model, like the Rivian app's: +# command -> (closure on an R1T, closure on any other model) +_BY_MODEL: Final[dict[str, tuple[body_pb2.ClosureId, body_pb2.ClosureId]]] = { + VehicleCommand.OPEN_LIFTGATE_UNLATCH_TAILGATE: ( + body_pb2.TAILGATE, + body_pb2.LIFTGATE, + ), +} + + def vehicle_command_feature(command: VehicleCommand | str) -> str | None: """Return the supported feature that sends a command over Parallax. None if the command has no Parallax equivalent, e.g. honk or wake. """ + if command in _BY_MODEL: + return "PVS_BD_CMD" return entry[0] if (entry := _COMMANDS.get(command)) else None def encode_vehicle_command( command: VehicleCommand | str, params: dict[str, Any] | None = None, + *, + model: str | None = None, ) -> tuple[str, bytes]: """Return the RVM and payload for a command's Parallax equivalent. `params` are those of `send_vehicle_command`, checked with the same rules as `Rivian.validate_vehicle_command`. Seat level 1 and steering wheel level 4 have no Parallax equivalent. + + `model` is the vehicle's model (e.g. "R1T"), needed for + `OPEN_LIFTGATE_UNLATCH_TAILGATE`: like the Rivian app, it opens an R1T's + tailgate and any other model's liftgate. """ + if closures := _BY_MODEL.get(command): + if not model: + raise RivianBadRequestError(f"{command} needs the vehicle's `model`") + closure = closures[0] if model.upper() == "R1T" else closures[1] + return OPEN_CLOSURES_RVM, encode_closures(closure) if not (entry := _COMMANDS.get(command)): raise RivianBadRequestError(f"{command} has no Parallax equivalent") _, rvm, encode = entry diff --git a/tests/parallax/test_vehicle_command.py b/tests/parallax/test_vehicle_command.py index e8ae865..7464b1e 100644 --- a/tests/parallax/test_vehicle_command.py +++ b/tests/parallax/test_vehicle_command.py @@ -35,10 +35,16 @@ b"\x0a\x01\x05", ), ( - VehicleCommand.OPEN_LIFTGATE_UNLATCH_TAILGATE, + VehicleCommand.RELEASE_LEFT_SIDE_BIN, None, "body.closures.open", - b"\x0a\x01\x07", + b"\x0a\x01\x08", + ), + ( + VehicleCommand.RELEASE_RIGHT_SIDE_BIN, + None, + "body.closures.open", + b"\x0a\x01\x09", ), (VehicleCommand.PANIC_ON, None, "security.alarm.enable_panic", b""), ( @@ -95,6 +101,28 @@ def test_encode( assert encode_vehicle_command(command, params) == (rvm, payload) +@pytest.mark.parametrize( + ("model", "payload"), + [ + ("R1T", b"\x0a\x01\x06"), # tailgate + ("r1t", b"\x0a\x01\x06"), + ("R1S", b"\x0a\x01\x07"), # liftgate + ("R2", b"\x0a\x01\x07"), + ], +) +def test_open_liftgate_unlatch_tailgate(model: str, payload: bytes) -> None: + """Like the Rivian app, it opens an R1T's tailgate and any other liftgate.""" + assert encode_vehicle_command( + VehicleCommand.OPEN_LIFTGATE_UNLATCH_TAILGATE, model=model + ) == ("body.closures.open", payload) + + +def test_open_liftgate_unlatch_tailgate_needs_model() -> None: + """Without the model, it's unknown which one to open.""" + with pytest.raises(RivianBadRequestError, match="model"): + encode_vehicle_command(VehicleCommand.OPEN_LIFTGATE_UNLATCH_TAILGATE) + + def test_feature() -> None: """Each command is sent over Parallax per its category's feature.""" assert vehicle_command_feature(VehicleCommand.UNLOCK_ALL_CLOSURES) == "PVS_BD_CMD" @@ -106,6 +134,11 @@ def test_feature() -> None: assert vehicle_command_feature(VehicleCommand.START_CHARGING) == "PVS_ENRG_CMD" # Like the app: LIFTGATE_CMD only shows the liftgate controls assert vehicle_command_feature(VehicleCommand.CLOSE_LIFTGATE) == "PVS_BD_CMD" + assert ( + vehicle_command_feature(VehicleCommand.OPEN_LIFTGATE_UNLATCH_TAILGATE) + == "PVS_BD_CMD" + ) + assert vehicle_command_feature(VehicleCommand.RELEASE_LEFT_SIDE_BIN) == "PVS_BD_CMD" assert vehicle_command_feature(VehicleCommand.WAKE_VEHICLE) is None assert vehicle_command_feature("NOT_A_COMMAND") is None