TasmotaManager/docs/summaries/unifi_hostname_bug_explanation.md
2025-10-28 00:21:08 +00:00

6.3 KiB

Explanation of the Unifi Hostname Bug TODO Comment

The Issue

The TODO comment on line 312 in TasmotaManager.py states:

# TODO: Implement Unifi Hostname bug handling

This comment is in the is_hostname_unknown function, specifically in a conditional block that checks if the from_unifi_os parameter is True.

What is the Unifi Hostname Bug?

The Unifi Hostname bug is a known issue with UniFi OS where it doesn't keep track of updated hostnames. When a device's hostname is updated and the connection reset, UniFi may not pick up the new name. This can cause problems when trying to identify devices based on their hostnames.

The bug is detected when:

  1. The UniFi-reported name matches an unknown_device_pattern (suggesting it's a Tasmota device)
  2. The device's self-reported hostname does NOT match any unknown_device_pattern (suggesting it's actually a properly named device)

This mismatch indicates that UniFi is reporting an outdated or incorrect hostname.

Current Implementation

Currently, the code:

  1. Detects the bug in the get_tasmota_devices function by:

    • Checking if a device's name or hostname from UniFi matches unknown device patterns
    • If it does, checking the device's self-reported hostname by making a request to the device
    • Comparing the self-reported hostname against the same unknown device patterns
    • If the UniFi-reported name matches unknown patterns but the self-reported hostname doesn't, it sets unifi_hostname_bug_detected = True
  2. Flags affected devices by including the unifi_hostname_bug_detected flag in the device information.

  3. Has a parameter from_unifi_os in the is_hostname_unknown function that's intended to handle the bug, but the actual handling logic hasn't been implemented yet (hence the TODO).

Why the TODO Comment is There

The TODO comment exists because:

  1. The developers recognized the need to handle the Unifi Hostname bug in the is_hostname_unknown function.
  2. They added the from_unifi_os parameter to support this future implementation.
  3. They added the conditional block and TODO comment as a placeholder for the actual implementation.
  4. The bug detection logic is already implemented in the get_tasmota_devices function, but the handling logic in is_hostname_unknown hasn't been implemented yet.

Why It Hasn't Been Implemented Yet

Based on the code and documentation, the handling hasn't been implemented yet likely because:

  1. The bug is already detected and flagged, which might be sufficient for current needs.
  2. The is_hostname_unknown function with the from_unifi_os parameter doesn't appear to be called with from_unifi_os=True anywhere in the main code yet, suggesting this feature isn't being used in production.
  3. The implementation might require additional logic or testing that hasn't been prioritized.

To implement the Unifi Hostname bug handling in the is_hostname_unknown function, the following approach could be used (pseudocode based on the existing implementation):

# This is pseudocode to illustrate the concept, not actual implementation
# In the real implementation, 'requests' would be imported at the top of the file
def is_hostname_unknown(self, hostname, patterns=None, from_unifi_os=False, ip=None):
    # ... existing code ...
    
    # Handle Unifi Hostname bug if hostname is from Unifi OS
    if from_unifi_os:
        # Check the device's self-reported hostname
        if ip:
            try:
                # Get the device's self-reported hostname
                url = f"http://{ip}/cm?cmnd=Status%205"
                # In the real implementation, 'requests' would be imported
                response = requests.get(url, timeout=5)
                
                if response.status_code == 200:
                    status_data = response.json()
                    device_reported_hostname = status_data.get('StatusNET', {}).get('Hostname', '')
                    
                    if device_reported_hostname:
                        self.logger.debug(f"Device self-reported hostname: {device_reported_hostname}")
                        
                        # Check if the self-reported hostname matches unknown patterns
                        for pattern in patterns:
                            if self._match_pattern(device_reported_hostname.lower(), pattern, match_entire_string=False):
                                self.logger.debug(f"Device's self-reported hostname '{device_reported_hostname}' matches unknown pattern: {pattern}")
                                return True
                        
                        # If we get here, the self-reported hostname doesn't match any unknown patterns
                        self.logger.info(f"UniFi OS hostname bug detected: hostname '{hostname}' matches unknown patterns but self-reported hostname '{device_reported_hostname}' doesn't")
                        return False
            except Exception as e:
                self.logger.debug(f"Error checking device's self-reported hostname: {str(e)}")
        
        self.logger.debug(f"Handling hostname '{hostname}' from Unifi OS (bug handling enabled)")
    
    # ... continue with existing code ...

This implementation:

  1. Checks if an IP address is provided (required to query the device)
  2. Makes a request to the device to get its self-reported hostname
  3. Checks if the self-reported hostname matches any unknown patterns
  4. Returns True if it does (it's an unknown device)
  5. Returns False if it doesn't (it's not an unknown device, despite what UniFi reports)
  6. Logs appropriate messages for debugging

To use this implementation, the code that calls is_hostname_unknown would need to pass from_unifi_os=True when the hostname is from UniFi OS and might be affected by the bug.

Conclusion

The TODO comment on line 312 is a placeholder for implementing the Unifi Hostname bug handling in the is_hostname_unknown function. While the bug is already detected and flagged in the get_tasmota_devices function, the actual handling logic in is_hostname_unknown hasn't been implemented yet. The recommended implementation would check the device's self-reported hostname and use that to determine if it's truly an unknown device, rather than relying solely on the hostname reported by UniFi OS.