From 919436779bd4bd885c17550f4d567f5ded3b51e5 Mon Sep 17 00:00:00 2001 From: Jc2k Date: Mon, 8 Aug 2022 13:40:21 +0100 Subject: [PATCH] Blog on PassiveBluetoothProcessorCoordinator (#1427) Co-authored-by: Martin Hjelmare --- blog/2022-08-08-bluetooth-api-changes.md | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 blog/2022-08-08-bluetooth-api-changes.md diff --git a/blog/2022-08-08-bluetooth-api-changes.md b/blog/2022-08-08-bluetooth-api-changes.md new file mode 100644 index 00000000..adfa6290 --- /dev/null +++ b/blog/2022-08-08-bluetooth-api-changes.md @@ -0,0 +1,45 @@ +--- +author: John Carr +authorURL: https://github.com/Jc2k +title: "Bluetooth passive sensor API changes for 2022.9" +--- + +For Home Assistant Core 2022.9 we have changed the `PassiveBluetoothProcessorCoordinator` and `PassiveBluetoothDataProcessor` bluetooth API's to make `PassiveBluetoothProcessorCoordinator` responsible for parsing. The coordinator then pushes parsed data to `PassiveBluetoothDataProcessor` instances. + +PassiveBluetoothProcessorCoordinator now takes a mandatory update_method callback that receives bluetooth advertisements (in the form of BluetoothServiceInfoBleak) and returns the data that should be handed off to any subscribed PassiveBluetoothDataProcessor: + +```python +def my_parser(service_info: BluetoothServiceInfoBleak) -> MyDataClass: + ... + + return MyDataClass( + a=some_parsed_data, + b=some_other_parsed_data, + ) + + +coordinator = PassiveBluetoothProcessorCoordinator( + hass, + _LOGGER, + address=address, + mode=BluetoothScanningMode.PASSIVE, + update_method=my_parser, +) +``` + +PassiveBluetoothDataProcessor still takes an update_method, but instead of a BluetoothServiceInfoBleak, it now receives the data returned from PassiveBluetoothProcessorCoordinator's update_method. It should still return a `PassiveBluetoothDataUpdate` as before: + +```python +def sensor_update_to_bluetooth_data_update( + sensor_update: MyDataClass, +) -> PassiveBluetoothDataUpdate: + """Convert a sensor update to a bluetooth data update.""" + ... + return PassiveBluetoothDataUpdate( ... ) + +processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update) +``` + +All the built-in integrations have already been converted, so take a look at them for more examples. + +This change will help integrations that need to start parsing data before loading a platform (for example, the list of platforms to load depend on data from the advertisements) or changes where a single advertisement drives multiple platforms (you won't have to parse the broadcast twice).