High availability Bluetooth
habluetooth is a Python library providing high-availability Bluetooth scanning and device management, built on top of Bleak. It aims to offer robust Bluetooth integration, often used in home automation contexts. The current version is 6.0.0. The project maintains an active and frequent release cadence, often with multiple updates per month, incorporating new features and bug fixes.
Warnings
- breaking Version 6.0.0 removes `BaseBleakScanner` inheritance from `HaBleakScannerWrapper` and the `register_detection_callback` method. Code relying on these old Bleak patterns will no longer work.
- gotcha The `habluetooth` library requires Python 3.11 or newer. Installations or attempts to run on older Python versions will fail or result in compatibility errors.
- gotcha habluetooth maintains a rapid release cadence, often publishing multiple versions within a month. While this indicates active development, it means minor API adjustments or bug fixes can land frequently.
Install
-
pip install habluetooth
Imports
- BluetoothManager
from habluetooth import BluetoothManager
- HaBleakScannerWrapper
from habluetooth import HaBleakScannerWrapper
Quickstart
import asyncio
from habluetooth import BluetoothManager, HaBleakScannerWrapper
async def main():
manager = BluetoothManager()
await manager.async_setup()
scanner = HaBleakScannerWrapper(manager)
print("Starting Bluetooth scan...")
async with scanner:
# Iterate over discovered devices and their advertisement data
async for device, advertisement_data in scanner.advertisement_data:
print(f"Device: {device.name or device.address}, RSSI: {advertisement_data.rssi}, Services: {advertisement_data.service_uuids}")
# Uncomment the line below to stop after the first detected device
# break
await manager.async_stop()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Scan interrupted by user.")