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
5.5 KiB
Unifi Hostname Bug Handling Summary
Overview
This document answers the questions:
- How many locations in the codebase handle the Unifi OS hostname bug?
- Can these locations call
is_hostname_unknowninstead of duplicating logic?
Locations That Handle the Unifi OS Hostname Bug
Out of the four locations that look for device self-reported hostnames, three handle the Unifi OS hostname bug:
-
is_hostname_unknownFunction (Lines 260-362)- Already has full bug handling capability with the
from_unifi_osandipparameters - Uses the
_match_patternhelper function for consistent pattern matching
- Already has full bug handling capability with the
-
get_tasmota_devicesMethod (Lines 480-537)- Partially uses
is_hostname_unknownfor initial pattern matching (lines 485-488) - Has its own implementation of the bug handling logic (lines 497-537)
- Partially uses
-
process_single_deviceMethod (Lines 1780-1841)- Doesn't use
is_hostname_unknownat all - Has its own implementation of both pattern matching and bug handling logic
- Doesn't use
-
Device Details Collection (Lines 2068-2092)
- Just retrieves hostname information
- Doesn't handle the Unifi OS hostname bug
- Doesn't need to be refactored
Can They Call is_hostname_unknown?
Yes, both the get_tasmota_devices and process_single_device methods can be refactored to call is_hostname_unknown instead of duplicating logic:
1. get_tasmota_devices Method
This method already uses is_hostname_unknown for initial pattern matching but could be refactored to use it for bug handling too:
Current implementation:
# Check if device name or hostname matches unknown patterns
unifi_name_matches_unknown = (
self.is_hostname_unknown(device_name, unknown_patterns) or
self.is_hostname_unknown(device_hostname, unknown_patterns)
)
if unifi_name_matches_unknown:
self.logger.debug(f"Device {device_name} matches unknown device pattern")
# If the name matches unknown patterns, check the device's self-reported hostname
# ... (custom bug handling logic) ...
Proposed refactoring:
# Check if device name or hostname matches unknown patterns
unifi_name_matches_unknown = (
self.is_hostname_unknown(device_name, unknown_patterns) or
self.is_hostname_unknown(device_hostname, unknown_patterns)
)
if unifi_name_matches_unknown:
self.logger.debug(f"Device {device_name} matches unknown device pattern")
# If the name matches unknown patterns, check for the Unifi OS hostname bug
if unifi_name_matches_unknown and device_ip:
# Use is_hostname_unknown with from_unifi_os=True to handle the bug
not_actually_unknown = not self.is_hostname_unknown(
device_name,
unknown_patterns,
from_unifi_os=True,
ip=device_ip
)
if not_actually_unknown:
unifi_hostname_bug_detected = True
self.logger.info(f"UniFi OS hostname bug detected for {device_name}")
2. process_single_device Method
This method doesn't use is_hostname_unknown at all and could be refactored to use it for both initial pattern matching and bug handling:
Current implementation:
# Check if device name or hostname matches unknown patterns
unifi_name_matches_unknown = False
for pattern in unknown_patterns:
# ... (custom pattern matching logic) ...
if (re.match(regex_pattern, device_name.lower()) or
re.match(regex_pattern, device_hostname.lower())):
unifi_name_matches_unknown = True
self.logger.info(f"Device {device_name} matches unknown device pattern: {pattern}")
break
# If the name matches unknown patterns, check the device's self-reported hostname
# ... (custom bug handling logic) ...
Proposed refactoring:
# Check if device name or hostname matches unknown patterns
unifi_name_matches_unknown = (
self.is_hostname_unknown(device_name, unknown_patterns) or
self.is_hostname_unknown(device_hostname, unknown_patterns)
)
if unifi_name_matches_unknown:
self.logger.info(f"Device {device_name} matches unknown device pattern")
# If the name matches unknown patterns, check for the Unifi OS hostname bug
if unifi_name_matches_unknown:
# Use is_hostname_unknown with from_unifi_os=True to handle the bug
is_unknown = self.is_hostname_unknown(
device_name,
unknown_patterns,
from_unifi_os=True,
ip=device_ip
)
if not is_unknown:
self.logger.info("Device NOT declared as unknown: self-reported hostname doesn't match unknown patterns (possible UniFi OS bug)")
unifi_hostname_bug_detected = True
else:
self.logger.info("Device declared as unknown: both UniFi-reported and self-reported hostnames match unknown patterns")
else:
is_unknown = unifi_name_matches_unknown
Benefits of Refactoring
Refactoring these methods to use is_hostname_unknown would provide several benefits:
- Code Reuse: Eliminates duplicated logic for pattern matching and bug handling
- Maintainability: Changes to the bug handling logic only need to be made in one place
- Consistency: Ensures that pattern matching and bug handling are performed consistently throughout the codebase
- Readability: Makes the code more concise and easier to understand
Conclusion
Three locations in the codebase handle the Unifi OS hostname bug, and two of them (get_tasmota_devices and process_single_device) can be refactored to call is_hostname_unknown instead of duplicating logic. This refactoring would improve code reuse, maintainability, consistency, and readability.