custom-components / ble_monitor

BLE monitor for passive BLE sensors
https://community.home-assistant.io/t/passive-ble-monitor-integration/
MIT License
1.93k stars 248 forks source link

[Bug]: Bluetooth adapter not available in list #1166

Open LeVraiRoiDHyrule opened 1 year ago

LeVraiRoiDHyrule commented 1 year ago

What happened?

Hi,

I'm trying to add BLE monitor to my setup to monitor my Xiaomi Body Composition Scale 2 using Rpi4 built in Bluetooth, HA is running in Docker.

I can correctly scan for nearby devices by doing bluetoothctl scan on from the container console. I correctly added my bluetooth adapter as a device through the official bluetooth integration. But BLE monitor doesn't find any bluetooth adapter, and thus doesn't work. My bluetooth adapter doesn't appear in the list during configuration.

In my HA logs, I only have this error: 2023-05-04 01:26:05.818 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (DC:A6:32:0D:EF:14): Error stopping scanner: [org.bluez.Error.Failed] No discovery started I'm not sure what it is related to.

What am I doing wrong ? What could be the problem ? Thanks in advance for any answer, have a great day.

Sensor type

Body Composition Scale 2

Relevant log output

2023-05-04 01:26:05.818 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (DC:A6:32:0D:EF:14): Error stopping scanner: [org.bluez.Error.Failed] No discovery started
Ernst79 commented 1 year ago

The errors are from the Bluetooth integration, not from BLE monitor. Using both the Bluetooth integration and BLE monitor with one Bluetooth dongle/radio is asking for trouble. Make sure both use active scanning or passive scanning, as long as it is the same for both integrations. If that doesn’t work, try using an extra Bluetooth dongle, one for each integration. Also try a full system restart ,(not only Home Assistant)

LeVraiRoiDHyrule commented 1 year ago

Thanks for your answer.

Ok, then my problem is probably not with BLE monitor. It's true that I also have scanning problems with the base HA bluetooth integration. My adapter is detected in HA, but no new device is found. From the console, bluetoothctl scan on finds nearby devices, but hcitool dev doesn't show any bluetooth adapter.

This is out of scope then, but if you have some time you are welcome to help: https://community.home-assistant.io/t/cant-find-how-to-give-ha-bluetooth-access-in-docker-bridge-mode/567061/5. I'm having a hard time getting Bluetooth scanning to work in HA through docker using internal Rpi4 bluetooth chip.

Have a great day !

LeVraiRoiDHyrule commented 1 year ago

Hi again,

I quite solved my issue. I switched my container to host mode. That's something I want to avoid but I'll deal with that later. Now I'm trying to connect my Mi Body Composition Scale 2 to BLE Monitor. The adapter is correctly detected. I've correctly configured the integration.

What should I do now ? I tried to turn it on by stepping on it, but the device is not being added in BLE monitor (even with discovery enabled). The device MIBFS is detected when doing bluetoothctl scan on in the console, I can also do bluetoothctl info to get lot of info about the device. But I don't see what I'm supposed to do. I've tried to manually add the device by its MAC and UUID, but nothing happens. There is no added device. Is there something I misunderstood ? I don't see how it is supposed to work.

Thanks in advance for any answer.

Ernst79 commented 1 year ago

It should be added automagically. Nothing special you should do. Try to enable the function “report unknown advertisements” to see if BLE monitor receives data for that MAC address. This is an option you can set per MAC address. And enable debug logging

LeVraiRoiDHyrule commented 1 year ago

Thanks for your answer. Here is my debug log:

2023-05-04 20:15:23.641 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration ble_monitor which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-05-04 20:15:23.644 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration xiaomi_miot which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-05-04 20:15:23.646 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration bodymiscale which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-05-04 20:15:23.648 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-05-04 20:15:38.705 ERROR (MainThread) [homeassistant] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/dbus_fast/aio/message_bus.py", line 214, in on_hello
    raise err
  File "src/dbus_fast/aio/message_reader.py", line 24, in dbus_fast.aio.message_reader.build_message_reader._message_reader
  File "src/dbus_fast/_private/unmarshaller.py", line 659, in dbus_fast._private.unmarshaller.Unmarshaller._unmarshall
  File "src/dbus_fast/_private/unmarshaller.py", line 533, in dbus_fast._private.unmarshaller.Unmarshaller._read_header
  File "src/dbus_fast/_private/unmarshaller.py", line 283, in dbus_fast._private.unmarshaller.Unmarshaller._read_to_pos
EOFError
2023-05-04 20:15:38.711 ERROR (MainThread) [homeassistant] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/dbus_fast/aio/message_bus.py", line 214, in on_hello
    raise err
  File "src/dbus_fast/aio/message_reader.py", line 24, in dbus_fast.aio.message_reader.build_message_reader._message_reader
  File "src/dbus_fast/_private/unmarshaller.py", line 659, in dbus_fast._private.unmarshaller.Unmarshaller._unmarshall
  File "src/dbus_fast/_private/unmarshaller.py", line 533, in dbus_fast._private.unmarshaller.Unmarshaller._read_header
  File "src/dbus_fast/_private/unmarshaller.py", line 283, in dbus_fast._private.unmarshaller.Unmarshaller._read_to_pos
EOFError
2023-05-04 20:15:42.551 INFO (MainThread) [Jellyfin] ---[ START JELLYFINCLIENT ]---
2023-05-04 20:15:47.283 ERROR (Thread-2) [custom_components.ble_monitor] HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period.
2023-05-04 20:16:49.605 ERROR (MainThread) [zigpy.device] Failed to parse message (b'08ff0a0cf0480c000100000000000000000000') on cluster 768, because Data is too short to contain 1 bytes
2023-05-04 20:16:50.306 ERROR (Thread-2) [custom_components.ble_monitor] HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period.
2023-05-04 20:16:50.308 ERROR (Thread-2) [custom_components.ble_monitor] HCIdump thread: Trying to power cycle Bluetooth adapter hci0 DC:A6:32:0D:EF:14, will try to use it next scan period.
2023-05-04 20:18:29.816 ERROR (MainThread) [zigpy.device] Failed to parse message (b'08040a0bf04800') on cluster 768, because Data is too short to contain 1 bytes
2023-05-04 20:19:48.588 ERROR (MainThread) [homeassistant.components.xbox] Error requesting xbox data: 429, message='Too Many Requests', url=URL('https://peoplehub.xboxlive.com/users/me/people/batch/decoration/preferredcolor,detail,multiplayersummary,presencedetail')
2023-05-04 20:40:58.926 ERROR (MainThread) [zigpy.device] Failed to parse message (b'08130a07f04800') on cluster 768, because Data is too short to contain 1 bytes
2023-05-04 20:43:36.586 ERROR (MainThread) [homeassistant.components.xbox] Error requesting xbox data: 429, message='Too Many Requests', url=URL('https://peoplehub.xboxlive.com/users/me/people/batch/decoration/preferredcolor,detail,multiplayersummary,presencedetail')
2023-05-04 20:44:39.528 ERROR (MainThread) [homeassistant.components.xbox] Error requesting xbox data: 429, message='Too Many Requests', url=URL('https://peoplehub.xboxlive.com/users/me/people/batch/decoration/preferredcolor,detail,multiplayersummary,presencedetail')
2023-05-04 20:45:10.894 ERROR (MainThread) [custom_components.ble_monitor] Waiting for the HCIdump thread to finish took too long! (>10s)
2023-05-04 20:45:15.936 ERROR (Thread-5) [custom_components.ble_monitor] HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period.
Ernst79 commented 1 year ago

I see both errors in dbus, which is probably used in the Bluetooth integration. And I see errors that the HCIdump isn't working, which is used by BLE monitor. It is basically telling that the Bluetooth adapter isn't ready (e.g. not accessible). Not sure what is wrong, hard to tell, but your Bluetooth dongle/receiver is still not working correct. So it isn't receiving any data.

Hadatko commented 1 year ago

Hi, in my supervision instalation this integration worked. But i moved from it to container only version where this integration is not seeing my bt dongle. Default BT integration see my dongle, but not this monitor integration. I was setting up bluetooth related things based on HA manual. Still getting this:

2023-08-26 22:06:33.475 DEBUG (MainThread) [custom_components.ble_monitor.sensor] 0 BLE advertisements processed for 0 sensor device(s)
2023-08-26 22:06:33.479 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: main event_loop stopped, finishing.
2023-08-26 22:06:33.479 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: Scanning will be restarted
2023-08-26 22:06:33.480 DEBUG (Thread-2) [custom_components.ble_monitor] 0 HCI events processed for previous period
2023-08-26 22:06:33.480 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: Run
2023-08-26 22:06:33.480 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: start main event_loop
2023-08-26 22:06:34.478 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] 0 BLE ADV messages processed last 60 seconds for 0 device tracker device(s)
2023-08-26 22:06:56.801 DEBUG (MainThread) [custom_components.ble_monitor.config_flow] async_step_init user_input: None
2023-08-26 22:06:56.801 DEBUG (MainThread) [custom_components.ble_monitor.config_flow] async_step_init (before): {'bt_interface': ['disable'], 'bt_auto_restart': False, 'active_scan': False, 'discovery': True, 'period': 60, 'use_median': False, 'log_spikes': False, 'restore_state': False, 'report_unknown': False, 'devices': [], 'batt_entities': True, 'hci_interface': ['disable'], 'is_flow': True}
2023-08-26 22:07:32.556 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] 0 BLE advertisements processed for 0 binary sensor device(s)
2023-08-26 22:07:33.558 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: main event_loop stopped, finishing.
2023-08-26 22:07:33.558 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: Scanning will be restarted
2023-08-26 22:07:33.558 DEBUG (Thread-2) [custom_components.ble_monitor] 0 HCI events processed for previous period
2023-08-26 22:07:33.558 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: Run
2023-08-26 22:07:33.558 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: start main event_loop
2023-08-26 22:07:33.559 DEBUG (MainThread) [custom_components.ble_monitor.sensor] 0 BLE advertisements processed for 0 sensor device(s)
2023-08-26 22:07:34.559 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] 0 BLE ADV messages processed last 60 seconds for 0 device tracker device(s)
2023-08-26 22:08:32.637 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] 0 BLE advertisements processed for 0 binary sensor device(s)
2023-08-26 22:08:33.641 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: main event_loop stopped, finishing.
2023-08-26 22:08:33.641 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: Scanning will be restarted
2023-08-26 22:08:33.641 DEBUG (Thread-2) [custom_components.ble_monitor] 0 HCI events processed for previous period
2023-08-26 22:08:33.641 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: Run
2023-08-26 22:08:33.641 DEBUG (Thread-2) [custom_components.ble_monitor] HCIdump thread: start main event_loop
2023-08-26 22:08:33.642 DEBUG (MainThread) [custom_components.ble_monitor.sensor] 0 BLE advertisements processed for 0 sensor device(s)
2023-08-26 22:08:34.642 DEBUG (MainThread) [custom_components.ble_monitor.device_tracker] 0 BLE ADV messages processed last 60 seconds for 0 device tracker device(s)
Hadatko commented 1 year ago

Addidn some pictures. I tried this also without HA bt integration Screenshot_20230918_010146 Screenshot_20230918_010301 Screenshot_20230918_010342 Screenshot_20230918_011038

Ernst79 commented 1 year ago

How did you configure BLE monitor anyway, via yaml or via the UI? I see both in the screenshots?

I see that the option "Don't use Bluetooth adapter" is selected, try to unselect that option.

Hadatko commented 1 year ago

I was trying both option. None of them worked. I tried uncheck that option several times but it is always checked when i am showing configuration.

Hadatko commented 1 year ago

In my case the issue is that container network has to be HOST not my own network. So the issue is likely not with this integration.

Hadatko commented 1 year ago

Sorry for spamming. Just bringing more information. A believe my error:

2023-09-25 13:43:42.476 DEBUG (SyncWorker_11) [bluetooth_adapters.systems.linux_hci] Error while getting HCI devices: [Errno 97] Address family not supported by protocol

comes from https://github.com/Bluetooth-Devices/bluetooth-adapters/blob/main/src/bluetooth_adapters/systems/linux_hci.py

def get_adapters_from_hci() -> dict[int, dict[str, Any]]:
    """Get bluetooth adapters from HCI."""
    out: dict[int, dict[str, Any]] = {}
    sock: socket.socket | None = None
    try:
        sock = socket.socket(AF_BLUETOOTH, socket.SOCK_RAW, BTPROTO_HCI)
        buf = hci_dev_list_req()
        buf.dev_num = HCI_MAX_DEV
        ret = fcntl.ioctl(sock.fileno(), HCIGETDEVLIST, buf)
        if ret < 0:
            raise OSError(f"HCIGETDEVLIST failed: {ret}")
        for i in range(buf.dev_num):
            dev_req = buf.dev_req[i]
            dev = hci_dev_info()
            dev.dev_id = dev_req.dev_id
            ret = fcntl.ioctl(sock.fileno(), HCIGETDEVINFO, dev)
            info = {str(k): getattr(dev, k) for k, v_ in dev._fields_}
            info["bdaddr"] = str(info["bdaddr"])
            info["name"] = info["name"].decode()
            out[int(dev.dev_id)] = info
    except OSError as error:
        _LOGGER.debug("Error while getting HCI devices: %s", error)
        return out
    except Exception as error:  # pylint: disable=broad-except
        _LOGGER.exception("Unexpected error while getting HCI devices: %s", error)
        return out
    finally:
        if sock:
            sock.close()
    return out