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

88 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test script to verify that appropriate messages are printed when no template match is found.
This script:
1. Gets a test device from current.json
2. Temporarily modifies the config_other section to ensure no match will be found
3. Calls the check_and_update_template method
4. Verifies that appropriate messages are printed
"""
import json
import logging
import sys
import os
# 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__)
# Import TasmotaManager class
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from TasmotaManager import TasmotaDiscovery
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 main():
"""Main test function"""
# 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})")
# Create a TasmotaDiscovery instance
discovery = TasmotaDiscovery(debug=True)
# Load the configuration
discovery.load_config('network_configuration.json')
# Temporarily modify the config_other section to ensure no match will be found
# Save the original config_other
original_config_other = discovery.config.get('config_other', {})
# Set an empty config_other to ensure no match
discovery.config['config_other'] = {
"NonExistentDevice": '{"NAME":"Test Device","GPIO":[0,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":18}'
}
logger.info("Modified config_other to ensure no match will be found")
# Call the check_and_update_template method
logger.info("Calling check_and_update_template method")
result = discovery.check_and_update_template(device_ip, device_name)
# Verify the result
logger.info(f"Result of check_and_update_template: {result}")
# Restore the original config_other
discovery.config['config_other'] = original_config_other
logger.info("Test completed. Check the output above to verify that appropriate messages were printed.")
return 0
if __name__ == "__main__":
sys.exit(main())