From 1214534726e442206e741a64ca8617720a128d52 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Wed, 23 Nov 2022 17:55:05 +0100 Subject: [PATCH] [Newm] Add method for displaying connected bluetooth devices --- .config/newm/config.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.config/newm/config.py b/.config/newm/config.py index 51afbdd..c53e465 100644 --- a/.config/newm/config.py +++ b/.config/newm/config.py @@ -6,6 +6,7 @@ import time import logging import psutil import subprocess +import dbus import docker docker_client = docker.from_env() @@ -249,6 +250,35 @@ def right_text() -> str: return " | ".join([unread_emails(), cpu_usage(), mem_usage(), battery_status()]) +def get_bluetooth_devices() -> str: + import xml.etree.ElementTree as ET + + bus = dbus.SystemBus() + service_name = "org.bluez" + + # Verify if bluetooth is turned on + proxy = bus.get_object(service_name, "/org/bluez/hci0") + props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + if not props.Get("org.bluez.Adapter1", "Powered"): + return "" + + # Grab all known devices + bt_intro_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Introspectable") + bt_intro = str(bt_intro_iface.Introspect()) + root_node = ET.fromstring(bt_intro) + known_devices = [n.get("name") for n in root_node.findall("node")] + + # Check if all devices are connected + counter = 0 + for device in known_devices: + object_path = f"/org/bluez/hci0/{device}" + proxy = bus.get_object(service_name, object_path) + props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + if props.Get("org.bluez.Device1", "Connected"): + counter = counter + 1 + return f" {counter}" + + def display_docker() -> str: containers = docker_client.containers.list(sparse=True) return f" {len(containers)}"