#!/usr/bin/env python3 """ Test script to verify the FullTopic parameter is set correctly without a %20 prefix. This script will: 1. Connect to a Tasmota device 2. Set the FullTopic parameter 3. Verify the FullTopic is set correctly without a %20 prefix """ import sys import logging import requests import json import argparse # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger("FullTopicTest") 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_setting(ip_address): """Test setting the FullTopic parameter on a device.""" logger.info(f"Testing FullTopic setting 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: 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 fixed method (with = instead of %20) try: 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) if response.status_code == 200: logger.info(f"Response from setting FullTopic: {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 # 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: new_value = response.text logger.info(f"New FullTopic value: {new_value}") # Check if the value contains %20 at the beginning if "%20" in new_value: logger.error("FullTopic still contains %20 - fix not working") return False else: logger.info("FullTopic set correctly without %20") return True 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 fix.""" parser = argparse.ArgumentParser(description='Test FullTopic parameter setting') 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_fix.py ") sys.exit(1) result = test_fulltopic_setting(args.ip_address) if result: print("SUCCESS: FullTopic set correctly without %20 prefix") sys.exit(0) else: print("FAILURE: FullTopic not set correctly") sys.exit(1) if __name__ == "__main__": main()