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:
- The UniFi-reported name matches an unknown_device_pattern (suggesting it's a Tasmota device)
- 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:
-
Detects the bug in the
get_tasmota_devicesfunction 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
-
Flags affected devices by including the
unifi_hostname_bug_detectedflag in the device information. -
Has a parameter
from_unifi_osin theis_hostname_unknownfunction 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:
- The developers recognized the need to handle the Unifi Hostname bug in the
is_hostname_unknownfunction. - They added the
from_unifi_osparameter to support this future implementation. - They added the conditional block and TODO comment as a placeholder for the actual implementation.
- The bug detection logic is already implemented in the
get_tasmota_devicesfunction, but the handling logic inis_hostname_unknownhasn'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:
- The bug is already detected and flagged, which might be sufficient for current needs.
- The
is_hostname_unknownfunction with thefrom_unifi_osparameter doesn't appear to be called withfrom_unifi_os=Trueanywhere in the main code yet, suggesting this feature isn't being used in production. - The implementation might require additional logic or testing that hasn't been prioritized.
Recommended Implementation
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:
- Checks if an IP address is provided (required to query the device)
- Makes a request to the device to get its self-reported hostname
- Checks if the self-reported hostname matches any unknown patterns
- Returns
Trueif it does (it's an unknown device) - Returns
Falseif it doesn't (it's not an unknown device, despite what UniFi reports) - 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.