From 6696c79fdc437335c05c839bb5a51cf474ad2af6 Mon Sep 17 00:00:00 2001 From: bullshit Date: Fri, 8 Aug 2025 15:31:13 +0200 Subject: [PATCH] refactor(tools): recover functionality after renaming component Signed-off-by: bullshit --- tools/README.md | 16 +++++----- tools/generate_vendor_list.py | 57 ++++++++++++++++++++--------------- tools/scan-usb.sh | 2 +- 3 files changed, 41 insertions(+), 34 deletions(-) mode change 100644 => 100755 tools/generate_vendor_list.py diff --git a/tools/README.md b/tools/README.md index dc5a65e..a06fd3b 100644 --- a/tools/README.md +++ b/tools/README.md @@ -1,6 +1,6 @@ # Development Tools -This folder contains development tools for the NUT UPS ESPHome component. +This folder contains development tools for the UPS HID ESPHome component. ## scan-usb.sh @@ -12,9 +12,9 @@ This script keeps the UPS vendor ID lists synchronized between C++, scripts and ### Purpose -The NUT UPS component maintains a list of known UPS vendor IDs in two places: -- `components/nut_ups/ups_vendors.h` (C++ runtime code) -- `components/nut_ups/__init__.py` (Python configuration validation) +The UPS HID component maintains a list of known UPS vendor IDs in two places: +- `components/ups_hid/ups_vendors.h` (C++ runtime code) +- `components/ups_hid/__init__.py` (Python configuration validation) This script extracts the vendor list from the C++ header file and updates the Python file to ensure they stay in sync. @@ -36,7 +36,7 @@ Or use the keyboard shortcut `Ctrl+Shift+P` -> "Tasks: Run Build Task" and selec ### When to Run Run this script whenever you: -1. Add new UPS vendor IDs to `components/nut_ups/ups_vendors.h` +1. Add new UPS vendor IDs to `components/ups_hid/ups_vendors.h` 2. Modify existing vendor information 3. Want to ensure the C++ and Python lists are synchronized @@ -51,16 +51,16 @@ The script will: ### Example Output ``` -Parsing vendors from /workspace/components/nut_ups/ups_vendors.h... +Parsing vendors from /workspace/components/ups_hid/ups_vendors.h... Found 7 vendors -Updated /workspace/components/nut_ups/__init__.py +Updated /workspace/components/ups_hid/__init__.py ``` ## Adding New Vendors To add a new UPS vendor: -1. Edit `components/nut_ups/ups_vendors.h` +1. Edit `components/ups_hid/ups_vendors.h` 2. Add a new entry to the `KNOWN_UPS_VENDORS` array: ```cpp {0x1234, "New Vendor", "New Vendor Description"}, diff --git a/tools/generate_vendor_list.py b/tools/generate_vendor_list.py old mode 100644 new mode 100755 index f26c2d7..c4543c4 --- a/tools/generate_vendor_list.py +++ b/tools/generate_vendor_list.py @@ -13,16 +13,23 @@ def parse_vendor_header(header_path): vendors = {} with open(header_path, "r") as f: - content = f.read() + lines = f.readlines() - # Find the KNOWN_UPS_VENDORS array + # Find and parse only uncommented entries in KNOWN_UPS_VENDORS array pattern = r'\{0x([0-9A-Fa-f]+),\s*"([^"]+)",\s*"([^"]+)"\}' - matches = re.findall(pattern, content) - - for match in matches: - vendor_id_str, name, description = match - vendor_id = int(vendor_id_str, 16) - vendors[vendor_id] = {"name": name, "description": description} + + for line in lines: + # Skip commented lines (starting with // or /* or whitespace + //) + stripped_line = line.strip() + if stripped_line.startswith('//') or stripped_line.startswith('/*'): + continue + + # Look for vendor entries in uncommented lines only + match = re.search(pattern, line) + if match: + vendor_id_str, name, description = match.groups() + vendor_id = int(vendor_id_str, 16) + vendors[vendor_id] = {"name": name, "description": description} return vendors @@ -114,40 +121,40 @@ def update_scan_usb_vendors(scan_usb_path, vendors): return False with open(scan_usb_path, "r") as f: - content = f.read() + lines = f.readlines() # 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 - new_line = f'lsusb | grep -iE "({vendor_pattern})"' + new_line = f'lsusb | grep -iE "({vendor_pattern})" || echo "No UPS devices found"\n' - # 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)' - r'lsusb \| grep -iE "\([^"]*\)"' - ) - replacement = rf'\1{new_line}' + # Find the line that contains the UPS vendor grep pattern + found_line = False + for i, line in enumerate(lines): + # Look for the line after "UPS devices:" that contains lsusb | grep -iE + if (i > 0 and + "UPS devices:" in lines[i-1] and + line.strip().startswith("lsusb | grep -iE") and + "UPS devices found" in line): + lines[i] = new_line + found_line = True + break - new_content = re.sub(ups_section_pattern, replacement, content) - - if new_content != content: + if found_line: with open(scan_usb_path, "w") as f: - f.write(new_content) + f.writelines(lines) return True else: - print( - f"Warning: Could not find UPS devices section in {scan_usb_path}" - ) + print(f"Warning: Could not find UPS devices grep line in {scan_usb_path}") return False def main(): """Update __init__.py, setup.sh, and scan-usb.sh with vendor list.""" script_dir = Path(__file__).parent - component_dir = script_dir.parent / "components" / "nut_ups" + component_dir = script_dir.parent / "components" / "ups_hid" header_path = component_dir / "ups_vendors.h" init_path = component_dir / "__init__.py" setup_path = script_dir.parent / ".devcontainer" / "setup.sh" diff --git a/tools/scan-usb.sh b/tools/scan-usb.sh index ca032ed..465a687 100755 --- a/tools/scan-usb.sh +++ b/tools/scan-usb.sh @@ -9,7 +9,7 @@ lsusb | grep -iE "(303a|10c4|1a86)" || echo "No ESP32 boards found" echo "" # Auto-generated from ups_vendors.h echo "UPS devices:" -lsusb | grep -iE "(0001|0463|050d|051d|05b8|0665|06da|0764|0925|09ae|09d6)" || echo "No UPS devices found" +lsusb | grep -iE "(0463|050d|051d|05b8|0665|06da|0764|0925|09ae|09d6)" || echo "No UPS devices found" echo "" echo "Serial ports:" ls -la /dev/tty{USB,ACM}* 2>/dev/null || echo "No serial ports found"