#!/usr/bin/env python3 """ Test script to verify the optimization for processing unknown devices. This script will run TasmotaManager with the --process-unknown flag and verify that it only processes devices that match the unknown_device_patterns. """ import sys import logging import subprocess import os import json # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger("ProcessUnknownTest") def test_process_unknown_optimization(): """Test that the --process-unknown flag skips detailed information gathering for non-matching devices.""" logger.info("Testing process-unknown optimization") # Check if current.json exists if not os.path.exists('current.json'): logger.error("current.json not found. Run discovery first.") return False # Run TasmotaManager with --process-unknown flag and capture output logger.info("Running TasmotaManager with --process-unknown flag") try: result = subprocess.run( ["python", "TasmotaManager.py", "--process-unknown", "--skip-unifi", "--debug"], capture_output=True, text=True, check=True ) output = result.stdout + result.stderr logger.info("TasmotaManager completed successfully") except subprocess.CalledProcessError as e: logger.error(f"Error running TasmotaManager: {e}") logger.error(f"Output: {e.stdout}") logger.error(f"Error: {e.stderr}") return False # Check that the output contains "Processing unknown devices" but not "Getting detailed version information" if "Step 2: Processing unknown devices" in output and "Getting detailed version information" not in output: logger.info("Verified that detailed version information gathering was skipped") else: logger.error("Failed to verify that detailed version information gathering was skipped") return False # Check the log for evidence that only unknown devices were processed unknown_devices_processed = 0 for line in output.splitlines(): if "Processing unknown device:" in line: unknown_devices_processed += 1 logger.info(f"Found log entry: {line.strip()}") logger.info(f"Found {unknown_devices_processed} unknown devices processed") # Load network_configuration.json to get unknown_device_patterns try: with open('network_configuration.json', 'r') as f: config = json.load(f) network_filters = config['unifi'].get('network_filter', {}) unknown_patterns = [] for network in network_filters.values(): unknown_patterns.extend(network.get('unknown_device_patterns', [])) logger.info(f"Found {len(unknown_patterns)} unknown device patterns in configuration") for pattern in unknown_patterns: logger.info(f" - {pattern}") except Exception as e: logger.error(f"Error loading configuration: {e}") return False logger.info("Test completed successfully") return True def main(): """Main function to run the test.""" print("Testing process-unknown optimization") result = test_process_unknown_optimization() if result: print("\nSUCCESS: The optimization for processing unknown devices is working correctly") print("The script only processes devices that match the unknown_device_patterns") sys.exit(0) else: print("\nFAILURE: The optimization for processing unknown devices is not working correctly") sys.exit(1) if __name__ == "__main__": main()