From b6429ba0c250e57398340b940ab15d3b18dc3ca0 Mon Sep 17 00:00:00 2001 From: Cobra Date: Tue, 14 Apr 2026 23:34:34 -0400 Subject: [PATCH] Prefer auto-discovered hostname over device label in alerts Label in device_labels is for tracking/identification. When mDNS reveals the real device name, use it. Label serves as fallback when no hostname has been discovered yet. --- net_alerter/net_alerter.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/net_alerter/net_alerter.py b/net_alerter/net_alerter.py index dd9d23a..e22cfd1 100644 --- a/net_alerter/net_alerter.py +++ b/net_alerter/net_alerter.py @@ -919,23 +919,22 @@ def fmt_duration(secs: float) -> str: def format_arrival(hostname: str, ip: str, vendor: str, mac: str) -> str: - """Format arrival alert message. Uses friendly label if configured, else hostname/vendor.""" + """Format arrival alert message. Discovered hostname takes precedence; label is fallback.""" label = device_labels.get(_normalize_mac(mac)) - if label: - return f"[NET] ARRIVED: {label} ({ip})" - if hostname == ip: - return f"[NET] ARRIVED: {ip} [{vendor}] MAC:{mac}" - return f"[NET] ARRIVED: {hostname} ({ip}) [{vendor}] MAC:{mac}" + # Use discovered hostname if it's real (not just the IP); fall back to label + display = (hostname if hostname and hostname != ip else None) or label + if display: + return f"[NET] ARRIVED: {display} ({ip})" + return f"[NET] ARRIVED: {ip} [{vendor}] MAC:{mac}" def format_departure(hostname: str, ip: str, vendor: str, mac: str, duration: str) -> str: - """Format departure alert message. Uses friendly label if configured, else hostname/vendor.""" + """Format departure alert message. Discovered hostname takes precedence; label is fallback.""" label = device_labels.get(_normalize_mac(mac)) - if label: - return f"[NET] DEPARTED: {label} ({ip}) — present {duration}" - if hostname == ip: - return f"[NET] DEPARTED: {ip} [{vendor}] MAC:{mac} — present {duration}" - return f"[NET] DEPARTED: {hostname} ({ip}) [{vendor}] MAC:{mac} — present {duration}" + display = (hostname if hostname and hostname != ip else None) or label + if display: + return f"[NET] DEPARTED: {display} ({ip}) — present {duration}" + return f"[NET] DEPARTED: {ip} [{vendor}] MAC:{mac} — present {duration}" def _update_occupancy_state_unlocked() -> None: