Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing to happen in PassiveBluetoothProcessorCoordinator #76384

Merged
merged 11 commits into from Aug 9, 2022

Conversation

Jc2k
Copy link
Member

@Jc2k Jc2k commented Aug 7, 2022

Breaking change

This changes the PassiveBluetoothProcessorCoordinator and PassiveBluetoothDataProcessor bluetooth API's introduced in 2022.8.

  • 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.
  • PassiveBluetoothDataProcessor still takes an update_method, but instead of a BluetoothServiceInfoBleak, it now takes receives the data from PassiveBluetoothProcessorCoordinator's update_method

This change is to help integrations where:

  • a single advertisement contains data for multiple platforms and only needs parsing once
  • where you need to parse advertisements to know what platforms to load

Proposed change

As per discussion in #76342, slight refactor so that the bluetooth coordinator can drive the parsers. This is useful:

  • Where 2 platforms in an integration use the same parser (e.g. sensor and binary_sensor contained in the same advertisement)
  • There is non-platform logic (like encryption and triggering reauth flows) that makes sense to pull out of the platform.
  • Where you need to parse the advertisements to know which platforms to load

If i rebase #76342 on top of this then I can implement an ActiveBluetoothProcessorCoordinator that polls characteristics at the coordinator and makes the data available to multiple platforms the same way passsive data is handled (DataProcessor.async_update) (in the PR as it is now, I have to do this in the DataProcessor which probhibits sharing collected data between platforms).

Crucially, each platform is still responsible for its own entities - the coordinator does not know about Home Assistant entities.

There is a default update_method for the coordinator - that just passes through the BluetotohServiceInfo, so other integrations carry on working as they are for now. I intend for that to be temporary, unless people think its useful.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 馃 Silver
  • 馃 Gold
  • 馃弳 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

Hey there @bdraco, mind taking a look at this pull request as it has been labeled with an integration (bluetooth) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link

Hey there @bdraco, mind taking a look at this pull request as it has been labeled with an integration (sensorpush) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link

Hey there @bdraco, mind taking a look at this pull request as it has been labeled with an integration (moat) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link

Hey there @bdraco, mind taking a look at this pull request as it has been labeled with an integration (inkbird) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link

Hey there @bdraco, mind taking a look at this pull request as it has been labeled with an integration (govee_ble) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link

Hey there @Ernst79, mind taking a look at this pull request as it has been labeled with an integration (xiaomi_ble) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@@ -52,7 +52,9 @@ class PassiveBluetoothDataUpdate(Generic[_T]):
)


class PassiveBluetoothProcessorCoordinator(BasePassiveBluetoothCoordinator):
class PassiveBluetoothProcessorCoordinator(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you subclass the coordinator and overload async_handle_bluetooth_event instead?

Copy link
Member Author

@Jc2k Jc2k Aug 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think this behaviour makes sense as the default, which is why I didn't do it in a subclass. If you disagree I am happy to do it in a subclass instead. But i'd be submitting an ActiveBluetoothProcessorCoordinator seperately for xiaomi, and I think this behaviour especially makes sense for any passive sensor that has binary sensor and sensor payloads in the same broadcast, and any passive sensor that has dynamic platforms. (So we'd have quite a few coordinator flavours).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its ok to change it, but we need to label it a breaking change since its shipped this way in 2022.8

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok my bad - didn't think of API stability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if i revert c8e0421 it won't be a breaking change.

@Jc2k Jc2k force-pushed the bluetooth_parse_in_coordinator branch from c8e0421 to 7ad474c Compare August 7, 2022 20:42
@Jc2k Jc2k changed the title WIP: Allow parsing to happen in PassiveBluetoothProcessorCoordinator Allow parsing to happen in PassiveBluetoothProcessorCoordinator Aug 7, 2022
@Jc2k Jc2k marked this pull request as ready for review August 7, 2022 20:42
@Jc2k
Copy link
Member Author

Jc2k commented Aug 7, 2022

TODO: Decide whether to revert 7c48dd5. If we do, this isn't a breaking change. Though we should probably slap a deprecation warning on it?

@bdraco
Copy link
Member

bdraco commented Aug 7, 2022

Its new enough that I would remove it now. Easier to do it take the pain now then when 50 integrations are using this

@Jc2k Jc2k mentioned this pull request Aug 7, 2022
23 tasks
@bdraco
Copy link
Member

bdraco commented Aug 7, 2022

We will need a breaking change section to tell devs what to do, probably actually better as a blog post on developers.home-assistant.io

@Jc2k
Copy link
Member Author

Jc2k commented Aug 7, 2022

馃憤 - will pick that up in the morning. long day and i don't think i can write anything coherent right now 馃槅 I've added a rambling Breaking section in the meantime.

Jc2k added a commit to Jc2k/developers.home-assistant that referenced this pull request Aug 8, 2022
@Jc2k
Copy link
Member Author

Jc2k commented Aug 8, 2022

First draft of blog post: home-assistant/developers.home-assistant#1427

@Jc2k Jc2k force-pushed the bluetooth_parse_in_coordinator branch from 7ad474c to efd21c2 Compare August 8, 2022 07:23
@Jc2k
Copy link
Member Author

Jc2k commented Aug 8, 2022

Last commit fixed missing test coverage, testing failure and recovery of the new method.

Will now start reworking the ActiveDataProcessor from my other PR to work with this instead.

@bdraco bdraco self-requested a review August 8, 2022 21:18
Dev automation moved this from Needs review to Reviewer approved Aug 8, 2022
@bdraco
Copy link
Member

bdraco commented Aug 8, 2022

All working as expected. Its definitely makes it a lot more flexible, and less code in the integration as well 馃憤

@Jc2k Jc2k merged commit 7d427dd into home-assistant:dev Aug 9, 2022
Dev automation moved this from Reviewer approved to Done Aug 9, 2022
@Jc2k Jc2k deleted the bluetooth_parse_in_coordinator branch August 9, 2022 05:36
@github-actions github-actions bot locked and limited conversation to collaborators Aug 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants