home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.45k stars 30.69k forks source link

BTHome devices are not updating when BT adapter is in passive mode #111353

Closed Sorien closed 8 months ago

Sorien commented 8 months ago

The problem

when I activate passive mode for Bluetooth adapter bthome devices are not updating, even though I see them updating in Passive BLE monitor connected to different adapter, i've tied to switch adapters, even disable second adapter, Passive BLE monitor and so on, so its not adapter problem, its something wrong in BTHome implementation

What version of Home Assistant Core has the issue?

core-2024.2.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Bluetooth

Link to integration documentation on our website

No response

Diagnostics information

i see them in bluetooth logs when loging is enabled

AdvertisementData(local_name='ATC_AC55D1', service_data={'0000fcd2-0000-1000-8000-00805f9b34fb': b'@\x00\x9e\x01d\x02\x83\x08\x03\x11\x13'}, rssi=-71) match: set() 2024-02-25 10:33:07.735 DEBUG (MainThread) [homeassistant.components.bluetooth.manager] hci1 (C8:8A:9A:5F:1B:16) [connectable]: A4:C1:38:51:BC:CA AdvertisementData(local_name='ATC_51BCCA', service_data={'0000fcd2-0000-1000-8000-00805f9b34fb': b'@\x00I\x01\x19\x02*\x08\x03\x89\x13'}, rssi=-76) match: set() 2024-02-25 10:33:08.502 DEBUG (MainThread) [homeassistant.components.bluetooth.manager] hci1 (C8:8A:9A:5F:1B:16) [connectable]: 58:2D:34:60:BC:D9

Example YAML snippet

No response

Anything in the logs that might be useful for us?

not all bluetooth integrations have this problem like Qingping and OralB devices are updated even in pasive mode

Additional information

Passive BLE: 12.9.0 Linux kernel: 6.6.17 Bluez: 5.68

home-assistant[bot] commented 8 months ago

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

Code owner commands Code owners of `bluetooth` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign bluetooth` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


bluetooth documentation bluetooth source (message by IssueLinks)

home-assistant[bot] commented 8 months ago

Hey there @ernst79, mind taking a look at this issue as it has been labeled with an integration (bthome) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `bthome` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign bthome` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


bthome documentation bthome source (message by IssueLinks)

Ernst79 commented 8 months ago

I see you use the ATC firmware on a Xiaomi temperature sensor. In the telink flasher, did you enable the option “send flags” (or something similar). If not, try to enable it.

Sorien commented 8 months ago

@Ernst79 it seems to be working, what is that setting? why your addon does not need that? because I have the same problem with iBeacon.

Ernst79 commented 8 months ago

It adds 020106 to the BLE advertisement, which is a Bluetooth advertising data type flag, telling the receiver what type of data it is.

06 sets the following two types to True

BLE_SIG_ADV_FLAG_LE_GENERAL_DISC_MODE (0x02)
BLE_SIG_ADV_FLAG_BR_EDR_NOT_SUPPORTED (0x04)

see also https://docs.particle.io/reference/device-os/api/bluetooth-le-ble/bleadvertisingdatatype-flags/

Most sensors, like Xiaomi, govee and qingping add this data by default. In the ATC firmware, it is disabled by default.

BLE monitor receives the data by collecting raw HCI data, while BTHome uses the Bluetooth integration, which is using some other package to collect the BLE data (bleak if I remember correct). I guess bleak requires this flag to be send.

I think it would be good to add this to the documentation, you’re not the first :-). I’ll do that later.

Sorien commented 8 months ago

@Ernst79 thx for your help, I really appreciate it

Sorien commented 8 months ago

I see that incorrect filtering was added when passive_scanning was introduced https://github.com/home-assistant/core/pull/75542/files#diff-1ce9248554a06c6b1190695bcc9148652a910e6750649c8933a0e75069bc2f80R48

but this file seems to not exist in the current version of the source code.

iBeacom integration seems to have a problem in passive mode because Android will never send that part of the packet and there is no API to force it.

bdraco commented 8 months ago

Code is still there but its moved to https://github.com/Bluetooth-Devices/habluetooth/blob/71bd22a4bea50fff5ff9b8661b4c19814a512331/src/habluetooth/scanner.py#L40

Those flags originate from https://github.com/hbldh/bleak/pull/884

We can adjust them, but bluez requires at least one orpattern to work

Sorien commented 8 months ago

I'll try to make it working locally and after that will open PR with proposed change

Sorien commented 8 months ago

@bdraco @Ernst79 maybe you could have look at it https://github.com/Bluetooth-Devices/habluetooth/pull/31

We can adjust them, but bluez requires at least one orpattern to work

I saw that issue from bleak autors in BlueZ repo but after commenting code that requires to have min one pattern set, I was still able to receive packet data but just only from devices that have advertising flags set.