Persistent BLE connection for instant writes and original app compatibility #60
Open
defire04 wants to merge 5 commits intoPatrick762:mainfrom
Open
Persistent BLE connection for instant writes and original app compatibility #60defire04 wants to merge 5 commits intoPatrick762:mainfrom
defire04 wants to merge 5 commits intoPatrick762:mainfrom
Conversation
…Event for handshake
… support shared BLE sessions
…ter with shared BLE session
This was referenced Mar 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Building on #59, I tested switches and selects on my AC180P. The feature worked, but every toggle took 5–14 seconds:
establish_connectionasyncio.sleep(2)The root cause:
DeviceWriteropened a fresh BLE connection and ran the full ECDH handshake for every single write command, then disconnected.Holding the connection open was the obvious fix — but it revealed a second problem: the device supports only one BLE connection at a time, so the official Bluetti app could not connect while Home Assistant held the link.
What changed
bluetti_bt_lib/bluetooth/device_connection.py(new)A new
DeviceConnectionclass that owns a singleBleakClientand aBluettiEncryptionsession shared betweenDeviceReaderandDeviceWriter.The connection lifecycle:
connect()— scans for the device, establishes the BLE linkasyncio.Eventconnect()returnsTrue— reader/writer can now use the shared sessiondisconnect()— unsubscribes, resets encryption state, closes the linkThe single notification handler routes messages internally:
_handle_pre_key_message()(duringconnect())_handle_encrypted_key_message()(duringconnect())_dispatch_data()→ forwarded viaset_data_callback()bluetti_bt_lib/bluetooth/device_writer.pyAdded
connection: DeviceConnection | None = Noneparameter. When provided, the writer skipsestablish_connection, skips the ECDH handshake (already done), usesconnection.encryptionfor AES encryption, and does not disconnect on cleanup — the shared connection stays alive.When
connection=Nonethe behaviour is identical to before.bluetti_bt_lib/bluetooth/device_reader.pyAdded
connection: DeviceConnection | None = Noneparameter. When provided:BleakClientinstead of opening its ownconnection.set_data_callback()instead of subscribing to NOTIFY_UUIDkeep_alive_secondsfromDeviceReaderConfig(default0). This keeps the BLE session alive briefly so writes arriving within that window are instant, then frees the slot so the original Bluetti app can connectWhen
connection=Nonethe behaviour is identical to before.bluetti_bt_lib/__init__.py/bluetti_bt_lib/bluetooth/__init__.pyAdded
DeviceConnectionto the top-level package exports.Write latency after this change
Tested on
Bluetti AC180P via ESPHome BLE proxy
AC output, DC output, Power Lifting, Charging Mode — all confirmed working
The HA integration side (coordinator wiring, shared lock, write_pending guard, deferred disconnect) is in a test branch:
defire04/hassio-bluetti-bt @ test-encrypted-writes-and-persistent-ble-connection
Notes
This PR builds on #59 — the encryption handshake implementation there is unchanged;
DeviceConnectionreuses it and lifts it out ofDeviceWriterso both reader and writer can share the session.