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:
@@ -875,22 +875,22 @@ def fmt_duration(secs: float) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def format_arrival(hostname: str, ip: str, vendor: str, mac: str) -> 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:
|
if hostname == ip:
|
||||||
# Hostname resolution failed, don't repeat IP
|
|
||||||
return f"[NET] ARRIVED: {ip} [{vendor}] MAC:{mac}"
|
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:
|
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:
|
if hostname == ip:
|
||||||
# Hostname resolution failed, don't repeat IP
|
|
||||||
return f"[NET] DEPARTED: {ip} [{vendor}] MAC:{mac} — present {duration}"
|
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}"
|
||||||
|
|
||||||
|
|
||||||
@@ -916,7 +916,7 @@ def _update_occupancy_state_unlocked() -> None:
|
|||||||
present = []
|
present = []
|
||||||
for mac, dev in known_devices.items():
|
for mac, dev in known_devices.items():
|
||||||
if not dev.get('departing', False) and dev['ip'] not in infrastructure_ips:
|
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)
|
present.append(label)
|
||||||
msg = f"[NET] Location: OCCUPIED ({', '.join(present)})"
|
msg = f"[NET] Location: OCCUPIED ({', '.join(present)})"
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user