linuxserver/docker-homeassistant

Add support for bluetooth

hydazz opened this issue · 7 comments

linuxserver.io


Following the recent release of home assistant they have broadened Bluetooth support, I would like to adopt my homekit BLE door lock into hass. I can't get it to work by myself on unraid, due to a limitation within the container

Desired Behavior

Bluetooth integration to start successfully

Current Behavior

my Bluetooth dongle is located at /dev/bus/usb/001/004 on the host, using --device=/dev/bus/usb/001/004 to passthrough to the container
Bluetooth integration won't start. bluez cant access dbus socket or the Bluetooth USB as its owned by root (i think)

Config entry 'Bluetooth' for bluetooth integration not ready yet: Failed to start Bluetooth: [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.42" (uid=99 pid=14825 comm="/usr/bin/python3 /usr/bin/hass -c /config") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=8854 comm="/usr/sbin/bluetoothd"); Retrying in background

Alternatives Considered

home assistant wants to be run as root? how dbus and bluez work is above me

Thanks for opening your first issue here! Be sure to follow the bug or feature issue templates!

I don't have a bluetooth dongle to test with and there is a lot of confusing and conflicting info on the web.

For usb zwave and zigbee dongles, it's easy because they are often at /dev/ttyACMX or /dev/ttyUSBX, likely in a separate group like dialout, with group rw perms so mapping those and adding the HA user to that group makes the devices accessible inside the container.

Are you sure the bluetooth device is not accessible at a separate location like those listed above besides the raw usb bus? Is it recognized by the linux kernel and the driver loaded?

Is it recognized by the linux kernel and the driver loaded

Yep, unraid does not have the drivers so they had to be installed manually. bluez is installed with nerdpack and the USB Bluetooth dongle is recognised by the host OS and can be controlled with bluetoothctl.

from reading here it looks like the container needs to be run as privileged. I'll keep looking into this and see if I can manage to get it to work without --privileged and without homeassistant being ran as root.

I don't have a bluetooth dongle

I assume that a raspberry pi could be used for testing to substitute a USB dongle for the built-in Bluetooth?

I've managed to get it to work:
steps to get Bluetooth to work on unraid in this container (should be applicable to other OS's):

Steps 1-3 are unraid specific

  1. Install bluez with NerdPack
  2. Run these commands to install drivers:
sudo curl -L https://raw.githubusercontent.com/Realtek-OpenSource/android_hardware_realtek/rtk1395/bt/rtkbt/Firmware/BT/rtl8761b_config -o /tmp/rtl8761bu_config.bin
sudo mv /tmp/rtl8761bu_config.bin /lib/firmware/rtl_bt/rtl8761bu_config.bin
sudo curl -L https://raw.githubusercontent.com/Realtek-OpenSource/android_hardware_realtek/rtk1395/bt/rtkbt/Firmware/BT/rtl8761b_fw -o /tmp/rtl8761bu_fw.bin
sudo mv /tmp/rtl8761bu_fw.bin /lib/firmware/rtl_bt/rtl8761bu_fw.bin
  1. Unplug the USB and plug it back in and run /etc/rc.d/rc.bluetooth start
  2. Run lsusb, get the Bus and Device
root@Discovery:/sys/class# lsusb
Bus 002 Device 003: ID 174c:3074 ASMedia Technology Inc. ASM1074 SuperSpeed hub
Bus 002 Device 002: ID 0781:5583 SanDisk Corp. Ultra Fit
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1a86:7523 QinHeng Electronics CH340 serial converter
Bus 001 Device 002: ID 174c:2074 ASMedia Technology Inc. ASM1074 High-Speed hub
>Bus 001 Device 006: ID 2357:0604 TP-Link TP-Link UB500 Adapter
Bus 001 Device 004: ID 0463:ffff MGE UPS Systems UPS
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Our Bluetooth USB (Bus 001 Device 006: ID 2357:0604 TP-Link TP-Link UB500 Adapter) is on Bus 001, Device 006, so our path is /dev/bus/usb/001/006.

  1. A path must be used rather than --device when passing through the USB
    when starting the container add --v /dev/bus/usb/<bus>/<device>:/dev/bus/usb/<bus>/<device> and -v /run/dbus:/run/dbus:ro
  2. Open a shell into the container and run apk add bluez
  3. run bluetoothctl show, should show something like:
Controller (MAC) (public)
        Name: BlueZ 5.50
        Alias: BlueZ 5.50
        Class: 0x00000104
        Powered: yes
        Discoverable: no
        DiscoverableTimeout: 0x000000b4
        Pairable: yes
        ...
        Discovering: yes
Advertising Features:
        ActiveInstances: 0x00 (0)
        SupportedInstances: 0x04 (4)
        SupportedIncludes: tx-power
        SupportedIncludes: appearance
        SupportedIncludes: local-name
  1. Install nano, nano /etc/services.d/homeassistant/run, remove s6-setuidgid abc from the run command.
  2. Restart the container and add Bluetooth integration

Mostly smooth sailing until we hit step 8, I know its highly illegal for any service to run as root but I cant see a way around it here,

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

hello, there is nothing else to do to this repo.
you can easily enable bluetooth in docker by the following steps:

  1. install bluez on your host, you can get bluez from nerd-pack or un-get on unraid.
  2. enable bluetoothd, you can enable it with /etc/rc.d/rc.bluetooth start
  3. enable your bluetooth device, hciconfig hci0 up
    if you have many devices, try this script:
#!/bin/bash

devices=`hciconfig | grep '^hci[0-9]\+' -o`

for device in ${devices}
do
    hciconfig ${device} up
done
  1. mount /run/dbus into your docker container with readonly mode.

here is the homeassistant section in my docker-compose file:

homeassistant:
  image: lscr.io/linuxserver/homeassistant:latest
  container_name: homeassistant
  environment:
    - PUID=99
    - PGID=100
    - UMASK=002
    - TZ=Asia/Shanghai
  volumes:
    - /mnt/user/appdata/homeassistant:/config
    - /mnt/user/media/public/music:/music
    - /run/dbus:/run/dbus:ro
  networks:
    webapp:
    host_link:
      ipv4_address: 192.168.29.105
    mariadb:
  depends_on:
    - db
  restart: unless-stopped

btw, unraid 6.11 has a built-in rtl8761buv firmware, so you neednt install the firmware manually

@VergilGao I had tried that before but lacked hciconfig hci0 up much appreciated on steps to get it working!