Package: pyhid-usb-relay
Python Utility for controlling HID USB RelaysThis utility is a Python version ofusb-relay-hidInstallationThe latest version can be installed using pip:python3 -m pip install pyhid-usb-relayManual InstallationThe library can be manually installed usingpoetry:poetry installFrom here, you can either run thepyhid-usb-relaycommand with:poetry run pyhid-usb-relayIf you want to build a wheel for installation, runpoetry buildUsageStandalone app outside of pythonpyhid-usb-relay--helpInside a python app/scriptimport pyhid_usb_relay

relay = pyhid_usb_relay.find()

print(relay.state)
print("Toggeling relay")

relay.toggle_state(1)

print(relay.state)ConfigurationRelay configuration is read from the file$XDG_CONFIG_HOME/usb-hid-relay/config.yaml(usually~/.config/usb-hid-relay/config.yaml). This YAML file should contain a top
level dictionary key for each relay serial number to be configured, like so:5291D:defaults:...aliases:...The following properties may be defined in thedefaultsection and apply to
all relays on the board:invert- A boolean that indicates if the relay logic should be invertedpulse-time- A floating point number of seconds the relay should remain in
the opposite state whenpyhid-usb-relay toggle --pulseis calledAliases are created by adding a new key underaliaseswith arelayproperty
indicating which relay number the alias controls. For example the following
config creates an alias calledfoothat may be used in place of relay number
2 in the API:5219D:aliases:foo:relay:2Aliases may also define any of the properties listed indefaults, in which
case they only apply when the specific alias is used. Note that these
properties apply to thealiasnot the relay number. Using a relay number in
the API will only apply the defaultsAn example configuration is show here:# Define properties for relay board with serial 5291D5291D:defaults:invert:true# Invert all relays by defaultpulse-time:5.0# Default pulse time is 5 seconds for this boardaliases:foo:# Create an alias called "foo"relay:1# This alias controls relay 1invert:false# Don't invert this alias (overrides the default)pulse-time:1.0# Override default pulse-time for this aliasPermissionsIf you want to access the relay devices as a normal user (which is recommended,
since it will respect your local configuration), you will need to modify your
udev rules to allow access to the HID device. You can do this by creating a
file named/etc/udev/rules.d/90-hidusb-relay.ruleswith the following
contents:# Give all users access to USB HID Relay
SUBSYSTEM=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", MODE:="0660", GROUP="dialout"You may need to reload your udev rules withsudo udevadm control --reload-rulesand unplug and reattach the USB relay board for this to take
effectNOTEThis rule allows any user that is part of thedialoutgroup to
access the board. If this is not what you want, you should change the udev
rules.Fixing Serial NumbersThe relays that this tool is designed to control have a quirk that they all
report the same USB Vendor, Product, and Serial Number (The serial number used
by this code is retrieved by the HID API). This can make it hard to distinguish
between multiple relays attached to the same device using udev rules.Thepyhid-usb-relaytool can be used to help resolve this by using theget-serialsubcommand, which will fetch the HID serial number from the device
with a udev rule that looks like:SUBSYSTEM=="usb", ATTR{idVendor}=="16c0", ATTR{idProduct}=="05df", ACTION=="add", PROGRAM="/usr/local/bin/pyhid-usb-relay get-serial '%E{BUSNUM}' '%E{DEVNUM}'", ENV{ID_SERIAL}:="%c"