Fix MAC normalization in device_labels lookup; add label support to arrival/departure alerts

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.
This commit is contained in:
Cobra
2026-04-14 14:52:41 -04:00
parent ba18b2fba8
commit be852dbcf3
+11 -11
View File
@@ -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: