diff --git a/TasmotaManager.py b/TasmotaManager.py index aea07cc..a564774 100755 --- a/TasmotaManager.py +++ b/TasmotaManager.py @@ -382,15 +382,21 @@ class TasmotaDiscovery: self.logger.debug(f"Handling hostname '{hostname}' from Unifi OS (bug handling enabled)") # Get the device's self-reported hostname using the common function - device_reported_hostname, success = self.get_device_hostname(ip, hostname, timeout=5, log_level='debug') + try: + device_reported_hostname, success = self.get_device_hostname(ip, hostname, timeout=5, log_level='debug') + except Exception as e: + self.logger.debug(f"Failed to retrieve self-reported hostname for {hostname} at {ip}: {e}") + device_reported_hostname, success = "", False if success: # Check if the self-reported hostname also matches unknown patterns device_hostname_matches_unknown = False + # Use the base of the self-reported hostname up to the first '-' for bug detection + device_hostname_base = device_reported_hostname.split('-')[0].lower() for pattern in patterns: - if self._match_pattern(device_reported_hostname.lower(), pattern, match_entire_string=False): + if self._match_pattern(device_hostname_base, pattern, match_entire_string=False): device_hostname_matches_unknown = True - self.logger.debug(f"Device's self-reported hostname '{device_reported_hostname}' matches unknown pattern: {pattern}") + self.logger.debug(f"Device's self-reported hostname base '{device_hostname_base}' (from '{device_reported_hostname}') matches unknown pattern: {pattern}") break # If UniFi name matches unknown patterns but device's self-reported name doesn't, @@ -559,10 +565,12 @@ class TasmotaDiscovery: if success: # Check if the self-reported hostname also matches unknown patterns device_hostname_matches_unknown = False + # Use the base of the self-reported hostname up to the first '-' for bug detection + device_hostname_base = device_reported_hostname.split('-')[0].lower() for pattern in unknown_patterns: - if self._match_pattern(device_reported_hostname.lower(), pattern, match_entire_string=False): + if self._match_pattern(device_hostname_base, pattern, match_entire_string=False): device_hostname_matches_unknown = True - self.logger.debug(f"Device's self-reported hostname '{device_reported_hostname}' matches unknown pattern: {pattern}") + self.logger.debug(f"Device's self-reported hostname base '{device_hostname_base}' (from '{device_reported_hostname}') matches unknown pattern: {pattern}") break # If UniFi name matches unknown patterns but device's self-reported name doesn't, @@ -1974,10 +1982,12 @@ class TasmotaDiscovery: if success and device_reported_hostname: # Check if the self-reported hostname also matches unknown patterns device_hostname_matches_unknown = False + # Use the base of the self-reported hostname up to the first '-' for bug detection + device_hostname_base = device_reported_hostname.split('-')[0].lower() for pattern in unknown_patterns: - if self._match_pattern(device_reported_hostname.lower(), pattern, match_entire_string=False): + if self._match_pattern(device_hostname_base, pattern, match_entire_string=False): device_hostname_matches_unknown = True - self.logger.info(f"Device's self-reported hostname '{device_reported_hostname}' matches unknown pattern: {pattern}") + self.logger.info(f"Device's self-reported hostname base '{device_hostname_base}' (from '{device_reported_hostname}') matches unknown pattern: {pattern}") break # Only declare as unknown if both UniFi-reported and self-reported hostnames match unknown patterns