4.1 KiB
Console Duplicate and Template Matching Fix Summary
Issues Addressed
- Duplicate Console Settings: Console settings were being applied twice during device configuration.
- Template Matching Failure: The template matching algorithm was not handling the response format correctly, causing the config_other settings to not be applied.
Changes Made
1. Fix for Duplicate Console Settings
The console settings were being applied in two places:
- In
configure_mqtt_settings()called fromcheck_mqtt_settings() - Directly in
get_device_details()
To fix this issue:
-
Added a
skip_consoleparameter toconfigure_mqtt_settings():def configure_mqtt_settings(self, ip, name, mqtt_status=None, is_new_device=False, set_friendly_name=False, enable_mqtt=False, with_retry=False, reboot=False, skip_console=False): -
Modified the function to skip console settings if
skip_consoleis True:# Apply console settings console_updated = False console_params = mqtt_config.get('console', {}) if console_params and not skip_console: # Console settings application code... -
Updated
check_mqtt_settings()to passskip_console=True:return self.configure_mqtt_settings( ip=ip, name=name, mqtt_status=mqtt_status, is_new_device=False, set_friendly_name=False, enable_mqtt=False, with_retry=True, reboot=False, skip_console=True # Skip console settings here as they'll be applied separately )
This ensures that console settings are only applied once, directly in get_device_details().
2. Fix for Template Matching Failure
The template matching algorithm was not handling the response format correctly. The function expected a "Template" key in the response, but the actual response had a different structure.
To fix this issue:
-
Added logging of the actual response format for debugging:
self.logger.debug(f"{name}: Template response: {template_data}") -
Enhanced the template extraction logic to handle different response formats:
# Extract current template - handle different response formats current_template = "" # Try different possible response formats if "Template" in template_data: current_template = template_data.get("Template", "") elif isinstance(template_data, dict) and len(template_data) > 0: # If there's no "Template" key but we have a dict, try to get the first value # This handles cases where the response might be {"NAME":"...","GPIO":[...]} first_key = next(iter(template_data)) if isinstance(template_data[first_key], str) and "{" in template_data[first_key]: current_template = template_data[first_key] self.logger.debug(f"{name}: Found template in alternate format under key: {first_key}") # Handle the case where the template is returned as a dict with NAME, GPIO, FLAG, BASE keys elif all(key in template_data for key in ['NAME', 'GPIO', 'FLAG', 'BASE']): # Convert the dict to a JSON string to match the expected format import json current_template = json.dumps(template_data) self.logger.debug(f"{name}: Found template in dict format with NAME, GPIO, FLAG, BASE keys")
This allows the function to handle the specific response format with 'NAME', 'GPIO', 'FLAG', and 'BASE' keys, which is what the OfficeLight device returns.
Testing and Verification
The changes were tested with the OfficeLight device and both issues were resolved:
- Console settings are now only applied once
- Template matching is working correctly and updating the template as needed
The TasmotaDevices.json file confirms that the template was successfully updated, with "template_status": "Updated".
Conclusion
These changes optimize the device configuration process by:
- Eliminating duplicate application of console settings
- Improving the template matching algorithm to handle different response formats
This ensures that all configuration steps (MQTT, config_other, and console) are applied correctly and efficiently.