From 483e735e11d1580f420d9c1a47f881b3a3f81a29 Mon Sep 17 00:00:00 2001 From: bullshit Date: Thu, 7 Aug 2025 08:45:57 +0200 Subject: [PATCH] fix: scan-usb also autogenerate vendor ids Signed-off-by: bullshit --- tools/generate_vendor_list.py | 43 ++++++++++++++++++++++- tools/scan-usb.sh | 6 ++-- vendor-validation-test.yaml | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 vendor-validation-test.yaml diff --git a/tools/generate_vendor_list.py b/tools/generate_vendor_list.py index ca08c53..08bda49 100644 --- a/tools/generate_vendor_list.py +++ b/tools/generate_vendor_list.py @@ -102,16 +102,52 @@ def update_udev_rules_in_setup_sh(setup_path, udev_rules): with open(setup_path, "w") as f: f.write(new_content) + # @TODO: reload udev if possible + return True +def update_scan_usb_vendors(scan_usb_path, vendors): + """Update vendor list in scan-usb.sh file.""" + if not scan_usb_path.exists(): + print(f"Warning: {scan_usb_path} not found!") + return False + + with open(scan_usb_path, "r") as f: + content = f.read() + + # Generate the vendor ID pattern for grep + vendor_ids = [f"{vid:04x}" for vid in sorted(vendors.keys())] + vendor_pattern = "|".join(vendor_ids) + + # Find and replace the UPS devices grep line + old_pattern = r'lsusb \| grep -iE "\([^"]*\)"' + new_line = f'lsusb | grep -iE "({vendor_pattern})"' + + # Replace the grep line in the UPS devices section + import re + ups_section_pattern = r'(# Auto-generated from ups_vendors\.h\necho "UPS devices:"\n)lsusb \| grep -iE "\([^"]*\)"' + replacement = rf'\1{new_line}' + + new_content = re.sub(ups_section_pattern, replacement, content) + + if new_content != content: + with open(scan_usb_path, "w") as f: + f.write(new_content) + return True + else: + print(f"Warning: Could not find UPS devices section in {scan_usb_path}") + return False + + def main(): - """Main function to update __init__.py and setup.sh with current vendor list.""" + """Main function to update __init__.py, setup.sh, and scan-usb.sh with current vendor list.""" script_dir = Path(__file__).parent component_dir = script_dir.parent / "components" / "nut_ups" header_path = component_dir / "ups_vendors.h" init_path = component_dir / "__init__.py" setup_path = script_dir.parent / ".devcontainer" / "setup.sh" + scan_usb_path = script_dir / "scan-usb.sh" if not header_path.exists(): print(f"Error: {header_path} not found!") @@ -156,6 +192,11 @@ def main(): f" Added {len([r for r in udev_rules if 'UPS' not in r])} standard + {len(vendors)} UPS vendor rules" ) + # Update scan-usb.sh vendor list + if update_scan_usb_vendors(scan_usb_path, vendors): + print(f"✅ Updated vendor list in {scan_usb_path}") + print(f" UPS vendor pattern: ({len(vendors)} vendors)") + return 0 diff --git a/tools/scan-usb.sh b/tools/scan-usb.sh index b620ce1..23295bf 100755 --- a/tools/scan-usb.sh +++ b/tools/scan-usb.sh @@ -5,11 +5,11 @@ echo "All USB devices:" lsusb echo "" echo "ESP32 boards:" -lsusb | grep -E "(303a|10c4|1a86)" || echo "No ESP32 boards found" +lsusb | grep -iE "(303a|10c4|1a86)" || echo "No ESP32 boards found" echo "" -# @TODO: update this with generate_vendor_list.py +# Auto-generated from ups_vendors.h echo "UPS devices:" -lsusb | grep -E "(051D|0764|09AE|06DA|0665|0463|06da|0001)" || echo "No UPS devices found" +lsusb | grep -iE "(0001|0463|051d|0665|06da|0764|09ae)" || echo "No UPS devices found" echo "" echo "Serial ports:" ls -la /dev/tty{USB,ACM}* 2>/dev/null || echo "No serial ports found" diff --git a/vendor-validation-test.yaml b/vendor-validation-test.yaml new file mode 100644 index 0000000..57f6d87 --- /dev/null +++ b/vendor-validation-test.yaml @@ -0,0 +1,65 @@ +substitutions: + name: vendor-validation-test + friendly_name: "UPS Vendor Validation Test" + +esphome: + name: "${name}" + friendly_name: "${friendly_name}" + comment: "Test configuration to validate unknown vendor ID handling" + +esp32: + board: esp32-s3-devkitc-1 + flash_size: 16MB + framework: + type: esp-idf + +logger: + level: VERBOSE + logs: + nut_ups: DEBUG + +api: + encryption: + key: !secret api_encryption_key + +ota: + - platform: esphome + password: !secret ota_password + +wifi: + ssid: !secret wifi_ssid + password: !secret wifi_password + +external_components: + - source: + type: local + path: components + +# Test with unknown vendor ID to trigger validation warning +nut_ups: + id: ups_monitor + update_interval: 30s + usb_vendor_id: 0x9999 # Unknown vendor ID - should trigger warning + usb_product_id: 0x0001 + auto_detect_protocol: true + protocol_timeout: 15s + simulation_mode: true + +sensor: + - platform: nut_ups + nut_ups_id: ups_monitor + name: "Battery Level" + type: battery_level + unit_of_measurement: "%" + +binary_sensor: + - platform: nut_ups + nut_ups_id: ups_monitor + name: "UPS Online" + type: online + +text_sensor: + - platform: nut_ups + nut_ups_id: ups_monitor + name: "UPS Status" + type: status \ No newline at end of file