1. Restructured configuration: Moved config_other and console to top level 2. Added common _match_pattern function for regex pattern matching 3. Implemented Unifi Hostname bug fix in is_hostname_unknown 4. Created common get_device_hostname function to eliminate code duplication 5. Added comprehensive test scripts for all new functionality 6. Added detailed documentation for all changes
3.3 KiB
Blank Template Value Handling
Issue Description
When a key in the config_other field of the network_configuration.json file has a blank or empty value, the system should not check or set the template or device name. Instead, it should print a message to the user that the device must be set manually in Configuration/Module to the string in the Key.
Changes Made
1. Modified the check_and_update_template Method
The check_and_update_template method in TasmotaManager.py was modified to check if a value is blank or empty before proceeding with template checks:
if device_name in config_other:
# Key matches device name, check if value is blank or empty
template_value = config_other[device_name]
if not template_value or template_value.strip() == "":
# Value is blank or empty, print message and skip template check
self.logger.info(f"{name}: Device name '{device_name}' matches key in config_other, but value is blank or empty")
print(f"\nDevice {name} at {ip} must be set manually in Configuration/Module to: {device_name}")
print(f"The config_other entry has a blank value for key: {device_name}")
return False
elif current_template != template_value:
# Template doesn't match, write value to template
# ... (existing code)
The changes include:
- Adding a check to see if the template value is blank or empty (
not template_value or template_value.strip() == "") - If the value is blank or empty, logging a message and printing a user-friendly message
- Returning
Falseto skip the rest of the template check
2. Created a Test Script
A test script test_blank_template_value.py was created to validate the changes. The script:
- Loads the configuration from
network_configuration.json - Finds a key in
config_otherthat has a non-empty value - Sets the value for this key to an empty string
- Creates a mock Status 0 response that returns this key as the device name
- Patches the
requests.getmethod to return this mock response - Calls the
check_and_update_templatemethod - Verifies that the method returns
False, indicating that the template check was skipped
Testing
The changes were tested using the test_blank_template_value.py script, which confirmed that:
- When a key in
config_otherhas a blank or empty value, thecheck_and_update_templatemethod returnsFalse - The template check is skipped, and no attempt is made to set the template or device name
- A message is printed to the user indicating that the device must be set manually in Configuration/Module
Example
Consider the following entry in network_configuration.json:
"Sonoff S31": ""
When a device with the name "Sonoff S31" is encountered, the system will:
- Skip the template check
- Print a message to the user:
Device Sonoff S31 at 192.168.8.123 must be set manually in Configuration/Module to: Sonoff S31 The config_other entry has a blank value for key: Sonoff S31 - Return
Falsefrom thecheck_and_update_templatemethod
Conclusion
These changes ensure that when a key in config_other has a blank or empty value, the system skips the template check and prints a message to the user, as required by the issue description. This provides a better user experience by clearly indicating what action the user needs to take.