170 lines
6.2 KiB
Python
170 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import requests
|
|
import time
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
# Add the current directory to the path so we can import TasmotaManager
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
from TasmotaManager import TasmotaDiscovery
|
|
|
|
# Set up logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Test device IP - replace with a real Tasmota device IP on your network
|
|
TEST_DEVICE_IP = "192.168.8.184" # Using the first device from TasmotaDevices.json
|
|
|
|
def reset_retain_parameters():
|
|
"""Reset all retain parameters to a known state"""
|
|
logger.info("Resetting all retain parameters to a known state")
|
|
|
|
# Reset ButtonRetain
|
|
url = f"http://{TEST_DEVICE_IP}/cm?cmnd=ButtonRetain%20Off"
|
|
response = requests.get(url, timeout=5)
|
|
logger.info(f"Reset ButtonRetain to Off: {response.text}")
|
|
|
|
# Reset SwitchRetain
|
|
url = f"http://{TEST_DEVICE_IP}/cm?cmnd=SwitchRetain%20Off"
|
|
response = requests.get(url, timeout=5)
|
|
logger.info(f"Reset SwitchRetain to Off: {response.text}")
|
|
|
|
# Reset PowerRetain
|
|
url = f"http://{TEST_DEVICE_IP}/cm?cmnd=PowerRetain%20Off"
|
|
response = requests.get(url, timeout=5)
|
|
logger.info(f"Reset PowerRetain to Off: {response.text}")
|
|
|
|
# Wait for commands to take effect
|
|
time.sleep(1)
|
|
|
|
def check_retain_status():
|
|
"""Check the current status of retain parameters on the device"""
|
|
logger.info("Checking retain parameters status")
|
|
|
|
# Check ButtonRetain
|
|
url = f"http://{TEST_DEVICE_IP}/cm?cmnd=ButtonRetain"
|
|
response = requests.get(url, timeout=5)
|
|
button_retain = response.text
|
|
logger.info(f"ButtonRetain status: {button_retain}")
|
|
|
|
# Check SwitchRetain
|
|
url = f"http://{TEST_DEVICE_IP}/cm?cmnd=SwitchRetain"
|
|
response = requests.get(url, timeout=5)
|
|
switch_retain = response.text
|
|
logger.info(f"SwitchRetain status: {switch_retain}")
|
|
|
|
# Check PowerRetain
|
|
url = f"http://{TEST_DEVICE_IP}/cm?cmnd=PowerRetain"
|
|
response = requests.get(url, timeout=5)
|
|
power_retain = response.text
|
|
logger.info(f"PowerRetain status: {power_retain}")
|
|
|
|
return button_retain, switch_retain, power_retain
|
|
|
|
def test_retain_parameters():
|
|
"""Test the retain parameters handling"""
|
|
logger.info("Testing retain parameters handling")
|
|
|
|
# Create a minimal configuration for testing
|
|
test_config = {
|
|
"mqtt": {
|
|
"console": {
|
|
"ButtonRetain": "On",
|
|
"SwitchRetain": "On",
|
|
"PowerRetain": "On"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Create a TasmotaDiscovery instance
|
|
discovery = TasmotaDiscovery(debug=True)
|
|
|
|
# Set the config directly
|
|
discovery.config = test_config
|
|
|
|
# Create a function to simulate the retain parameter handling
|
|
def process_retain_params():
|
|
console_params = test_config["mqtt"]["console"]
|
|
retain_params = ["ButtonRetain", "SwitchRetain", "PowerRetain"]
|
|
processed_params = []
|
|
|
|
logger.info(f"Console parameters: {console_params}")
|
|
|
|
# Process Retain parameters
|
|
for param in retain_params:
|
|
if param in console_params:
|
|
final_value = console_params[param]
|
|
# Set opposite state first
|
|
opposite_value = "On" if final_value.lower() == "off" else "Off"
|
|
|
|
logger.info(f"Setting {param} to {opposite_value} (step 1 of 2)")
|
|
processed_params.append((param, opposite_value))
|
|
|
|
logger.info(f"Setting {param} to {final_value} (step 2 of 2)")
|
|
processed_params.append((param, final_value))
|
|
|
|
# Debug the processed params
|
|
logger.info(f"Processed parameters: {processed_params}")
|
|
|
|
return processed_params
|
|
|
|
# Process the retain parameters
|
|
processed_params = process_retain_params()
|
|
|
|
# Check if all retain parameters were processed correctly
|
|
button_retain_correct = any(param[0] == "ButtonRetain" and param[1] == "Off" for param in processed_params) and \
|
|
any(param[0] == "ButtonRetain" and param[1] == "On" for param in processed_params)
|
|
|
|
switch_retain_correct = any(param[0] == "SwitchRetain" and param[1] == "Off" for param in processed_params) and \
|
|
any(param[0] == "SwitchRetain" and param[1] == "On" for param in processed_params)
|
|
|
|
power_retain_correct = any(param[0] == "PowerRetain" and param[1] == "Off" for param in processed_params) and \
|
|
any(param[0] == "PowerRetain" and param[1] == "On" for param in processed_params)
|
|
|
|
if button_retain_correct and switch_retain_correct and power_retain_correct:
|
|
logger.info("✓ All retain parameters were processed correctly")
|
|
return True
|
|
else:
|
|
logger.error("✗ Retain parameters were not processed correctly")
|
|
return False
|
|
|
|
def main():
|
|
"""Main test function"""
|
|
logger.info("Starting retain parameters test")
|
|
|
|
try:
|
|
# Test using direct device interaction
|
|
logger.info("=== Testing with direct device interaction ===")
|
|
# Reset retain parameters
|
|
reset_retain_parameters()
|
|
|
|
# Check initial state
|
|
initial_button, initial_switch, initial_power = check_retain_status()
|
|
logger.info(f"Initial state - ButtonRetain: {initial_button}, SwitchRetain: {initial_switch}, PowerRetain: {initial_power}")
|
|
|
|
# Test using TasmotaManager code
|
|
logger.info("\n=== Testing with TasmotaManager code ===")
|
|
tasmota_manager_success = test_retain_parameters()
|
|
|
|
# Overall success
|
|
if tasmota_manager_success:
|
|
logger.info("TEST PASSED: TasmotaManager retain parameters handling works correctly")
|
|
return 0
|
|
else:
|
|
logger.error("TEST FAILED: TasmotaManager retain parameters handling did not work as expected")
|
|
return 1
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error during test: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return 1
|
|
|
|
if __name__ == "__main__":
|
|
main() |