From 4b66ba96fe7b16907ac1cf603ed78d6034d113b8 Mon Sep 17 00:00:00 2001 From: Cobra Date: Tue, 14 Apr 2026 14:52:41 -0400 Subject: [PATCH] Fix MAC normalization in device_labels lookup; add label support to arrival/departure alerts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit device_labels keys are normalized (no colons, uppercase) but known_devices keys are colon-lowercase. device_labels.get(mac) always returned None. Also extend label display to arrival and departure alerts — if a MAC has a configured label, show that instead of vendor/hostname. --- net_alerter/net_alerter.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/net_alerter/net_alerter.py b/net_alerter/net_alerter.py index 12c8cc2..10e2fd0 100644 --- a/net_alerter/net_alerter.py +++ b/net_alerter/net_alerter.py @@ -875,23 +875,23 @@ def fmt_duration(secs: float) -> str: def format_arrival(hostname: str, ip: str, vendor: str, mac: str) -> str: - """Format arrival alert message. Deduplicate IP if hostname equals IP.""" + """Format arrival alert message. Uses friendly label if configured, else hostname/vendor.""" + label = device_labels.get(_normalize_mac(mac)) + if label: + return f"[NET] ARRIVED: {label} ({ip})" if hostname == ip: - # Hostname resolution failed, don't repeat IP return f"[NET] ARRIVED: {ip} [{vendor}] MAC:{mac}" - else: - # Hostname resolved successfully, show both - return f"[NET] ARRIVED: {hostname} ({ip}) [{vendor}] MAC:{mac}" + return f"[NET] ARRIVED: {hostname} ({ip}) [{vendor}] MAC:{mac}" def format_departure(hostname: str, ip: str, vendor: str, mac: str, duration: str) -> str: - """Format departure alert message. Deduplicate IP if hostname equals IP.""" + """Format departure alert message. Uses friendly label if configured, else hostname/vendor.""" + label = device_labels.get(_normalize_mac(mac)) + if label: + return f"[NET] DEPARTED: {label} ({ip}) — present {duration}" if hostname == ip: - # Hostname resolution failed, don't repeat IP return f"[NET] DEPARTED: {ip} [{vendor}] MAC:{mac} — present {duration}" - else: - # Hostname resolved successfully, show both - return f"[NET] DEPARTED: {hostname} ({ip}) [{vendor}] MAC:{mac} — present {duration}" + return f"[NET] DEPARTED: {hostname} ({ip}) [{vendor}] MAC:{mac} — present {duration}" def _update_occupancy_state_unlocked() -> None: @@ -916,7 +916,7 @@ def _update_occupancy_state_unlocked() -> None: present = [] for mac, dev in known_devices.items(): if not dev.get('departing', False) and dev['ip'] not in infrastructure_ips: - label = device_labels.get(mac) or dev.get('vendor') or mac[-8:] + label = device_labels.get(_normalize_mac(mac)) or dev.get('vendor') or mac[-8:] present.append(label) msg = f"[NET] Location: OCCUPIED ({', '.join(present)})" else: