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: