So, one of the most difficult things about Home Assistant is that it often must use usb devices to communicate to the external world. Well, this post outlines one way to get Home Assistant setup as a usbip client. Then, it is possible to make home assistant a truly portable and contanered application.
Step 1 – Configure a usbip server
First, we have to have a Debian instance running and attached to a usb device. Raspberry Pi is a good candidate for this option. Especially, if running with a wifi adapter. Then, it is possible to setup the usb dongle in the most central location. Thus, optimizing for use and access to associated devices.
Install usbip and usbutils
sudo apt install -y usbip usbutils
sudo usbipd -D
sudo modprobe usbip_host
sudo echo 'usbip_host' >> /etc/modules
sudo usbip list -p -l
busid=3-1#usbid=10c4:ea60#
sudo usbip bind --busid=3-1
usbip: info: bind device on busid 3-1: complete
Step 2 – Configure a usbip client in home assistant pod.
sudo apt install -y usbip usbutils
sudo modprobe vhci-hcd
sudo echo 'vhci-hcd' >> /etc/modules
sudo usbip attach -r 192.168.1.172 -d 3-1
lsusb
Bus 002 Device 002: ID 10c4:ea60 Silicon Labs CP210x UART Bridge
Step 3 – Make USBIP Run as a Service on server and in Home Assistant Pod.
Final step, At least for this first rendition is to make services for both home assistant pod and usbip server. This way they will auto start on reboot. Now, I have not got this 100 percent yet; however, I wanted to share what I do have working and then as I finalize my setup I will add the other elements to this post.
Below, is the /lib/systemd/system/usbipd.service
[Unit]
Description=usbip host daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/usbipd -D
ExecStartPost=/bin/sh -c "/lib/systemd/system/usbipdup.sh"
ExecStop=/bin/sh -c "/lib/systemd/system/usbipddwn.sh"
[Install]
WantedBy=multi-user.target
This is the /lib/systemd/system/usbipdup.sh
script
#!/bin/bash
/usr/sbin/usbip bind --$(/usr/sbin/usbip list -p -l | grep '#usbid=10c4:ea60#' | cut '-d#' -f1)
#/usr/sbin/usbip bind --$(/usr/sbin/usbip list -p -l | grep '#usbid=0a12:0001#' | cut '-d#' -f1)
This is the /lib/systemd/system/usbipddwn.sh
#!/bin/bash
/usr/sbin/usbip unbind --$(/usr/sbin/usbip list -p -l | grep '#usbid=10c4:ea60#' | cut '-d#' -f1);
#/usr/sbin/usbip unbind --$(/usr/sbin/usbip list -p -l | grep '#usbid=0a12:0001#' | cut '-d#' -f1);
killall usbipd
other client commands
sudo usbip list -r 192.168.1.172
Exportable USB devices
======================
- 192.168.1.172
3-1: Silicon Labs : CP210x UART Bridge (10c4:ea60)
: /sys/devices/pci0000:00/0000:00:1d.0/usb3/3-1
: (Defined at Interface level) (00/00/00)
: 0 - Vendor Specific Class / unknown subclass / unknown protocol (ff/00/00)
sudo usbip port
Imported USB devices
====================
Port 00: <Port in Use> at Full Speed(12Mbps)
Silicon Labs : CP210x UART Bridge (10c4:ea60)
2-1 -> usbip://192.168.1.172:3240/3-1
-> remote bus/dev 003/002
sudo usbip detach -p 00
usbip: info: Port 0 is now detached!
Common Errors
tcp connect
debian@usbip-2:~$ sudo usbip attach -r 192.168.1.172 -d 3-1
usbip: error: tcp connect
Typically occurs because usbipd -D was not ran on server and so client cannot create a tcp port.
Device busy (exported)
debian@usbip-2:~$ sudo usbip attach -r 192.168.1.172 -d 3-1
usbip: error: Attach Request for 3-1 failed - Device busy (exported)
Typically, occurs when client is already ported in device from server.