Files
salt-states/_grains/lsusb.py
2025-12-27 18:46:18 -05:00

33 lines
906 B
Python

import re
import logging
from subprocess import check_output, CalledProcessError
log = logging.getLogger(__name__)
def main():
dev_re = re.compile(r"Bus\s+(\d+)\s+Device\s+(\d+):\s+ID\s(\w+:\w+)\s(.+)$")
try:
lsusb = check_output("lsusb", encoding='UTF-8')
except (CalledProcessError, OSError) as exc:
log.debug(exc)
return {}
devices = []
corsair_aio = None
for line in lsusb.splitlines():
info = dev_re.match(line)
if not info:
continue
bus, device, dev_id, tag = info.groups()
dev_info = {
"device": f"/dev/bus/usb/{bus}/{device}",
"id": dev_id,
"tag": tag,
}
if tag.startswith('Corsair H'):
corsair_aio = tag.split(maxsplit=3)[1]
devices.append(dev_info)
return {
'corsair_aio': corsair_aio,
'lsusb': devices,
}