fix: scan-usb also autogenerate vendor ids

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-07 08:45:57 +02:00
1 parent 92d3e9b835
commit 483e735e11
3 files changed
+110 -4

No files matched your search

+42 -1
View File
@@ -102,16 +102,52 @@ def update_udev_rules_in_setup_sh(setup_path, udev_rules):
with open(setup_path, "w") as f: with open(setup_path, "w") as f:
f.write(new_content) f.write(new_content)
# @TODO: reload udev if possible
return True 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(): 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 script_dir = Path(__file__).parent
component_dir = script_dir.parent / "components" / "nut_ups" component_dir = script_dir.parent / "components" / "nut_ups"
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"
scan_usb_path = script_dir / "scan-usb.sh"
if not header_path.exists(): if not header_path.exists():
print(f"Error: {header_path} not found!") 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" 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 return 0
+3 -3
View File
@@ -5,11 +5,11 @@ echo "All USB devices:"
lsusb lsusb
echo "" echo ""
echo "ESP32 boards:" 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 "" echo ""
# @TODO: update this with generate_vendor_list.py # Auto-generated from ups_vendors.h
echo "UPS devices:" 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 ""
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"
+65
View File
@@ -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