Skip to content

Instantly share code, notes, and snippets.

@ffeldner
Last active November 4, 2024 04:20
Show Gist options
  • Save ffeldner/8bead7d9f56bc78e24f5dfedb468fc8a to your computer and use it in GitHub Desktop.
Save ffeldner/8bead7d9f56bc78e24f5dfedb468fc8a to your computer and use it in GitHub Desktop.
automatically reattaches zigbee usb device to homeassistant VM and starts the addon after attach - modified and extended from https://gist.github.com/nickpegg/417cf5024b765c3c92cbfbd725310091/
# File location: /etc/udev/rules.d/90-libvirt-usb.rules
ACTION=="bind", \
SUBSYSTEM=="usb", \
ENV{ID_VENDOR_ID}=="10c4", \
ENV{ID_MODEL_ID}=="ea60", \
RUN+="/usr/local/bin/kvm-udev.sh attach <DOMAIN>"
ACTION=="remove", \
SUBSYSTEM=="usb", \
ENV{ID_VENDOR_ID}=="10c4", \
ENV{ID_MODEL_ID}=="ea60", \
RUN+="/usr/local/bin/kvm-udev.sh detach <DOMAIN>"
#!/bin/bash
# File location: /usr/local/bin/kvm-udev.sh
# Usage: ./kvm-udev.sh attach|detach <domain>
set -ex
ACTION="$1"
DOMAIN="$2"
CONF_FILE="$(mktemp --suffix=.kvm-udev)"
cat << EOF >"$CONF_FILE"
<hostdev mode='subsystem' type='${SUBSYSTEM}'>
<source>
<vendor id='0x${ID_VENDOR_ID}' />
<product id='0x${ID_MODEL_ID}' />
</source>
</hostdev>
EOF
virsh "${ACTION}-device" --domain "$DOMAIN" --file "$CONF_FILE" --live
rm "$CONF_FILE"
if [[ "$ACTION" = "attach" ]]; then
curl \
-H "Authorization: Bearer <TOKEN>" \
-H "Content-Type: application/json" \
-d '{"addon": "45df7312_zigbee2mqtt"}' \
http://<homeassistanthostname>:8123/api/services/hassio/addon_start
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment