From 4be7a10ed18072dea670c95f9f26bcb1c52b81f4 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 01:24:18 +0000 Subject: [PATCH 1/3] Check params in encode_vehicle_command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit encode_vehicle_command relied on callers having run Rivian.validate_vehicle_command first. Without it, some invalid params encoded into real commands (a charge limit of 150, 45 °C, defrost level "0" turning defrost on), others raised a raw TypeError or ValueError, and a missing param was reported as "has no Parallax equivalent". Each command now checks its params the way validate_vehicle_command does, and the broad KeyError catch is gone. Valid params encode exactly as before. Co-Authored-By: Claude Opus 5.5 --- src/rivian/parallax/vehicle_command.py | 52 ++++++++++++++++++-------- tests/parallax/test_vehicle_command.py | 12 +++++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/rivian/parallax/vehicle_command.py b/src/rivian/parallax/vehicle_command.py index dd8f862..eea2fe2 100644 --- a/src/rivian/parallax/vehicle_command.py +++ b/src/rivian/parallax/vehicle_command.py @@ -7,7 +7,7 @@ from __future__ import annotations -from collections.abc import Callable +from collections.abc import Callable, Container from typing import Any, Final from ..const import VehicleCommand @@ -62,6 +62,33 @@ _Encode = Callable[[dict[str, Any]], bytes] +def _int_param(params: dict[str, Any], key: str, valid: Container[int]) -> int: + """Return an int param, as `Rivian.validate_vehicle_command` accepts it.""" + value = params.get(key) + if isinstance(value, bool) or not isinstance(value, int) or value not in valid: + raise RivianBadRequestError(f"`{key}` {value!r} has no Parallax equivalent") + return value + + +def _level( + params: dict[str, Any], levels: dict[int, comfort_pb2.ConditioningLevel] +) -> comfort_pb2.ConditioningLevel: + return levels[_int_param(params, "level", levels)] + + +def _temperature(params: dict[str, Any]) -> float: + temp = params.get("HVAC_set_temp") + if ( + isinstance(temp, bool) + or not isinstance(temp, (float, int)) + or not (16 <= temp <= 29 or temp in (0, 63.5)) + ): + raise RivianBadRequestError( + f"`HVAC_set_temp` {temp!r} has no Parallax equivalent" + ) + return float(temp) + + def _seat( surface: comfort_pb2.CabinSurface, conditioning: comfort_pb2.ConditioningType = comfort_pb2.CONDITIONING_HEAT, @@ -69,7 +96,7 @@ def _seat( return ( "PVS_COMF_CMD", SET_CABIN_SURFACE_RVM, - lambda p: encode_cabin_surface(surface, conditioning, _SEAT_LEVELS[p["level"]]), + lambda p: encode_cabin_surface(surface, conditioning, _level(p, _SEAT_LEVELS)), ) @@ -150,12 +177,12 @@ def _seat( VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP: ( "PVS_COMF_CMD", SET_PRECONDITIONING_TEMPERATURE_RVM, - lambda p: encode_preconditioning_temperature(float(p["HVAC_set_temp"])), + lambda p: encode_preconditioning_temperature(_temperature(p)), ), VehicleCommand.CABIN_HVAC_DEFROST_DEFOG: ( "PVS_COMF_CMD", SET_DEFROST_DEFOG_RVM, - lambda p: encode_defrost_defog(bool(p["level"])), + lambda p: encode_defrost_defog(bool(_int_param(p, "level", range(5)))), ), VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT: _seat(comfort_pb2.SEAT_ROW_1_LEFT), VehicleCommand.CABIN_HVAC_RIGHT_SEAT_HEAT: _seat(comfort_pb2.SEAT_ROW_1_RIGHT), @@ -179,7 +206,7 @@ def _seat( lambda p: encode_cabin_surface( comfort_pb2.STEERING_WHEEL, comfort_pb2.CONDITIONING_HEAT, - _STEERING_WHEEL_LEVELS[p["level"]], + _level(p, _STEERING_WHEEL_LEVELS), ), ), VehicleCommand.START_CHARGING: ("PVS_ENRG_CMD", START_CHARGING_RVM, lambda _: b""), @@ -187,7 +214,7 @@ def _seat( VehicleCommand.CHARGING_LIMITS: ( "PVS_ENRG_CMD", SET_CHARGE_LIMIT_RVM, - lambda p: encode_charge_limit(p["SOC_limit"]), + lambda p: encode_charge_limit(_int_param(p, "SOC_limit", range(50, 101))), ), } @@ -206,16 +233,11 @@ def encode_vehicle_command( ) -> tuple[str, bytes]: """Return the RVM and payload for a command's Parallax equivalent. - `params` are those of `send_vehicle_command`, already checked with - `Rivian.validate_vehicle_command`. Seat level 1 and steering wheel level 4 - have no Parallax equivalent. + `params` are those of `send_vehicle_command`, checked as + `Rivian.validate_vehicle_command` checks them. Seat level 1 and steering + wheel level 4 have no Parallax equivalent. """ if not (entry := _COMMANDS.get(command)): raise RivianBadRequestError(f"{command} has no Parallax equivalent") _, rvm, encode = entry - try: - return rvm, encode(params or {}) - except KeyError as err: - raise RivianBadRequestError( - f"{command} {params} has no Parallax equivalent" - ) from err + return rvm, encode(params or {}) diff --git a/tests/parallax/test_vehicle_command.py b/tests/parallax/test_vehicle_command.py index d5cc718..cb0fe4a 100644 --- a/tests/parallax/test_vehicle_command.py +++ b/tests/parallax/test_vehicle_command.py @@ -117,9 +117,19 @@ def test_feature() -> None: (VehicleCommand.CABIN_HVAC_STEERING_HEAT, {"level": 4}), (VehicleCommand.CHARGING_LIMITS, None), (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {}), + # Params `Rivian.validate_vehicle_command` rejects + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 150}), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": "80"}), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 80.0}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 45}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": "22"}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": "0"}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": 7}), + (VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, {"level": 3.0}), + (VehicleCommand.CABIN_HVAC_STEERING_HEAT, {"level": True}), ], ) def test_no_equivalent(command: VehicleCommand, params: dict[str, Any] | None) -> None: - """Commands, levels or missing params with no Parallax equivalent are rejected.""" + """Commands, levels or params with no Parallax equivalent are rejected.""" with pytest.raises(RivianBadRequestError): encode_vehicle_command(command, params) From 5cf9bb2ea769585b7bc036c071fbc34e01628662 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 01:33:07 +0000 Subject: [PATCH 2/3] Share the vehicle command param rules The param rules were written out twice, in validate_vehicle_command and in encode_vehicle_command, and already differed on bool levels. They now live once in parallax/vehicle_command.py (check_charge_limit, check_hvac_level, check_temperature), and validate_vehicle_command calls them. Bool params are rejected on both paths. Errors now tell an invalid param ("must include parameter ...") apart from a valid level the Parallax command doesn't have ("SEAT_ROW_1_LEFT level 1 has no Parallax equivalent"). Tests cover the range edges and check that the validator and encoder accept the same params. The send_vehicle_command docstring now says HVAC_set_temp is a number, which is what the validator has always required. Co-Authored-By: Claude Opus 5.5 --- src/rivian/parallax/vehicle_command.py | 106 +++++++++++++++++-------- src/rivian/rivian.py | 53 ++++--------- tests/parallax/test_vehicle_command.py | 90 +++++++++++++++++---- 3 files changed, 165 insertions(+), 84 deletions(-) diff --git a/src/rivian/parallax/vehicle_command.py b/src/rivian/parallax/vehicle_command.py index eea2fe2..7e79d70 100644 --- a/src/rivian/parallax/vehicle_command.py +++ b/src/rivian/parallax/vehicle_command.py @@ -7,8 +7,8 @@ from __future__ import annotations -from collections.abc import Callable, Container -from typing import Any, Final +from collections.abc import Callable +from typing import Any, Final, TypeGuard from ..const import VehicleCommand from ..exceptions import RivianBadRequestError @@ -62,33 +62,72 @@ _Encode = Callable[[dict[str, Any]], bytes] -def _int_param(params: dict[str, Any], key: str, valid: Container[int]) -> int: - """Return an int param, as `Rivian.validate_vehicle_command` accepts it.""" - value = params.get(key) - if isinstance(value, bool) or not isinstance(value, int) or value not in valid: - raise RivianBadRequestError(f"`{key}` {value!r} has no Parallax equivalent") - return value +# Commands whose `level` param is 0..4: 0 is off, 1 is on, 2..4 are level_1..level_3 +HVAC_LEVEL_COMMANDS: Final[frozenset[str]] = frozenset( + { + VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, + VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, + VehicleCommand.CABIN_HVAC_LEFT_SEAT_VENT, + VehicleCommand.CABIN_HVAC_REAR_LEFT_SEAT_HEAT, + VehicleCommand.CABIN_HVAC_REAR_RIGHT_SEAT_HEAT, + VehicleCommand.CABIN_HVAC_RIGHT_SEAT_HEAT, + VehicleCommand.CABIN_HVAC_RIGHT_SEAT_VENT, + VehicleCommand.CABIN_HVAC_STEERING_HEAT, + VehicleCommand.CABIN_HVAC_3RD_ROW_REAR_LEFT_SEAT_HEAT, + VehicleCommand.CABIN_HVAC_3RD_ROW_REAR_RIGHT_SEAT_HEAT, + } +) -def _level( - params: dict[str, Any], levels: dict[int, comfort_pb2.ConditioningLevel] -) -> comfort_pb2.ConditioningLevel: - return levels[_int_param(params, "level", levels)] +def _is_int(value: Any) -> TypeGuard[int]: + return isinstance(value, int) and not isinstance(value, bool) + + +def check_charge_limit(params: dict[str, Any] | None) -> int: + """Return the `SOC_limit` param, or raise RivianBadRequestError if invalid.""" + limit = (params or {}).get("SOC_limit") + if not (_is_int(limit) and 50 <= limit <= 100): + raise RivianBadRequestError( + "Charging limit must include parameter `SOC_limit` with a valid value between 50 and 100" + ) + return limit + + +def check_hvac_level(params: dict[str, Any] | None) -> int: + """Return the `level` param, or raise RivianBadRequestError if invalid.""" + level = (params or {}).get("level") + if not (_is_int(level) and 0 <= level <= 4): + raise RivianBadRequestError( + "HVAC setting must include parameter `level` with a valid value between 0 and 4" + ) + return level -def _temperature(params: dict[str, Any]) -> float: - temp = params.get("HVAC_set_temp") - if ( - isinstance(temp, bool) - or not isinstance(temp, (float, int)) - or not (16 <= temp <= 29 or temp in (0, 63.5)) +def check_temperature(params: dict[str, Any] | None) -> float: + """Return the `HVAC_set_temp` param, or raise RivianBadRequestError if invalid.""" + temp = (params or {}).get("HVAC_set_temp") + if not ( + (_is_int(temp) or isinstance(temp, float)) + and (16 <= temp <= 29 or temp in (0, 63.5)) ): raise RivianBadRequestError( - f"`HVAC_set_temp` {temp!r} has no Parallax equivalent" + "HVAC setting must include parameter `HVAC_set_temp` with a valid value between 16 and 29 or 0/63.5 for LO/HI, respectively" ) return float(temp) +def _surface_level( + surface: comfort_pb2.CabinSurface, + params: dict[str, Any], + levels: dict[int, comfort_pb2.ConditioningLevel], +) -> comfort_pb2.ConditioningLevel: + if (level := check_hvac_level(params)) not in levels: + raise RivianBadRequestError( + f"{comfort_pb2.CabinSurface.Name(surface)} level {level} has no Parallax equivalent" + ) + return levels[level] + + def _seat( surface: comfort_pb2.CabinSurface, conditioning: comfort_pb2.ConditioningType = comfort_pb2.CONDITIONING_HEAT, @@ -96,7 +135,9 @@ def _seat( return ( "PVS_COMF_CMD", SET_CABIN_SURFACE_RVM, - lambda p: encode_cabin_surface(surface, conditioning, _level(p, _SEAT_LEVELS)), + lambda p: encode_cabin_surface( + surface, conditioning, _surface_level(surface, p, _SEAT_LEVELS) + ), ) @@ -177,28 +218,31 @@ def _seat( VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP: ( "PVS_COMF_CMD", SET_PRECONDITIONING_TEMPERATURE_RVM, - lambda p: encode_preconditioning_temperature(_temperature(p)), + lambda p: encode_preconditioning_temperature(check_temperature(p)), ), VehicleCommand.CABIN_HVAC_DEFROST_DEFOG: ( "PVS_COMF_CMD", SET_DEFROST_DEFOG_RVM, - lambda p: encode_defrost_defog(bool(_int_param(p, "level", range(5)))), + # Levels 1..4 all turn it on + lambda p: encode_defrost_defog(check_hvac_level(p) != 0), ), VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT: _seat(comfort_pb2.SEAT_ROW_1_LEFT), VehicleCommand.CABIN_HVAC_RIGHT_SEAT_HEAT: _seat(comfort_pb2.SEAT_ROW_1_RIGHT), VehicleCommand.CABIN_HVAC_LEFT_SEAT_VENT: _seat( - comfort_pb2.SEAT_ROW_1_LEFT, comfort_pb2.CONDITIONING_VENT + comfort_pb2.SEAT_ROW_1_LEFT, + comfort_pb2.CONDITIONING_VENT, ), VehicleCommand.CABIN_HVAC_RIGHT_SEAT_VENT: _seat( - comfort_pb2.SEAT_ROW_1_RIGHT, comfort_pb2.CONDITIONING_VENT + comfort_pb2.SEAT_ROW_1_RIGHT, + comfort_pb2.CONDITIONING_VENT, ), VehicleCommand.CABIN_HVAC_REAR_LEFT_SEAT_HEAT: _seat(comfort_pb2.SEAT_ROW_2_LEFT), VehicleCommand.CABIN_HVAC_REAR_RIGHT_SEAT_HEAT: _seat(comfort_pb2.SEAT_ROW_2_RIGHT), VehicleCommand.CABIN_HVAC_3RD_ROW_REAR_LEFT_SEAT_HEAT: _seat( - comfort_pb2.SEAT_ROW_3_LEFT + comfort_pb2.SEAT_ROW_3_LEFT, ), VehicleCommand.CABIN_HVAC_3RD_ROW_REAR_RIGHT_SEAT_HEAT: _seat( - comfort_pb2.SEAT_ROW_3_RIGHT + comfort_pb2.SEAT_ROW_3_RIGHT, ), VehicleCommand.CABIN_HVAC_STEERING_HEAT: ( "PVS_COMF_CMD", @@ -206,7 +250,7 @@ def _seat( lambda p: encode_cabin_surface( comfort_pb2.STEERING_WHEEL, comfort_pb2.CONDITIONING_HEAT, - _level(p, _STEERING_WHEEL_LEVELS), + _surface_level(comfort_pb2.STEERING_WHEEL, p, _STEERING_WHEEL_LEVELS), ), ), VehicleCommand.START_CHARGING: ("PVS_ENRG_CMD", START_CHARGING_RVM, lambda _: b""), @@ -214,7 +258,7 @@ def _seat( VehicleCommand.CHARGING_LIMITS: ( "PVS_ENRG_CMD", SET_CHARGE_LIMIT_RVM, - lambda p: encode_charge_limit(_int_param(p, "SOC_limit", range(50, 101))), + lambda p: encode_charge_limit(check_charge_limit(p)), ), } @@ -233,9 +277,9 @@ def encode_vehicle_command( ) -> tuple[str, bytes]: """Return the RVM and payload for a command's Parallax equivalent. - `params` are those of `send_vehicle_command`, checked as - `Rivian.validate_vehicle_command` checks them. Seat level 1 and steering - wheel level 4 have no 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. """ if not (entry := _COMMANDS.get(command)): raise RivianBadRequestError(f"{command} has no Parallax equivalent") diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index a2b35d4..d1c434a 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -43,6 +43,12 @@ encode_live_streaming, encode_vehicle_operation, ) +from .parallax.vehicle_command import ( + HVAC_LEVEL_COMMANDS, + check_charge_limit, + check_hvac_level, + check_temperature, +) from .utils import generate_vehicle_command_hmac, send_deprecation_warning from .ws_monitor import WebSocketMonitor @@ -956,44 +962,15 @@ def validate_vehicle_command( ) -> None: """Raise RivianBadRequestError if a command's params are invalid. - Shared by `send_vehicle_command` and callers sending its Parallax - equivalent (`parallax.encode_vehicle_command`). + Used by `send_vehicle_command`; `parallax.encode_vehicle_command` checks + its params with the same rules. """ - if command == VehicleCommand.CHARGING_LIMITS and not ( - params - and isinstance((limit := params.get("SOC_limit")), int) - and 50 <= limit <= 100 - ): - raise RivianBadRequestError( - "Charging limit must include parameter `SOC_limit` with a valid value between 50 and 100" - ) - if command in ( - VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, - VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, - VehicleCommand.CABIN_HVAC_LEFT_SEAT_VENT, - VehicleCommand.CABIN_HVAC_REAR_LEFT_SEAT_HEAT, - VehicleCommand.CABIN_HVAC_REAR_RIGHT_SEAT_HEAT, - VehicleCommand.CABIN_HVAC_RIGHT_SEAT_HEAT, - VehicleCommand.CABIN_HVAC_RIGHT_SEAT_VENT, - VehicleCommand.CABIN_HVAC_STEERING_HEAT, - VehicleCommand.CABIN_HVAC_3RD_ROW_REAR_LEFT_SEAT_HEAT, - VehicleCommand.CABIN_HVAC_3RD_ROW_REAR_RIGHT_SEAT_HEAT, - ) and not ( - params - and isinstance((level := params.get("level")), int) - and 0 <= level <= 4 - ): - raise RivianBadRequestError( - "HVAC setting must include parameter `level` with a valid value between 0 and 4" - ) - if command == VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP and not ( - params - and isinstance((temp := params.get("HVAC_set_temp")), (float, int)) - and (16 <= temp <= 29 or temp in (0, 63.5)) - ): - raise RivianBadRequestError( - "HVAC setting must include parameter `HVAC_set_temp` with a valid value between 16 and 29 or 0/63.5 for LO/HI, respectively" - ) + if command == VehicleCommand.CHARGING_LIMITS: + check_charge_limit(params) + if command in HVAC_LEVEL_COMMANDS: + check_hvac_level(params) + if command == VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP: + check_temperature(params) async def send_vehicle_command( self, @@ -1014,7 +991,7 @@ async def send_vehicle_command( Certain commands may require additional details via the `params` mapping. Some known examples include: - `CABIN_HVAC_*`: params = {"level": 0..4} where 0 is off, 1 is on, 2 is low/level_1, 3 is medium/level_2 and 4 is high/level_3 - - `CABIN_PRECONDITIONING_SET_TEMP`: params = {"HVAC_set_temp": "deg_C"} where `deg_C` is a string value between 16 and 29 or 0/63.5 for LO/HI, respectively + - `CABIN_PRECONDITIONING_SET_TEMP`: params = {"HVAC_set_temp": deg_C} where `deg_C` is a number between 16 and 29 or 0/63.5 for LO/HI, respectively - `CHARGING_LIMITS`: params = {"SOC_limit": 50..100} """ self.validate_vehicle_command(command, params) diff --git a/tests/parallax/test_vehicle_command.py b/tests/parallax/test_vehicle_command.py index cb0fe4a..e8ae865 100644 --- a/tests/parallax/test_vehicle_command.py +++ b/tests/parallax/test_vehicle_command.py @@ -6,9 +6,10 @@ import pytest -from rivian import VehicleCommand +from rivian import Rivian, VehicleCommand from rivian.exceptions import RivianBadRequestError from rivian.parallax import encode_vehicle_command, vehicle_command_feature +from rivian.parallax.vehicle_command import _COMMANDS @pytest.mark.parametrize( @@ -115,21 +116,80 @@ def test_feature() -> None: (VehicleCommand.WAKE_VEHICLE, None), (VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, {"level": 1}), (VehicleCommand.CABIN_HVAC_STEERING_HEAT, {"level": 4}), - (VehicleCommand.CHARGING_LIMITS, None), - (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {}), - # Params `Rivian.validate_vehicle_command` rejects - (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 150}), - (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": "80"}), - (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 80.0}), - (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 45}), - (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": "22"}), - (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": "0"}), - (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": 7}), - (VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, {"level": 3.0}), - (VehicleCommand.CABIN_HVAC_STEERING_HEAT, {"level": True}), ], ) def test_no_equivalent(command: VehicleCommand, params: dict[str, Any] | None) -> None: - """Commands, levels or params with no Parallax equivalent are rejected.""" - with pytest.raises(RivianBadRequestError): + """Valid commands and levels with no Parallax equivalent are rejected.""" + Rivian.validate_vehicle_command(command, params) + with pytest.raises(RivianBadRequestError, match="no Parallax equivalent"): encode_vehicle_command(command, params) + + +_INVALID: list[tuple[VehicleCommand, dict[str, Any] | None]] = [ + (VehicleCommand.CHARGING_LIMITS, None), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 49}), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 101}), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": "80"}), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 80.0}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 15.5}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 29.5}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": "22"}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": True}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": "0"}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": 5}), + (VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, {"level": 3.0}), + (VehicleCommand.CABIN_HVAC_STEERING_HEAT, {"level": True}), +] +_VALID: list[tuple[VehicleCommand, dict[str, Any] | None]] = [ + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 50}), + (VehicleCommand.CHARGING_LIMITS, {"SOC_limit": 100}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 16}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 29.0}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 0}), + (VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, {"HVAC_set_temp": 63.5}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": 0}), + (VehicleCommand.CABIN_HVAC_DEFROST_DEFOG, {"level": 4}), + (VehicleCommand.CABIN_HVAC_LEFT_SEAT_HEAT, {"level": 4}), + (VehicleCommand.CABIN_HVAC_STEERING_HEAT, {"level": 3}), +] + + +@pytest.mark.parametrize(("command", "params"), _INVALID) +def test_invalid_params(command: VehicleCommand, params: dict[str, Any] | None) -> None: + """Invalid params are rejected either way of sending.""" + with pytest.raises(RivianBadRequestError, match="must include parameter"): + Rivian.validate_vehicle_command(command, params) + with pytest.raises(RivianBadRequestError, match="must include parameter"): + encode_vehicle_command(command, params) + + +@pytest.mark.parametrize(("command", "params"), _VALID) +def test_valid_params(command: VehicleCommand, params: dict[str, Any] | None) -> None: + """Params at the edges of their ranges are accepted either way of sending.""" + Rivian.validate_vehicle_command(command, params) + encode_vehicle_command(command, params) + + +def test_validation_agrees() -> None: + """The validator and the encoder accept the same params for every command.""" + samples = [params for _, params in _INVALID + _VALID] + [ + {"level": level} for level in range(5) + ] + for command in _COMMANDS: + for params in samples: + try: + Rivian.validate_vehicle_command(command, params) + valid = True + except RivianBadRequestError: + valid = False + try: + encode_vehicle_command(command, params) + encoded = True + except RivianBadRequestError as err: + encoded = False + if valid: # only seat level 1 and steering wheel level 4 + assert "no Parallax equivalent" in str(err), (command, params) + if encoded: + assert valid, (command, params) From 235591216e37436e853bc3e633349b9009a78d37 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 01:38:15 +0000 Subject: [PATCH 3/3] Leave the caller's params alone in send_vehicle_command send_vehicle_command converted HVAC_set_temp to a string in the caller's dict, so sending the same params again failed validation, which only accepts a number. It now sends a copy. Co-Authored-By: Claude Opus 5.5 --- src/rivian/rivian.py | 3 ++- tests/test_rivian.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index d1c434a..1560f92 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -996,7 +996,8 @@ async def send_vehicle_command( """ self.validate_vehicle_command(command, params) if command == VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP and params: - params["HVAC_set_temp"] = str(params["HVAC_set_temp"]) + # A copy, so the caller's params can be sent again + params = params | {"HVAC_set_temp": str(params["HVAC_set_temp"])} command = str(command) timestamp = str(int(time.time())) diff --git a/tests/test_rivian.py b/tests/test_rivian.py index 7f364ce..dfefd9f 100644 --- a/tests/test_rivian.py +++ b/tests/test_rivian.py @@ -959,6 +959,36 @@ def test_validate_vehicle_command_keeps_params() -> None: assert params == {"HVAC_set_temp": 21} +async def test_send_vehicle_command_keeps_params( + aresponses: ResponsesMockServer, +) -> None: + """The temperature is sent as a string, and the caller's params can be sent again.""" + response = {"data": {"sendVehicleCommand": {"id": "command_id"}}} + requests = capture_requests(aresponses, response, response) + vehicle_key, private_key = generate_key_pair() + params = {"HVAC_set_temp": 21} + async with aiohttp.ClientSession(): + rivian = parallax_rivian() + for _ in range(2): + assert ( + await rivian.send_vehicle_command( + VehicleCommand.CABIN_PRECONDITIONING_SET_TEMP, + "vehicle_id", + "phone_id", + "identity_id", + vehicle_key, + private_key, + params=params, + ) + == "command_id" + ) + await rivian.close() + assert params == {"HVAC_set_temp": 21} + assert [r["variables"]["attrs"]["params"] for r in requests] == [ + {"HVAC_set_temp": "21"} + ] * 2 + + async def test_graphql_errors(aresponses: ResponsesMockServer) -> None: """Test GraphQL error responses.""" host = "rivian.com"