TasmotaManager/blank_template_value_handling.md
Mike Geppert 126cd39555 Major code improvements and bug fixes:
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
2025-08-08 19:04:33 -05:00

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:

  1. Adding a check to see if the template value is blank or empty (not template_value or template_value.strip() == "")
  2. If the value is blank or empty, logging a message and printing a user-friendly message
  3. Returning False to 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:

  1. Loads the configuration from network_configuration.json
  2. Finds a key in config_other that has a non-empty value
  3. Sets the value for this key to an empty string
  4. Creates a mock Status 0 response that returns this key as the device name
  5. Patches the requests.get method to return this mock response
  6. Calls the check_and_update_template method
  7. 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:

  1. When a key in config_other has a blank or empty value, the check_and_update_template method returns False
  2. The template check is skipped, and no attempt is made to set the template or device name
  3. 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:

  1. Skip the template check
  2. 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
    
  3. Return False from the check_and_update_template method

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.