TasmotaManager/tests/test_unifi_hostname_bug_flag.py
2025-10-28 00:21:08 +00:00

116 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify the unifi_hostname_bug_detected flag is set correctly.
This script will:
1. Run TasmotaManager with --Device parameter for a device with the UniFi OS hostname bug
2. Check if the unifi_hostname_bug_detected flag is set correctly in the output
"""
import sys
import subprocess
import json
import logging
# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
def get_test_device():
"""Get a test device from current.json"""
try:
with open('current.json', 'r') as f:
data = json.load(f)
devices = data.get('tasmota', {}).get('devices', [])
if devices:
return devices[0] # Use the first device
else:
logger.error("No devices found in current.json")
return None
except Exception as e:
logger.error(f"Error reading current.json: {e}")
return None
def run_device_mode(device_ip):
"""Run TasmotaManager in Device mode with the given IP"""
try:
cmd = ["python3", "TasmotaManager.py", "--Device", device_ip, "--debug"]
logger.info(f"Running command: {' '.join(cmd)}")
# Run the command and capture output
process = subprocess.run(cmd, capture_output=True, text=True)
# Log the output
logger.info("Command output:")
for line in process.stdout.splitlines():
logger.info(f" {line}")
if process.returncode != 0:
logger.error(f"Command failed with return code {process.returncode}")
logger.error(f"Error output: {process.stderr}")
return False
return True
except Exception as e:
logger.error(f"Error running TasmotaManager: {e}")
return False
def check_tasmota_devices_json():
"""Check if the unifi_hostname_bug_detected flag is set in TasmotaDevices.json"""
try:
with open('TasmotaDevices.json', 'r') as f:
data = json.load(f)
devices = data.get('devices', [])
if not devices:
logger.error("No devices found in TasmotaDevices.json")
return False
# Check each device for the flag
for device in devices:
name = device.get('name', 'Unknown')
ip = device.get('ip', '')
bug_detected = device.get('unifi_hostname_bug_detected', None)
if bug_detected is None:
logger.error(f"Device {name} ({ip}) does not have the unifi_hostname_bug_detected flag")
return False
logger.info(f"Device {name} ({ip}) has unifi_hostname_bug_detected = {bug_detected}")
return True
except Exception as e:
logger.error(f"Error reading TasmotaDevices.json: {e}")
return False
def main():
# Get a test device
device = get_test_device()
if not device:
logger.error("No test device available. Run discovery first.")
return 1
device_name = device.get('name')
device_ip = device.get('ip')
logger.info(f"Testing with device: {device_name} (IP: {device_ip})")
# Run TasmotaManager in Device mode
logger.info(f"Running TasmotaManager in Device mode for {device_ip}")
success = run_device_mode(device_ip)
if not success:
logger.error("Failed to run TasmotaManager in Device mode")
return 1
# Check if the flag is set correctly
logger.info("Checking if the unifi_hostname_bug_detected flag is set correctly")
if check_tasmota_devices_json():
logger.info("SUCCESS: unifi_hostname_bug_detected flag is set correctly!")
return 0
else:
logger.error("FAILURE: unifi_hostname_bug_detected flag is not set correctly!")
return 1
if __name__ == "__main__":
sys.exit(main())