- Created modular Python files (main, utils, discovery, configuration, console_settings, unknown_devices, reporting, unifi_client) - Moved documentation files to docs/ - Moved data files to data/ - Removed old monolithic TasmotaManager.py and TasmotaManager_fixed.py - Updated .gitignore and pyproject.toml - All functionality preserved, command-line interface unchanged Version: 2.0.0
3.5 KiB
Console Settings Optimization
Issue Description
The issue was related to how console settings were being applied in the TasmotaManager code. The original implementation used a skip_console parameter in the configure_mqtt_settings function to prevent console settings from being applied twice:
- Once in
configure_mqtt_settings(but skipped withskip_console=True) - Again directly in
get_device_details
The question was raised: "I question why the skip_console was needed. Seems like the console settings before thecheck_mqtt_settings should be the one deleted?"
Analysis
After reviewing the code, I found that:
-
In
get_device_details, it callscheck_mqtt_settingswhich callsconfigure_mqtt_settingswithskip_console=True. This preventsconfigure_mqtt_settingsfrom applying console settings. -
Later in
get_device_details, console settings are applied directly with a large block of code that duplicates functionality already present inconfigure_mqtt_settings. -
In
configure_unknown_device, it callsconfigure_mqtt_settingswithout specifyingskip_console, so it uses the default value ofFalse. This means console settings are applied when configuring unknown devices.
This approach added unnecessary complexity with the skip_console parameter and made the code less intuitive (why skip in one place and apply in another?).
Changes Made
I implemented the following changes to optimize the code:
-
Removed the
skip_consoleparameter from theconfigure_mqtt_settingsfunction signature: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): -
Updated the condition in
configure_mqtt_settingsto always apply console settings:# Apply console settings console_updated = False console_params = mqtt_config.get('console', {}) if console_params: self.logger.info(f"{name}: Setting console parameters from configuration") -
Updated
check_mqtt_settingsto callconfigure_mqtt_settingswithout theskip_consoleparameter: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 ) -
Removed the console settings application code in
get_device_detailsand replaced it with:# Console settings are now applied in configure_mqtt_settings console_updated = mqtt_updated
Benefits
These changes provide several benefits:
-
Simplified Code: Removed the
skip_consoleparameter and eliminated duplicate code. -
More Intuitive Design: Console settings are now applied in the same place as MQTT settings, making the code more logical and easier to understand.
-
Reduced Maintenance: With only one place to update console settings logic, future changes will be easier to implement.
-
Consistent Behavior: Console settings are now applied consistently for both unknown and known devices.
Testing
The changes were tested to ensure that console settings are still applied correctly. The console_updated flag is now set based on the result of the MQTT settings update, which includes console settings application.
This approach maintains all the functionality of the original code while making it more maintainable and easier to understand.