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
157 lines
5.4 KiB
Python
Executable File
157 lines
5.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Test script for the is_device_excluded function.
|
|
|
|
This script tests the is_device_excluded function with various device names and hostnames
|
|
to ensure it correctly identifies devices that should be excluded based on exclude_patterns.
|
|
"""
|
|
|
|
import sys
|
|
import logging
|
|
from TasmotaManager import TasmotaDiscovery
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Configuration file path
|
|
CONFIG_FILE = "network_configuration.json"
|
|
|
|
def test_with_config_patterns():
|
|
"""Test is_device_excluded with patterns from the configuration."""
|
|
logger.info("Testing is_device_excluded with patterns from configuration")
|
|
|
|
# Create TasmotaDiscovery instance
|
|
manager = TasmotaDiscovery(debug=True)
|
|
manager.load_config(CONFIG_FILE)
|
|
|
|
# Get the patterns from the configuration for reference
|
|
patterns = []
|
|
network_filters = manager.config['unifi'].get('network_filter', {})
|
|
for network in network_filters.values():
|
|
patterns.extend(network.get('exclude_patterns', []))
|
|
|
|
logger.info(f"Patterns from configuration: {patterns}")
|
|
|
|
# Test cases that should be excluded
|
|
exclude_cases = [
|
|
("homeassistant", "homeassistant.local"),
|
|
("homeassistant123", ""),
|
|
("sonos", ""),
|
|
("mysonos", ""),
|
|
("sonosdevice", ""),
|
|
("", "sonos.local"),
|
|
("", "mysonos.local")
|
|
]
|
|
|
|
# Test cases that should not be excluded
|
|
no_exclude_cases = [
|
|
("tasmota_device", "tasmota.local"),
|
|
("esp-abcd", "esp.local"),
|
|
("kitchen_light", "kitchen.local"),
|
|
("living_room_switch", "living-room.local"),
|
|
("bedroom_lamp", "bedroom.local")
|
|
]
|
|
|
|
# Test cases that should be excluded
|
|
logger.info("Testing devices that should be excluded:")
|
|
for device_name, hostname in exclude_cases:
|
|
result = manager.is_device_excluded(device_name, hostname)
|
|
logger.info(f" {device_name} ({hostname}): {result}")
|
|
if not result:
|
|
logger.error(f" ERROR: {device_name} ({hostname}) should be excluded but isn't")
|
|
|
|
# Test cases that should not be excluded
|
|
logger.info("Testing devices that should not be excluded:")
|
|
for device_name, hostname in no_exclude_cases:
|
|
result = manager.is_device_excluded(device_name, hostname)
|
|
logger.info(f" {device_name} ({hostname}): {result}")
|
|
if result:
|
|
logger.error(f" ERROR: {device_name} ({hostname}) should not be excluded but is")
|
|
|
|
def test_with_custom_patterns():
|
|
"""Test is_device_excluded with custom patterns."""
|
|
logger.info("Testing is_device_excluded with custom patterns")
|
|
|
|
# Create TasmotaDiscovery instance
|
|
manager = TasmotaDiscovery(debug=True)
|
|
manager.load_config(CONFIG_FILE)
|
|
|
|
# Define custom patterns
|
|
custom_patterns = [
|
|
"^test-*",
|
|
"^custom_*",
|
|
"^.*special-device.*"
|
|
]
|
|
|
|
logger.info(f"Custom patterns: {custom_patterns}")
|
|
|
|
# Test cases that should be excluded
|
|
exclude_cases = [
|
|
("test-device", "test.local"),
|
|
("custom_light", "custom.local"),
|
|
("special-device", "special.local"),
|
|
("my-special-device", ""),
|
|
("", "special-device.local")
|
|
]
|
|
|
|
# Test cases that should not be excluded
|
|
no_exclude_cases = [
|
|
("mytest-device", "mytest.local"),
|
|
("mycustom_light", "mycustom.local"),
|
|
("device-special", "device-special.local")
|
|
]
|
|
|
|
# Test cases that should be excluded
|
|
logger.info("Testing devices that should be excluded:")
|
|
for device_name, hostname in exclude_cases:
|
|
result = manager.is_device_excluded(device_name, hostname, custom_patterns)
|
|
logger.info(f" {device_name} ({hostname}): {result}")
|
|
if not result:
|
|
logger.error(f" ERROR: {device_name} ({hostname}) should be excluded but isn't")
|
|
|
|
# Test cases that should not be excluded
|
|
logger.info("Testing devices that should not be excluded:")
|
|
for device_name, hostname in no_exclude_cases:
|
|
result = manager.is_device_excluded(device_name, hostname, custom_patterns)
|
|
logger.info(f" {device_name} ({hostname}): {result}")
|
|
if result:
|
|
logger.error(f" ERROR: {device_name} ({hostname}) should not be excluded but is")
|
|
|
|
def test_log_levels():
|
|
"""Test is_device_excluded with different log levels."""
|
|
logger.info("Testing is_device_excluded with different log levels")
|
|
|
|
# Create TasmotaDiscovery instance
|
|
manager = TasmotaDiscovery(debug=True)
|
|
manager.load_config(CONFIG_FILE)
|
|
|
|
# Define a simple pattern
|
|
patterns = ["^homeassistant*"]
|
|
|
|
# Test with different log levels
|
|
log_levels = ['debug', 'info', 'warning', 'error']
|
|
|
|
for level in log_levels:
|
|
logger.info(f"Testing with log_level='{level}'")
|
|
result = manager.is_device_excluded("homeassistant", "homeassistant.local", patterns, log_level=level)
|
|
logger.info(f" Result: {result}")
|
|
|
|
def main():
|
|
"""Run all tests."""
|
|
logger.info("Starting tests for is_device_excluded function")
|
|
|
|
# Run tests
|
|
test_with_config_patterns()
|
|
print("\n")
|
|
test_with_custom_patterns()
|
|
print("\n")
|
|
test_log_levels()
|
|
|
|
logger.info("All tests completed")
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |