Home Assistant Bluetooth integration

I have recently purchased an Xiaomi LYWSD03MMC BLE temperature and humidity sensor, I would like to connect this device to my Home Assistant system.

Therefore, I would like to enable Bluetooth integration to Home Assistant. When I attempt to add the integration it says:

No unconfigured Bluetooth adapters found

There is a help link that points to Bluetooth - Home Assistant and I believe Balena would fall under this.

  • Home Assistant Container: The host system must run BlueZ, and the D-Bus socket must be accessible to Home Assistant inside the container.

I believe D-Bus access is achieved by adding the io.balena.features.dbus: '1' label to homeassistant service in docker-compose.yml.

But, I don’t know how to run bluez on the host system. I have tried adding RUN commands to the Dockerfile but nothing is right:

e.g.

FROM homeassistant/home-assistant:stable

COPY start.sh /tmp/start.sh

RUN apt install bluez

RUN chmod +x /tmp/start.sh

CMD ["bash","/tmp/start.sh"]

But balena push attempts respond with /bin/ash: apt: not found

I have connected to the Host serial console and noticed there are hci tools already installed, from there I was able to run hcitool lescan and the device I want to connect to is discovered:

root@2b97a16:~# hcitool lescan
LE Scan ...
...
A4:C1:38:F3:FA:1B ATC_F3FA1B

This tells me bluetooth already is available to the host … So why am I unable to detect the BLE device in Home Assistant :-/

Hello, the io.balena.features.dbus: '1' is a good start, but I think you also need to specify DBUS_SYSTEM_BUS_ADDRESS env variable, see : Communicate outside the container - Balena Documentation - You may also want to check out our Bluetooth block GitHub - balena-labs-projects/bluetooth: Optimized bluetooth agent for balenaOS. Based on BlueZ 5.0. for an example of BlueZ on balena which you could use or adapt for your use.

Thanks, I didn’t know about setting the D_BUS environment variable, that page too looks like a great source.

I was interested by the bluetooth block too, but wasn’t sure how or if that would work with the homeassistant service.

You may also find the explanations and suggestions in this similar post helpful: Configure Bluez on Host OS from Container

I wanted to use the dbus and bluetooth blocks so added those first, but could not get anything on home assistant to work. After that I instead simply added the environment variable and it worked, without the aforementioned blocks. I feel like the blocks is the Balena way to do things so it was a shame this did not easily work [but I blame my lack of working knowledge of blocks for that].

I added the PVVX firmware to the sensor and have been able to add as a BTHome integration.

I do notice this error and warnings though:

 homeassistant  2023-01-01 23:53:05.692 ERROR (MainThread) [homeassistant.components.bluetooth.scanner] hci0 (B8:27:EB:0E:64:EC): Error stopping scanner: [org.bluez.Error.InProgress] Operation already in progress
 homeassistant  2023-01-01 23:53:05.702 WARNING (MainThread) [bluetooth_auto_recovery.recover] Getting Bluetooth adapter failed hci0 [B8:27:EB:0E:64:EC] because the system cannot create a bluetooth socket: Unable to open PF_BLUETOOTH socket
 homeassistant  2023-01-01 23:53:05.707 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not find adapter with mac address B8:27:EB:0E:64:EC or hci0
 homeassistant  2023-01-01 23:53:05.712 WARNING (MainThread) [bluetooth_auto_recovery.recover] Closing Bluetooth adapter hci0 [B8:27:EB:0E:64:EC] failed: 'NoneType' object has no attribute 'detach'

Would be nicer if they didn’t exist.