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

154 lines
5.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test script to verify the issue with an extra '=' being added to the beginning of the FullTopic value.
This script will:
1. Connect to a Tasmota device
2. Check the current FullTopic value
3. Set the FullTopic parameter using the current code
4. Verify if an extra '=' is being added to the beginning of the value
"""
import sys
import logging
import requests
import json
import argparse
import time
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("FullTopicEqualsTest")
def load_config():
"""Load the network configuration."""
try:
with open('network_configuration.json', 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Error loading configuration: {str(e)}")
sys.exit(1)
def test_fulltopic_equals_issue(ip_address):
"""Test if an extra '=' is being added to the beginning of the FullTopic value."""
logger.info(f"Testing FullTopic equals issue on device at {ip_address}")
# Load configuration
config = load_config()
mqtt_config = config.get('mqtt', {})
if not mqtt_config:
logger.error("No MQTT configuration found")
return False
# Get the FullTopic value from configuration
full_topic = mqtt_config.get('FullTopic', '%prefix%/%topic%/')
logger.info(f"FullTopic from configuration: {full_topic}")
# First, check the current FullTopic value
try:
status_url = f"http://{ip_address}/cm?cmnd=FullTopic"
response = requests.get(status_url, timeout=5)
if response.status_code == 200:
try:
# Try to parse as JSON
data = response.json()
if isinstance(data, dict) and "FullTopic" in data:
current_value = data["FullTopic"]
else:
current_value = response.text
except:
# If not JSON, use the raw text
current_value = response.text
logger.info(f"Current FullTopic value: {current_value}")
else:
logger.error(f"Failed to get current FullTopic value: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
logger.error(f"Error connecting to device: {str(e)}")
return False
# Set the FullTopic using the current code method
try:
# This is how it's done in TasmotaManager.py
set_url = f"http://{ip_address}/cm?cmnd=FullTopic={full_topic}"
logger.info(f"Setting FullTopic with URL: {set_url}")
response = requests.get(set_url, timeout=5)
# Log the raw response for debugging
logger.info(f"Raw response: {response.text}")
if response.status_code == 200:
try:
# Try to parse as JSON
data = response.json()
logger.info(f"Response JSON: {data}")
except:
logger.info(f"Response is not JSON: {response.text}")
else:
logger.error(f"Failed to set FullTopic: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
logger.error(f"Error setting FullTopic: {str(e)}")
return False
# Wait a moment for the change to take effect
time.sleep(1)
# Verify the FullTopic was set correctly
try:
verify_url = f"http://{ip_address}/cm?cmnd=FullTopic"
response = requests.get(verify_url, timeout=5)
if response.status_code == 200:
try:
# Try to parse as JSON
data = response.json()
if isinstance(data, dict) and "FullTopic" in data:
new_value = data["FullTopic"]
else:
new_value = response.text
except:
# If not JSON, use the raw text
new_value = response.text
logger.info(f"New FullTopic value: {new_value}")
# Check if the value has an extra '=' at the beginning
if new_value.startswith('='):
logger.error(f"ISSUE DETECTED: FullTopic has an extra '=' at the beginning: {new_value}")
return True # Return True to indicate the issue was found
else:
logger.info("FullTopic does not have an extra '=' at the beginning")
return False # Return False to indicate the issue was not found
else:
logger.error(f"Failed to verify FullTopic: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
logger.error(f"Error verifying FullTopic: {str(e)}")
return False
def main():
"""Main function to test the FullTopic equals issue."""
parser = argparse.ArgumentParser(description='Test FullTopic equals issue')
parser.add_argument('ip_address', help='IP address of the Tasmota device to test')
args = parser.parse_args()
if not args.ip_address:
print("Usage: python test_fulltopic_equals_issue.py <ip_address>")
sys.exit(1)
issue_found = test_fulltopic_equals_issue(args.ip_address)
if issue_found:
print("ISSUE CONFIRMED: An extra '=' is being added to the beginning of the FullTopic value")
sys.exit(0)
else:
print("ISSUE NOT FOUND: No extra '=' is being added to the beginning of the FullTopic value")
sys.exit(0)
if __name__ == "__main__":
main()