From 6fffa0177b79c54d1d2ca584d46057908351c982 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Wed, 23 Nov 2022 17:55:55 +0100 Subject: [PATCH] =?UTF-8?q?[Newm]=20New=20method=20showing=20computer?= =?UTF-8?q?=E2=80=99s=20connectivity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/newm/config.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/.config/newm/config.py b/.config/newm/config.py index eafaf0a..9240041 100644 --- a/.config/newm/config.py +++ b/.config/newm/config.py @@ -279,6 +279,53 @@ def get_bluetooth_devices() -> str: return f" {counter}" +def get_wifi_name(iface) -> str: + bus = dbus.SystemBus() + service_name = "org.freedesktop.NetworkManager" + proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings") + + # Get the device object path based on interface name + proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager") + nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager") + devpath = nm.GetDeviceByIpIface(iface) + + # Get a proxy to the wifi device and get the active access point's object path + proxy = bus.get_object(service_name, devpath) + props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + active_ap_path = props.Get( + "org.freedesktop.NetworkManager.Device.Wireless", "ActiveAccessPoint" + ) + if active_ap_path == "/": + return "" + + # Get the active access point's SSID and BSSID + ap_proxy = bus.get_object(service_name, active_ap_path) + ap_props = dbus.Interface(ap_proxy, "org.freedesktop.DBus.Properties") + raw_ssid = ap_props.Get("org.freedesktop.NetworkManager.AccessPoint", "Ssid") + ssid = b"".join([bytes([v]) for v in raw_ssid]).decode("utf-8") + return ssid + + +def get_local_address(wifi_iface, eth_iface) -> str: + if_addrs = psutil.net_if_addrs() + wifi = if_addrs.get(wifi_iface) + eth = if_addrs.get(eth_iface) + wifi_addr = wifi[0].address if wifi else "" + eth_addr = eth[0].address if eth else "" + wifi_addr = wifi_addr if not ":" in wifi_addr else "" + eth_addr = eth_addr if not ":" in eth_addr else "" + return wifi_addr or eth_addr + + +def get_network() -> str: + wifi_interface = "wlp8s0" + eth_interface = "enp9s0f1" + network_name = get_wifi_name(wifi_interface) + network_name = f" {network_name}" if network_name else " eth" + addr = get_local_address(wifi_interface, eth_interface) + return f"{network_name} ({addr})" if addr else "睊 disconnected" + + def get_currently_playing(): bus = dbus.SessionBus() service_name = "org.mpris.MediaPlayer2.playerctld"