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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically determine the advertising interval for bluetooth devices #79669

Merged
merged 34 commits into from
Oct 14, 2022

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Oct 5, 2022

Proposed change

Currently we rely on the bluetooth stack to tell us when a device is no longer available. That makes sense for connectable devices, but for devices that do not require an active connection they can sleep for a long time, wake up, broadcast, and go back to sleep to save battery.

To solve this we now collect the maximum time between 16 advertisements and only declare these devices unavailable once we see it miss advertising. If we can't automatically determine the advertising interval we will use the 15m fallback interval currently used for scanners that do not have their own tracking stack (esphome).

This also allows us to have a better metric to determine when advertisements are stale and we should switch adapters when there are multiple adapters for both connectable and non-connectable devices.

fixes #78702
fixes #79178
fixes #78195
fixes #78380
fixes #78261

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:

@homeassistant
Copy link
Contributor

Hey there @OttoWinter, @jesserockz, mind taking a look at this pull request as it has been labeled with an integration (esphome) you are listed as a code owner for? Thanks!

@bdraco
Copy link
Member Author

bdraco commented Oct 6, 2022

We should do this for active devices as well, but not for unavailability, just to know when to switch to another adapter on failure

@bdraco bdraco changed the title Automatically determine the advertising interval for passive bluetooth devices Automatically determine the advertising interval for bluetooth devices Oct 6, 2022
@bdraco
Copy link
Member Author

bdraco commented Oct 6, 2022

Testing

  • fail over
  • performance
  • unavailability

@bdraco
Copy link
Member Author

bdraco commented Oct 6, 2022

Turned out to be smaller than expected. It took a lot more testing time than actual time writing code (but doesn't everything)

@bdraco
Copy link
Member Author

bdraco commented Oct 6, 2022

Need to adjust the tests and I think this should be GTG if I don't find anything when adjusting the tests

@bdraco bdraco marked this pull request as ready for review October 8, 2022 06:07
@bdraco bdraco mentioned this pull request Oct 14, 2022
31 tasks
@balloob balloob merged commit 0c76e3a into home-assistant:dev Oct 14, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.