|
| 1 | +"""Tests for the CleaningFluidTrait class.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from roborock.data import RoborockDockCleaningFluidCode, RoborockDockTypeCode |
| 8 | +from roborock.devices.device import RoborockDevice |
| 9 | +from roborock.devices.traits.v1.cleaning_fluid import CleaningFluidTrait |
| 10 | +from roborock.exceptions import RoborockException, RoborockUnsupportedFeature |
| 11 | +from roborock.roborock_typing import RoborockCommand |
| 12 | +from tests.devices.traits.v1.helpers import dock_types_with_capability |
| 13 | + |
| 14 | +SAROS_10R_DOCK = RoborockDockTypeCode.shell_3s_dock |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(name="cleaning_fluid") |
| 18 | +def cleaning_fluid_trait( |
| 19 | + device: RoborockDevice, |
| 20 | + discover_features_fixture: None, |
| 21 | +) -> CleaningFluidTrait | None: |
| 22 | + """Create a CleaningFluidTrait instance with mocked dependencies.""" |
| 23 | + assert device.v1_properties |
| 24 | + return device.v1_properties.cleaning_fluid |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + ("dock_type_code"), |
| 29 | + dock_types_with_capability("is_clean_fluid_auto_delivery_supported"), |
| 30 | +) |
| 31 | +async def test_cleaning_fluid_available( |
| 32 | + cleaning_fluid: CleaningFluidTrait | None, |
| 33 | + dock_type_code: RoborockDockTypeCode, |
| 34 | +) -> None: |
| 35 | + """Test that the trait is available for docks with cleaning fluid auto-delivery.""" |
| 36 | + assert cleaning_fluid is not None |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + ("dock_type_code"), |
| 41 | + dock_types_with_capability("is_clean_fluid_auto_delivery_supported", expected=False), |
| 42 | +) |
| 43 | +async def test_unsupported_cleaning_fluid( |
| 44 | + cleaning_fluid: CleaningFluidTrait | None, |
| 45 | + dock_type_code: RoborockDockTypeCode, |
| 46 | +) -> None: |
| 47 | + """Test that the trait is not available for docks without cleaning fluid auto-delivery.""" |
| 48 | + assert cleaning_fluid is None |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize(("dock_type_code"), [SAROS_10R_DOCK]) |
| 52 | +@pytest.mark.parametrize( |
| 53 | + ("response", "expected"), |
| 54 | + [ |
| 55 | + ({"status": 1}, RoborockDockCleaningFluidCode.standard), # Saros 10R |
| 56 | + ({"status": 3}, RoborockDockCleaningFluidCode.large), |
| 57 | + ({"status": 0}, RoborockDockCleaningFluidCode.off), |
| 58 | + ({"status": 2}, RoborockDockCleaningFluidCode.unknown), |
| 59 | + ], |
| 60 | +) |
| 61 | +async def test_refresh_unknown_code( |
| 62 | + cleaning_fluid: CleaningFluidTrait | None, |
| 63 | + mock_rpc_channel: AsyncMock, |
| 64 | + dock_type_code: RoborockDockTypeCode, |
| 65 | + response: dict[str, int], |
| 66 | + expected: RoborockDockCleaningFluidCode, |
| 67 | +) -> None: |
| 68 | + """Test that refresh decodes known codes and tolerates unknown ones.""" |
| 69 | + assert cleaning_fluid is not None |
| 70 | + mock_rpc_channel.send_command.side_effect = [response] |
| 71 | + |
| 72 | + await cleaning_fluid.refresh() |
| 73 | + |
| 74 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_AUTO_DELIVERY_CLEANING_FLUID) |
| 75 | + assert cleaning_fluid.status == expected |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize(("dock_type_code"), [SAROS_10R_DOCK]) |
| 79 | +async def test_options( |
| 80 | + cleaning_fluid: CleaningFluidTrait | None, |
| 81 | + dock_type_code: RoborockDockTypeCode, |
| 82 | +) -> None: |
| 83 | + """Test the cleaning fluid amounts offered by the app.""" |
| 84 | + assert cleaning_fluid is not None |
| 85 | + assert cleaning_fluid.cleaning_fluid_options == [ |
| 86 | + RoborockDockCleaningFluidCode.off, |
| 87 | + RoborockDockCleaningFluidCode.standard, |
| 88 | + RoborockDockCleaningFluidCode.large, |
| 89 | + ] |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize(("dock_type_code"), [SAROS_10R_DOCK]) |
| 93 | +@pytest.mark.parametrize( |
| 94 | + ("mode", "expected_code"), |
| 95 | + [ |
| 96 | + (RoborockDockCleaningFluidCode.off, 0), |
| 97 | + (RoborockDockCleaningFluidCode.standard, 1), |
| 98 | + (RoborockDockCleaningFluidCode.large, 3), |
| 99 | + ], |
| 100 | +) |
| 101 | +async def test_set_cleaning_fluid( |
| 102 | + cleaning_fluid: CleaningFluidTrait | None, |
| 103 | + mock_rpc_channel: AsyncMock, |
| 104 | + dock_type_code: RoborockDockTypeCode, |
| 105 | + mode: RoborockDockCleaningFluidCode, |
| 106 | + expected_code: int, |
| 107 | +) -> None: |
| 108 | + """Test setting an offered cleaning fluid amount sends its wire code.""" |
| 109 | + assert cleaning_fluid is not None |
| 110 | + |
| 111 | + await cleaning_fluid.set_cleaning_fluid(mode) |
| 112 | + |
| 113 | + mock_rpc_channel.send_command.assert_called_once_with( |
| 114 | + RoborockCommand.SET_AUTO_DELIVERY_CLEANING_FLUID, params={"status": expected_code} |
| 115 | + ) |
| 116 | + assert cleaning_fluid.status == mode |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize(("dock_type_code"), [SAROS_10R_DOCK]) |
| 120 | +async def test_set_rejects_unoffered( |
| 121 | + cleaning_fluid: CleaningFluidTrait | None, |
| 122 | + mock_rpc_channel: AsyncMock, |
| 123 | + dock_type_code: RoborockDockTypeCode, |
| 124 | +) -> None: |
| 125 | + """Test that an amount the app does not offer is rejected without sending.""" |
| 126 | + assert cleaning_fluid is not None |
| 127 | + |
| 128 | + with pytest.raises(RoborockUnsupportedFeature): |
| 129 | + await cleaning_fluid.set_cleaning_fluid(RoborockDockCleaningFluidCode.unknown) |
| 130 | + |
| 131 | + mock_rpc_channel.send_command.assert_not_called() |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize(("dock_type_code"), [SAROS_10R_DOCK]) |
| 135 | +async def test_set_failure_keeps_state( |
| 136 | + cleaning_fluid: CleaningFluidTrait | None, |
| 137 | + mock_rpc_channel: AsyncMock, |
| 138 | + dock_type_code: RoborockDockTypeCode, |
| 139 | +) -> None: |
| 140 | + """Test that a failed command leaves the amount unchanged.""" |
| 141 | + assert cleaning_fluid is not None |
| 142 | + cleaning_fluid.status = RoborockDockCleaningFluidCode.standard |
| 143 | + mock_rpc_channel.send_command.side_effect = RoborockException("boom") |
| 144 | + |
| 145 | + with pytest.raises(RoborockException): |
| 146 | + await cleaning_fluid.set_cleaning_fluid(RoborockDockCleaningFluidCode.large) |
| 147 | + |
| 148 | + assert cleaning_fluid.status == RoborockDockCleaningFluidCode.standard |
0 commit comments