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
135 lines
5.5 KiB
Markdown
135 lines
5.5 KiB
Markdown
# Unifi Hostname Bug Handling Summary
|
|
|
|
## Overview
|
|
|
|
This document answers the questions:
|
|
1. How many locations in the codebase handle the Unifi OS hostname bug?
|
|
2. Can these locations call `is_hostname_unknown` instead 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:
|
|
|
|
1. **`is_hostname_unknown` Function (Lines 260-362)**
|
|
- Already has full bug handling capability with the `from_unifi_os` and `ip` parameters
|
|
- Uses the `_match_pattern` helper function for consistent pattern matching
|
|
|
|
2. **`get_tasmota_devices` Method (Lines 480-537)**
|
|
- Partially uses `is_hostname_unknown` for initial pattern matching (lines 485-488)
|
|
- Has its own implementation of the bug handling logic (lines 497-537)
|
|
|
|
3. **`process_single_device` Method (Lines 1780-1841)**
|
|
- Doesn't use `is_hostname_unknown` at all
|
|
- Has its own implementation of both pattern matching and bug handling logic
|
|
|
|
4. **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:**
|
|
```python
|
|
# 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:**
|
|
```python
|
|
# 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:**
|
|
```python
|
|
# 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:**
|
|
```python
|
|
# 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:
|
|
|
|
1. **Code Reuse**: Eliminates duplicated logic for pattern matching and bug handling
|
|
2. **Maintainability**: Changes to the bug handling logic only need to be made in one place
|
|
3. **Consistency**: Ensures that pattern matching and bug handling are performed consistently throughout the codebase
|
|
4. **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. |