fix: handle missing USB string descriptors in linux_list_serial_ports#95
Conversation
USB devices are not required to provide manufacturer, product, or serial number string descriptors (indicated by index 0 in the USB device descriptor). When these descriptors are absent the corresponding sysfs files do not exist, causing read_text() to raise FileNotFoundError. Previously the entire OSError catch block skipped the device with `continue`, so any device without a serial number (a common case) was silently excluded from the port list. Fix by reading the optional fields via _read_optional_sysfs(), which returns None on OSError, while keeping mandatory fields (vid, pid, bcd_device, interface_num) in the existing try/except that skips devices that disappear during iteration. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks! Could you run |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #95 +/- ##
==========================================
+ Coverage 91.46% 91.52% +0.05%
==========================================
Files 22 22
Lines 3599 3610 +11
==========================================
+ Hits 3292 3304 +12
+ Misses 307 306 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
If I understand correctly, umockdev should be run on HA? Stef |
Stef |
|
Ah, I didn't realize this was with Home Assistant OS. Ignore me 😄 |
Problem
USB devices are not required to provide manufacturer, product, or serial number string descriptors (indicated by index 0 in the USB device descriptor). When these descriptors are absent, the corresponding sysfs files (
serial,manufacturer,product) do not exist under/sys/bus/usb/devices/<device>/.Previously, these fields were read directly with
.read_text()inside theSerialPortInfo(...)constructor call, outside thetry/except OSErrorblock. A missing sysfs file raisesFileNotFoundError(a subclass ofOSError), which was caught by the outerexcept OSError: continue, causing the entire device to be silently skipped.Affected device
Velleman VMB1USB (VID:
10cf, PID:0b1b) — a CDC-ACM USB serial interface without a USB serial number descriptor. The device appears correctly in/dev/serial/by-id/,/sys/class/tty/, and is detected by pyserial, but was invisible toserialx.list_serial_ports().This caused the device to not appear in the Home Assistant Velbus integration USB port selector, which uses
serialxviahomeassistant.components.usb.async_scan_serial_ports().Fix
Add a
_read_optional_sysfs()helper that returnsNoneonOSErrorinstead of raising. Use it for the three optional USB string descriptor fields (serial_number,manufacturer,product) in both theusb-serialandusb(CDC-ACM) subsystem branches.Mandatory fields (
idVendor,idProduct,bcdDevice,bInterfaceNumber) remain in the existingtry/exceptblock that handles devices disappearing during iteration.This mirrors the existing pattern already used for
interface_description, which was already handled with an explicit.exists()check.