bluetooth2: using dbus api, shortening output, some refactoring

This commit is contained in:
Ludwig Behm 2023-11-10 16:50:16 +01:00
parent c3d4fce74c
commit 53de1b524a
Signed by: l.behm
GPG key ID: D344835D63B89384

View file

@ -8,7 +8,6 @@ Parameters:
contributed by `martindoublem <https://github.com/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