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

Add bluetooth subscribe_advertisements WebSocket API #134291

Merged
merged 24 commits into from
Jan 11, 2025
Merged

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Dec 30, 2024

frontend: home-assistant/frontend#23531

Proposed change

Add bluetooth subscribe_advertisements WebSocket API

  • add/remove events
  • private api to get a callback from _address_disappeared

The goal is to build something like the current devices dashboard where all the Bluetooth devices can be seen, along with the current preferred source scanner and RSSI, and by clicking on one it will show the Advertisement data for it.

Address Name Source RSSI
AA:BB:CC:DD:EE:FF Sensor EE:FF:CC:DD:EE:DD (scanner name) -74
AA:BB:CC:DD:EE:DD Light EE:FF:CC:DD:EE:DD (scanner name) -84

Frontend testing

$0.hass.connection.subscribeMessage(function(msg) { console.log(msg); }, {"type":"bluetooth/subscribe_advertisements"});

API Samples

After calling bluetooth/subscribe_advertisements

{'id': 1, 'result': None, 'success': True, 'type': 'result'}

Next all the current advertisements are sent (always sent in a list, there will likely be 100s of them on the first event):

{'event': {'add': [{'address': '44:44:33:11:23:12',
                    'connectable': True,
                    'manufacturer_data': {},
                    'name': 'wohand_signal_100',
                    'rssi': -127,
                    'service_data': {},
                    'service_uuids': [],
                    'source': 'hci0',
                    'time': 1735670958.165404,
                    'tx_power': -127}]},
 'id': 1,
 'type': 'event'}

As updates come in, a new event is sent. Note that there is no difference between add and change and the client side needs to keep track of existing ones and update them.

{'event': {'add': [{'address': '44:44:33:11:23:12',
                    'connectable': True,
                    'manufacturer_data': {'123': '616263'},
                    'name': 'wohand_signal_100',
                    'rssi': -80,
                    'service_data': {},
                    'service_uuids': [],
                    'source': 'hci1',
                    'time': 1735670959.165404,
                    'tx_power': -127}]},
 'id': 1,
 'type': 'event'}

When a devices goes out of range and the keep timer expires, a remove event is generated

{'event': {'remove': [{'address': '44:44:33:11:23:12'}]},
 'id': 1,
 'type': 'event'}

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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format 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.

To help with the load of incoming pull requests:

@bdraco
Copy link
Member Author

bdraco commented Dec 30, 2024

Need to rework so we have, add and remove events, and subscribe to unavailable as well

will need to hook that into _address_disappeared.. will need a private API for that

@bdraco bdraco marked this pull request as ready for review January 3, 2025 20:55
@bdraco bdraco merged commit cdc96fd into dev Jan 11, 2025
64 checks passed
@bdraco bdraco deleted the bluetooth_device_list branch January 11, 2025 02:49
@github-actions github-actions bot locked and limited conversation to collaborators Jan 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants