获取蓝牙数据
选择数据获取方式
如果设备主要通过 Bluetooth 广播来通知更新,且其核心功能是传感器、二进制传感器或触发事件:
- 如果所有传感器都通过 Bluetooth 广播更新:
PassiveBluetoothProcessorCoordinator - 如果部分传感器需要主动连接:
ActiveBluetoothProcessorCoordinator
如果设备主要通过 Bluetooth 广播来通知更新,但其核心功能不是传感器、二进制传感器或触发事件:
- 如果所有实体都通过 Bluetooth 广播更新:
PassiveBluetoothCoordinator - 如果需要主动连接:
ActiveBluetoothDataUpdateCoordinator
如果你的设备仅通过 Bluetooth 连接通信,而不使用 Bluetooth 广播:
Bluetooth 处理器协调器
ActiveBluetoothProcessorCoordinator 和 PassiveBluetoothProcessorCoordinator 能显著减少为以传感器、二进制传感器或触发事件为主的集成编写初始化代码的工作量。只要把库返回的数据整理为 PassiveBluetoothDataUpdate 对象,框架就可以按需创建设备实体,并将 sensor 与 binary_sensor 平台实现简化到最少。
这些框架要求将来自库的数据整理成如下格式的 PassiveBluetoothDataUpdate:
PassiveBluetoothProcessorCoordinator
下面是集成在 __init__.py 中使用 PassiveBluetoothProcessorCoordinator 的 async_setup_entry 示例:
示例 sensor.py:
ActiveBluetoothProcessorCoordinator
ActiveBluetoothProcessorCoordinator 的工作方式与 PassiveBluetoothProcessorCoordinator 基本相同,但它还会根据 needs_poll_method 和 poll_method 主动建立连接进行轮询,以便在设备的 Bluetooth 广播变化时获取额外数据。其 sensor.py 实现与 PassiveBluetoothProcessorCoordinator 相同。
下面是集成在 __init__.py 中使用 ActiveBluetoothProcessorCoordinator 的 async_setup_entry 示例:

