Home Assistant on Hyper-V with USB over IP

 

There is a pretty good article regarding USB over IP solution for HA on Hyper-v:
https://dvlup.com/2020/10/23/usb-in-hyper-v/

Two usbip software were mentioned in the article: USB Network Gate ($159) and USB Redirector ($74). I also found a slightly cheaper one called VirtualHere ($49). I tested VirtualHere and it also works. Note that VirtualHere has a “node-locked” license that you cannot transfer to a different computer.

Now understood the concept and I start searching for a “FREE” solution (right, assume my time is free). And I did come up with a free solution. The only negative part is that the host windows OS has to run in Test Mode as the driver is self signed. In my use case I have an Intel NUC running Hyper-V and WSL2. A Zwave/Zigbee stick is plugged in the local NUC USB port.

And Here is the procedures:

  1. Install Debian 11 in Hyper-V

  2. Install Supervised HA on Debian

  3. Build uspip windows server
    3.1 Get the source code from github: https://github.com/cezanne/usbip-win. This is usbip server side driver and daemon.
    3.2 Set up the build environment (Visual Studio 2019 Community Edition, Windows DDK, etc)
    3.3 Build the binaries from the source
    3.4 install certificate (follow instructions on github)
    3.5 run command: “usbip list -l”, determine which usb device to bind
    3.6 bind the device and start daemon: usbipd

  4. Configure Debian usbip client
    4.1 enable usbip module: modprobe vhci-hcd
    4.2 attach usb over ip: usbip attach -r {host_ip} -b {bus_id}
    4.3 run command: “usbip port” to confirm usb device binds successfully

  5. In HA UI confirm the new device discovery. Test to make sure everything works (I did one z-wave switch inclusion and use that to test z-wave function)

  6. Now everything works we need to steamline the process
    6.1 Configure the Debian VM to remain off upon host start, and shutdown upon host shutdown
    6.1 In windows, launch Task Scheduler, create new task, set trigger to “run at logon”. Check “run at highest privilege”
    6.2 Add action to bind the usb device: {your_path}\usbip bind -b {bus_id}
    6.3 Add action to launch usbipd and dettach: powershell Start-Process -FilePath ‘{your_path}\usbipd.exe’ -ArgumentList ‘-d -4’
    6.4 Add action to start VM: powershell Start-VM ‘{your_VM_name}’
    6.5 by doing above we guarantee the VM only starts after usbipd is already running. You need to run usbip using powershell Start-Process and do not call the exe directly. otherwise it will run a infinite loop and you can never pass that step and proceed to start the VM.
    6.6 In Debian create a usbip.conf in /etc/modules-load.d to auto load module
    6.7 In Debian create a service to auto load “usbip attach …” after network-online, enable service by systemctl enable usbip.service

  7. Restart the system. by design the VM should shutdown when system is restarted. Upon logon, the task scheduler sequentially starts usbip binding, launches usbipd daemon and starts Debian VM. Inside Debian VM the vhci-hcd module gets loaded and the usbip service gets called to attach the remote usb device. Once HA core gets loaded we should have a fully functional system. Now we can finally sit back and enjoy the home automation.


Comments