Skip to content

Conversation

@allenporter
Copy link
Contributor

@allenporter allenporter commented Dec 6, 2025

Always start a local connection immediately to ensure initial RPCs can be sent locally. Without this, the first RPC may be sent over MQTT if the local connection didn't have a chance to start yet.

Issue #639

Always start a local connection immediately to ensure initial RPCs can be sent locally. Without this, the first RPC may be sent over MQTT if the local connection didn't have a chance to start yet.
Copilot AI review requested due to automatic review settings December 6, 2025 18:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR ensures that device connections are initiated immediately during device discovery, preventing the first RPC from being sent over MQTT before a local connection is established. The key change is converting start_connect() from a synchronous fire-and-forget method to an async method that waits for the initial connection attempt to begin.

  • Converted start_connect() to async with a 5-second timeout for initial connection attempts
  • Modified device manager to await all start_connect() tasks in parallel using asyncio.gather()
  • Simplified test to rely on synchronous behavior instead of polling for connection attempts

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
roborock/devices/device.py Converted start_connect() to async method with event-based waiting mechanism for initial connection attempt (with 5s timeout)
roborock/devices/device_manager.py Updated to collect and await start_connect() tasks for all devices using asyncio.gather()
tests/devices/test_device_manager.py Simplified test to assert immediate connection attempt instead of polling with sleep loop

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

new_devices[duid] = new_device

self._devices.update(new_devices)
await asyncio.gather(*start_tasks)
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The asyncio.gather(*start_tasks) call should include return_exceptions=True to prevent one device's connection failure from blocking the discovery of other devices. Without this parameter, if any start_connect() raises an exception that escapes the timeout, the entire discover_devices() operation will fail.

Consider changing this to:

await asyncio.gather(*start_tasks, return_exceptions=True)
Suggested change
await asyncio.gather(*start_tasks)
await asyncio.gather(*start_tasks, return_exceptions=True)

Copilot uses AI. Check for mistakes.
This will give a moment for the first connection attempt to start so
that the device will have connections established -- however, this will
never directly fail.
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace should be removed from this blank line.

Suggested change

Copilot uses AI. Check for mistakes.
start_attempt.set()

self._connect_task = asyncio.create_task(connect_loop())

Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace should be removed from this blank line.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines 156 to 157


Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace should be removed from this blank line.

Suggested change

Copilot uses AI. Check for mistakes.
device_products = home_data.device_products
_LOGGER.debug("Discovered %d devices %s", len(device_products), home_data)

# These are connected serially to avoid overwhelming the MQTT broker
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is now misleading. The code previously connected devices serially, but with the changes in this PR, devices now start connecting in parallel (via asyncio.gather() on line 94). The comment should be updated to reflect the new behavior, or removed if parallel connection is the intended behavior.

If overwhelming the MQTT broker is still a concern, consider updating the comment to explain why parallel connection is now acceptable, or implement a mechanism to limit concurrency (e.g., using a semaphore).

Suggested change
# These are connected serially to avoid overwhelming the MQTT broker

Copilot uses AI. Check for mistakes.
@allenporter allenporter merged commit 3c918ae into Python-roborock:main Dec 6, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants