Public Access
refactor(tools): recover functionality after renaming component
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
3 files changed
+38
-31
No files matched your search
+8
-8
@@ -1,6 +1,6 @@
|
|||||||
# Development Tools
|
# 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
|
## scan-usb.sh
|
||||||
|
|
||||||
@@ -12,9 +12,9 @@ This script keeps the UPS vendor ID lists synchronized between C++, scripts and
|
|||||||
|
|
||||||
### Purpose
|
### Purpose
|
||||||
|
|
||||||
The NUT UPS component maintains a list of known UPS vendor IDs in two places:
|
The UPS HID component maintains a list of known UPS vendor IDs in two places:
|
||||||
- `components/nut_ups/ups_vendors.h` (C++ runtime code)
|
- `components/ups_hid/ups_vendors.h` (C++ runtime code)
|
||||||
- `components/nut_ups/__init__.py` (Python configuration validation)
|
- `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.
|
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
|
### When to Run
|
||||||
|
|
||||||
Run this script whenever you:
|
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
|
2. Modify existing vendor information
|
||||||
3. Want to ensure the C++ and Python lists are synchronized
|
3. Want to ensure the C++ and Python lists are synchronized
|
||||||
|
|
||||||
@@ -51,16 +51,16 @@ The script will:
|
|||||||
### Example Output
|
### 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
|
Found 7 vendors
|
||||||
Updated /workspace/components/nut_ups/__init__.py
|
Updated /workspace/components/ups_hid/__init__.py
|
||||||
```
|
```
|
||||||
|
|
||||||
## Adding New Vendors
|
## Adding New Vendors
|
||||||
|
|
||||||
To add a new UPS vendor:
|
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:
|
2. Add a new entry to the `KNOWN_UPS_VENDORS` array:
|
||||||
```cpp
|
```cpp
|
||||||
{0x1234, "New Vendor", "New Vendor Description"},
|
{0x1234, "New Vendor", "New Vendor Description"},
|
||||||
|
|||||||
Regular → Executable
+29
-22
@@ -13,14 +13,21 @@ def parse_vendor_header(header_path):
|
|||||||
vendors = {}
|
vendors = {}
|
||||||
|
|
||||||
with open(header_path, "r") as f:
|
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*"([^"]+)"\}'
|
pattern = r'\{0x([0-9A-Fa-f]+),\s*"([^"]+)",\s*"([^"]+)"\}'
|
||||||
matches = re.findall(pattern, content)
|
|
||||||
|
|
||||||
for match in matches:
|
for line in lines:
|
||||||
vendor_id_str, name, description = match
|
# 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)
|
vendor_id = int(vendor_id_str, 16)
|
||||||
vendors[vendor_id] = {"name": name, "description": description}
|
vendors[vendor_id] = {"name": name, "description": description}
|
||||||
|
|
||||||
@@ -114,40 +121,40 @@ def update_scan_usb_vendors(scan_usb_path, vendors):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
with open(scan_usb_path, "r") as f:
|
with open(scan_usb_path, "r") as f:
|
||||||
content = f.read()
|
lines = f.readlines()
|
||||||
|
|
||||||
# Generate the vendor ID pattern for grep
|
# Generate the vendor ID pattern for grep
|
||||||
vendor_ids = [f"{vid:04x}" for vid in sorted(vendors.keys())]
|
vendor_ids = [f"{vid:04x}" for vid in sorted(vendors.keys())]
|
||||||
vendor_pattern = "|".join(vendor_ids)
|
vendor_pattern = "|".join(vendor_ids)
|
||||||
|
|
||||||
# Find and replace the UPS devices grep line
|
# 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
|
# Find the line that contains the UPS vendor grep pattern
|
||||||
import re
|
found_line = False
|
||||||
ups_section_pattern = (
|
for i, line in enumerate(lines):
|
||||||
r'(# Auto-generated from ups_vendors\.h\necho "UPS devices:"\n)'
|
# Look for the line after "UPS devices:" that contains lsusb | grep -iE
|
||||||
r'lsusb \| grep -iE "\([^"]*\)"'
|
if (i > 0 and
|
||||||
)
|
"UPS devices:" in lines[i-1] and
|
||||||
replacement = rf'\1{new_line}'
|
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 found_line:
|
||||||
|
|
||||||
if new_content != content:
|
|
||||||
with open(scan_usb_path, "w") as f:
|
with open(scan_usb_path, "w") as f:
|
||||||
f.write(new_content)
|
f.writelines(lines)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print(
|
print(f"Warning: Could not find UPS devices grep line in {scan_usb_path}")
|
||||||
f"Warning: Could not find UPS devices section in {scan_usb_path}"
|
|
||||||
)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Update __init__.py, setup.sh, and scan-usb.sh with vendor list."""
|
"""Update __init__.py, setup.sh, and scan-usb.sh with vendor list."""
|
||||||
script_dir = Path(__file__).parent
|
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"
|
header_path = component_dir / "ups_vendors.h"
|
||||||
init_path = component_dir / "__init__.py"
|
init_path = component_dir / "__init__.py"
|
||||||
setup_path = script_dir.parent / ".devcontainer" / "setup.sh"
|
setup_path = script_dir.parent / ".devcontainer" / "setup.sh"
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ lsusb | grep -iE "(303a|10c4|1a86)" || echo "No ESP32 boards found"
|
|||||||
echo ""
|
echo ""
|
||||||
# Auto-generated from ups_vendors.h
|
# Auto-generated from ups_vendors.h
|
||||||
echo "UPS devices:"
|
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 ""
|
||||||
echo "Serial ports:"
|
echo "Serial ports:"
|
||||||
ls -la /dev/tty{USB,ACM}* 2>/dev/null || echo "No serial ports found"
|
ls -la /dev/tty{USB,ACM}* 2>/dev/null || echo "No serial ports found"
|
||||||
|
|||||||
Reference in new issue
Block a user