Skip to content

feat: structured BLE connection error handling - #67

Open
cinderblock wants to merge 1 commit into
Patrick762:mainfrom
cinderblock:feat/structured-ble-errors
Open

feat: structured BLE connection error handling#67
cinderblock wants to merge 1 commit into
Patrick762:mainfrom
cinderblock:feat/structured-ble-errors

Conversation

@cinderblock

Copy link
Copy Markdown

Summary

  • Adds custom exception hierarchy: DeviceNotFoundError, ConnectionFailedError, EncryptionHandshakeError (all extend BluettiBluetoothError)
  • Replaces generic return None error handling in DeviceReader, DeviceWriter, and DeviceRecognizer with stage-specific exceptions carrying actionable messages
  • Reduces establish_connection max_attempts from 10 to 3 so busy-device detection fails in ~10s instead of 60+

Why

When BLE communication fails, the library returns None with a vague "Timeout" log. Users (especially via Home Assistant) can't tell if the device is:

  1. Out of range / powered offDeviceNotFoundError: "Move closer to the device and ensure it is turned on."
  2. Busy (another client connected)ConnectionFailedError: "Another Bluetooth client (such as the Bluetti app) may already be connected."
  3. Connected but ECDH failedEncryptionHandshakeError: "This may indicate a library bug. Please open an issue."

Each case now raises a typed exception with a clear, actionable message. Callers can catch BluettiBluetoothError for uniform handling or catch specific subclasses.

Changes

File Change
bluetooth/exceptions.py New — exception hierarchy
bluetooth/device_reader.py Stage-specific try/except for scan → connect → notify → ECDH → data
bluetooth/device_writer.py Same pattern for the write path
bluetooth/device_recognizer.py Short-circuits on DeviceNotFoundError, continues on others
bluetooth/__init__.py Exports exceptions
__init__.py Exports exceptions

Test plan

  • All 73 existing tests pass (mock path bypasses scan/connect stages)
  • Exception hierarchy verified: all three subclass BluettiBluetoothError

🤖 Generated with Claude Code

Add custom exception hierarchy to distinguish between three failure modes:
- DeviceNotFoundError: device not in range or powered off
- ConnectionFailedError: device found but busy (another client connected)
- EncryptionHandshakeError: connected but ECDH failed (likely a bug —
  message directs users to open a GitHub issue)

Each exception carries an actionable message explaining what went wrong
and what the user should do about it.

Also reduces establish_connection max_attempts from 10 to 3 to fail
faster when the device is busy instead of wasting 60+ seconds retrying.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant