|
1 | 1 | """Tests for the DoNotDisturbTrait class.""" |
2 | 2 |
|
3 | | -from unittest.mock import AsyncMock |
| 3 | +from unittest.mock import AsyncMock, call |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
7 | 7 | from roborock.data import DnDTimer |
8 | 8 | from roborock.devices.device import RoborockDevice |
9 | 9 | from roborock.devices.traits.v1.do_not_disturb import DoNotDisturbTrait |
| 10 | +from roborock.exceptions import RoborockException |
10 | 11 | from roborock.roborock_typing import RoborockCommand |
11 | 12 |
|
12 | 13 |
|
@@ -74,27 +75,74 @@ async def test_set_dnd_timer_success( |
74 | 75 | dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock, sample_dnd_timer: DnDTimer |
75 | 76 | ) -> None: |
76 | 77 | """Test successfully setting DnD timer settings.""" |
| 78 | + mock_rpc_channel.send_command.side_effect = [ |
| 79 | + # Response for SET_DND_TIMER |
| 80 | + {}, |
| 81 | + # Response for GET_DND_TIMER after updating |
| 82 | + { |
| 83 | + "startHour": 22, |
| 84 | + "startMinute": 0, |
| 85 | + "endHour": 8, |
| 86 | + "endMinute": 0, |
| 87 | + "enabled": 1, |
| 88 | + }, |
| 89 | + ] |
| 90 | + |
77 | 91 | # Call the method |
78 | 92 | await dnd_trait.set_dnd_timer(sample_dnd_timer) |
79 | 93 |
|
80 | 94 | # Verify the RPC call was made correctly with dataclass converted to dict |
81 | 95 |
|
82 | 96 | expected_params = [22, 0, 8, 0] |
83 | | - mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.SET_DND_TIMER, params=expected_params) |
| 97 | + mock_rpc_channel.send_command.mock_calls = [ |
| 98 | + call(RoborockCommand.SET_DND_TIMER, params=expected_params), |
| 99 | + call(RoborockCommand.GET_DND_TIMER), |
| 100 | + ] |
| 101 | + |
| 102 | + # Verify the trait state is updated |
| 103 | + assert dnd_trait.enabled == 1 |
| 104 | + assert dnd_trait.is_on |
| 105 | + assert dnd_trait.start_hour == 22 |
| 106 | + assert dnd_trait.start_minute == 0 |
| 107 | + assert dnd_trait.end_hour == 8 |
| 108 | + assert dnd_trait.end_minute == 0 |
84 | 109 |
|
85 | 110 |
|
86 | 111 | async def test_clear_dnd_timer_success(dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock) -> None: |
87 | 112 | """Test successfully clearing DnD timer settings.""" |
| 113 | + mock_rpc_channel.send_command.side_effect = [ |
| 114 | + # Response for CLOSE_DND_TIMER |
| 115 | + {}, |
| 116 | + # Response for GET_DND_TIMER after clearing |
| 117 | + { |
| 118 | + "startHour": 0, |
| 119 | + "startMinute": 0, |
| 120 | + "endHour": 0, |
| 121 | + "endMinute": 0, |
| 122 | + "enabled": 0, |
| 123 | + }, |
| 124 | + ] |
| 125 | + |
88 | 126 | # Call the method |
89 | 127 | await dnd_trait.clear_dnd_timer() |
90 | 128 |
|
91 | 129 | # Verify the RPC call was made correctly |
92 | | - mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.CLOSE_DND_TIMER) |
| 130 | + mock_rpc_channel.send_command.mock_calls = [ |
| 131 | + call(RoborockCommand.CLOSE_DND_TIMER), |
| 132 | + call(RoborockCommand.GET_DND_TIMER), |
| 133 | + ] |
| 134 | + |
| 135 | + # Verify the trait state is updated |
| 136 | + assert dnd_trait.enabled == 0 |
| 137 | + assert not dnd_trait.is_on |
| 138 | + assert dnd_trait.start_hour == 0 |
| 139 | + assert dnd_trait.start_minute == 0 |
| 140 | + assert dnd_trait.end_hour == 0 |
| 141 | + assert dnd_trait.end_minute == 0 |
93 | 142 |
|
94 | 143 |
|
95 | 144 | async def test_get_dnd_timer_propagates_exception(dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock) -> None: |
96 | 145 | """Test that exceptions from RPC channel are propagated in get_dnd_timer.""" |
97 | | - from roborock.exceptions import RoborockException |
98 | 146 |
|
99 | 147 | # Setup mock to raise an exception |
100 | 148 | mock_rpc_channel.send_command.side_effect = RoborockException("Communication error") |
|
0 commit comments