Skip to content

Conversation

@bdraco
Copy link
Member

@bdraco bdraco commented Dec 7, 2025

Proposed change

Keep a persistent BLE connection open between Shelly WiFi scan and provisioning steps during BLE-based device setup.

Some Shelly devices (particularly XSM boards) fail to re-establish the BLE connection once dropped, making persistent connections essential for reliable provisioning. Previously, the config flow created a new BLE connection for each operation (WiFi scan and WiFi provisioning), which caused these devices to fail during the second connection attempt.

This change:

  • Maintains a persistent RpcDevice instance across config flow steps
  • Uses the new wifi_scan() and extended wifi_setconfig() methods on RpcDevice instead of the helper functions that create their own connections
  • Properly cleans up the BLE connection after provisioning completes or when the flow is removed
  • Reuses the existing connection for the BLE fallback IP lookup

Type of change

  • Bugfix (non-breaking change which fixes an issue)

Additional information

Note: This PR requires aioshelly 13.23.0 (or newer) which adds wifi_scan() and extends wifi_setconfig() on RpcDevice. It should be merged after the aioshelly PR is merged and released.

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Dec 7, 2025

Hey there @bieniu, @thecode, @chemelli74, mind taking a look at this pull request as it has been labeled with an integration (shelly) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of shelly can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign shelly Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@bdraco bdraco marked this pull request as ready for review December 7, 2025 17:33
@bdraco bdraco requested a review from bieniu as a code owner December 7, 2025 17:33
Copilot AI review requested due to automatic review settings December 7, 2025 17:33
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 pull request maintains a persistent BLE connection during Shelly WiFi provisioning to fix an issue where some Shelly devices (particularly XSM boards) fail to re-establish BLE connections once dropped. Previously, the config flow created a new BLE connection for each operation (WiFi scan and provisioning), which caused certain devices to fail during the second connection attempt.

Key Changes:

  • Introduces persistent RpcDevice instance across config flow steps via _ble_rpc_device instance variable
  • Replaces helper functions (async_scan_wifi_networks, async_provision_wifi) with direct method calls on the persistent device (wifi_scan(), wifi_setconfig())
  • Implements proper cleanup via async_remove() callback and in provisioning completion paths

Reviewed changes

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

File Description
homeassistant/components/shelly/config_flow.py Added _ble_rpc_device instance variable and three new methods (_async_ensure_ble_connected, _async_disconnect_ble, _async_get_ip_from_ble) to manage persistent BLE connections; updated WiFi scan and provisioning steps to use persistent connection; added async_remove() callback for cleanup on flow abort
tests/components/shelly/test_config_flow.py Replaced old fixtures (mock_wifi_scan, mock_wifi_provision) with new fixtures (mock_ble_rpc_device, mock_ble_rpc_device_class) that mock the persistent RpcDevice; updated all BLE provisioning tests to use new mocking approach; added new test for cleanup verification; fixed incorrect await on sync async_abort() method

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@bdraco
Copy link
Member Author

bdraco commented Dec 7, 2025

Thanks. Still works fine

@bdraco bdraco merged commit ca31134 into dev Dec 7, 2025
36 checks passed
@bdraco bdraco deleted the shelly_ble_provision_keep_connection branch December 7, 2025 20:28
@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants