From 53de1b524a9e1182fd23593cdc44eb337f0ea5b3 Mon Sep 17 00:00:00 2001 From: Ludwig Behm Date: Fri, 10 Nov 2023 16:50:16 +0100 Subject: [PATCH] bluetooth2: using dbus api, shortening output, some refactoring --- .../modules/contrib/bluetooth2.py | 33 +++++-------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/bumblebee_status/modules/contrib/bluetooth2.py b/bumblebee_status/modules/contrib/bluetooth2.py index 22eae88..76f1da5 100644 --- a/bumblebee_status/modules/contrib/bluetooth2.py +++ b/bumblebee_status/modules/contrib/bluetooth2.py @@ -8,7 +8,6 @@ Parameters: contributed by `martindoublem `_ - many thanks! """ - import os import re import subprocess @@ -22,7 +21,6 @@ import core.input import util.cli - class Module(core.module.Module): def __init__(self, config, theme): super().__init__(config, theme, core.widget.Widget(self.status)) @@ -46,7 +44,7 @@ class Module(core.module.Module): ) if state > 0: connected_devices = self.get_connected_devices() - self._status = "On - {}".format(connected_devices) + self._status = "{}".format(connected_devices) else: self._status = "Off" adapters_cmd = "rfkill list | grep Bluetooth" @@ -58,31 +56,22 @@ class Module(core.module.Module): def _toggle(self, widget=None): """Toggle bluetooth state.""" - if "On" in self._status: - state = "false" - else: - state = "true" - - cmd = ( - "dbus-send --system --print-reply --dest=org.blueman.Mechanism /org/blueman/mechanism org.blueman.Mechanism.SetRfkillState boolean:%s" - % state - ) - logging.debug("bt: toggling bluetooth") - util.cli.execute(cmd, ignore_errors=True) + + SetRfkillState = self._bus.get_object("org.blueman.Mechanism", "/org/blueman/mechanism").get_dbus_method("SetRfkillState", dbus_interface="org.blueman.Mechanism") + SetRfkillState(self._status == "Off") def state(self, widget): """Get current state.""" state = [] - if self._status == "No Adapter Found": + if self._status in [ "No Adapter Found", "Off" ]: state.append("critical") - elif self._status == "On - 0": + elif self._status == "0": state.append("warning") - elif "On" in self._status and not (self._status == "On - 0"): - state.append("ON") else: - state.append("critical") + state.append("ON") + return state def get_connected_devices(self): @@ -92,12 +81,8 @@ class Module(core.module.Module): ).GetManagedObjects() for path, interfaces in objects.items(): if "org.bluez.Device1" in interfaces: - if dbus.Interface( - self._bus.get_object("org.bluez", path), - "org.freedesktop.DBus.Properties", - ).Get("org.bluez.Device1", "Connected"): + if dbus.Interface(self._bus.get_object("org.bluez", path), "org.freedesktop.DBus.Properties", ).Get("org.bluez.Device1", "Connected"): devices += 1 return devices - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4