Skip to content

Commit c4b0353

Browse files
committed
Simplify local connection handling
1 parent 0e9794d commit c4b0353

File tree

2 files changed

+13
-52
lines changed

2 files changed

+13
-52
lines changed

roborock/devices/v1_channel.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,12 @@ async def send_decoded_command(
148148
) -> _T:
149149
"""Send a command using the best available transport.
150150
151-
Will try local connection first if available and preferred,
152-
falling back to MQTT if needed.
151+
Will prefer local connection if available, falling back to MQTT.
153152
"""
154-
_LOGGER.debug("Sending command: %s, params=%s", method, params)
153+
connection = "local" if self.is_local_connected else "mqtt"
154+
_LOGGER.debug("Sending command (%s): %s, params=%s", connection, method, params)
155155
if self._local_channel:
156-
try:
157-
return await self._send_local_decoded_command(method, response_type=response_type, params=params)
158-
except RoborockException as e:
159-
_LOGGER.info("Local command failed, falling back to MQTT: %s", e)
160-
156+
return await self._send_local_decoded_command(method, response_type=response_type, params=params)
161157
return await self._send_mqtt_decoded_command(method, response_type=response_type, params=params)
162158

163159
async def _send_mqtt_raw_command(self, method: CommandType, params: ParamsType | None = None) -> dict[str, Any]:

tests/devices/test_v1_channel.py

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ async def test_v1_channel_send_decoded_command_local_preferred(
234234
assert result.state == RoborockStateCode.cleaning
235235

236236

237-
async def test_v1_channel_send_decoded_command_fallback_to_mqtt(
237+
async def test_v1_channel_send_decoded_command_local_fails(
238238
v1_channel: V1Channel,
239239
mock_mqtt_channel: Mock,
240240
mock_local_channel: Mock,
241241
mqtt_responses: list[RoborockMessage],
242242
) -> None:
243-
"""Test command sending falls back to MQTT when local fails."""
243+
"""Test case where sending with local connection fails."""
244244

245245
# Establish connections
246246
await v1_channel.subscribe(Mock())
@@ -251,15 +251,15 @@ async def test_v1_channel_send_decoded_command_fallback_to_mqtt(
251251

252252
# Send command
253253
mqtt_responses.append(TEST_RESPONSE)
254-
result = await v1_channel.send_decoded_command(
255-
RoborockCommand.CHANGE_SOUND_VOLUME,
256-
response_type=S5MaxStatus,
257-
)
254+
with pytest.raises(RoborockException, match="Local failed"):
255+
await v1_channel.send_decoded_command(
256+
RoborockCommand.CHANGE_SOUND_VOLUME,
257+
response_type=S5MaxStatus,
258+
)
258259

259-
# Verify both were attempted
260+
# Verify local was attempted but not mqtt
260261
mock_local_channel.send_command.assert_called_once()
261-
mock_mqtt_channel.send_command.assert_called_once()
262-
assert result.state == RoborockStateCode.cleaning
262+
mock_mqtt_channel.send_command.assert_not_called()
263263

264264

265265
async def test_v1_channel_send_decoded_command_mqtt_only(
@@ -531,38 +531,3 @@ async def test_v1_channel_full_subscribe_and_command_flow(
531531
unsub()
532532
mqtt_unsub.assert_called_once()
533533
local_unsub.assert_called_once()
534-
535-
536-
async def test_v1_channel_graceful_degradation_local_to_mqtt(
537-
mock_mqtt_channel: Mock,
538-
mock_local_session: Mock,
539-
mock_local_channel: Mock,
540-
mqtt_responses: list[RoborockMessage],
541-
) -> None:
542-
"""Test graceful degradation from local to MQTT during operation."""
543-
v1_channel = V1Channel(
544-
device_uid=TEST_DEVICE_UID,
545-
security_data=TEST_SECURITY_DATA,
546-
mqtt_channel=mock_mqtt_channel,
547-
local_session=mock_local_session,
548-
)
549-
550-
await v1_channel.subscribe(Mock())
551-
mock_mqtt_channel.send_command.reset_mock(return_value=False)
552-
553-
# First command: local works
554-
mock_local_channel.send_command.return_value = TEST_RESPONSE
555-
result1 = await v1_channel.send_decoded_command(RoborockCommand.GET_STATUS, response_type=S5MaxStatus)
556-
assert result1.state == RoborockStateCode.cleaning
557-
mock_local_channel.send_command.assert_called_once()
558-
mock_mqtt_channel.send_command.assert_not_called()
559-
560-
# Second command: local fails, falls back to MQTT
561-
mock_local_channel.send_command.side_effect = RoborockException("Local failed")
562-
mqtt_responses.append(TEST_RESPONSE)
563-
result2 = await v1_channel.send_decoded_command(RoborockCommand.GET_STATUS, response_type=S5MaxStatus)
564-
assert result2.state == RoborockStateCode.cleaning
565-
566-
# Verify both were attempted
567-
assert mock_local_channel.send_command.call_count == 2
568-
mock_mqtt_channel.send_command.assert_called_once()

0 commit comments

Comments
 (0)