2020-07-18 08:23:28 +02:00
|
|
|
"""Displays bluetooth status. Left mouse click launches manager app `blueman-manager`,
|
2020-04-29 20:19:45 +02:00
|
|
|
right click toggles bluetooth. Needs dbus-send to toggle bluetooth state and
|
|
|
|
python-dbus to count the number of connections
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* bluetooth.manager : application to launch on click (blueman-manager)
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
contributed by `martindoublem <https://github.com/martindoublem>`_ - many thanks!
|
2020-04-29 20:19:45 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import dbus
|
|
|
|
import dbus.mainloop.glib
|
|
|
|
import logging
|
|
|
|
|
2020-04-29 20:23:10 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
2020-04-29 20:19:45 +02:00
|
|
|
|
2020-04-29 20:23:10 +02:00
|
|
|
import util.cli
|
2020-04-29 20:19:45 +02:00
|
|
|
|
2020-04-29 20:23:10 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.status))
|
2020-04-29 20:19:45 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.manager = self.parameter("manager", "blueman-manager")
|
|
|
|
self._status = "Off"
|
2020-04-29 20:19:45 +02:00
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
self._bus = dbus.SystemBus()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.manager)
|
|
|
|
core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self._toggle)
|
2020-04-29 20:19:45 +02:00
|
|
|
|
|
|
|
def status(self, widget):
|
|
|
|
"""Get status."""
|
2024-04-17 22:46:18 +02:00
|
|
|
return self._status if self._status.isdigit() and int(self._status) > 1 else ""
|
2020-04-29 20:19:45 +02:00
|
|
|
|
2020-04-29 20:23:10 +02:00
|
|
|
def update(self):
|
2020-04-29 20:19:45 +02:00
|
|
|
"""Update current state."""
|
2020-05-03 11:15:52 +02:00
|
|
|
state = len(
|
|
|
|
subprocess.run(["bluetoothctl", "list"], stdout=subprocess.PIPE).stdout
|
|
|
|
)
|
2020-04-29 20:19:45 +02:00
|
|
|
if state > 0:
|
|
|
|
connected_devices = self.get_connected_devices()
|
2023-11-10 16:50:16 +01:00
|
|
|
self._status = "{}".format(connected_devices)
|
2020-04-29 20:19:45 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
self._status = "Off"
|
|
|
|
adapters_cmd = "rfkill list | grep Bluetooth"
|
|
|
|
if not len(
|
|
|
|
subprocess.run(adapters_cmd, shell=True, stdout=subprocess.PIPE).stdout
|
|
|
|
):
|
|
|
|
self._status = "No Adapter Found"
|
2020-04-29 20:19:45 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
def _toggle(self, widget=None):
|
|
|
|
"""Toggle bluetooth state."""
|
2020-05-03 11:15:52 +02:00
|
|
|
logging.debug("bt: toggling bluetooth")
|
2023-11-10 16:50:16 +01:00
|
|
|
|
|
|
|
SetRfkillState = self._bus.get_object("org.blueman.Mechanism", "/org/blueman/mechanism").get_dbus_method("SetRfkillState", dbus_interface="org.blueman.Mechanism")
|
|
|
|
SetRfkillState(self._status == "Off")
|
2020-04-29 20:19:45 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
"""Get current state."""
|
|
|
|
state = []
|
|
|
|
|
2023-11-10 16:50:16 +01:00
|
|
|
if self._status in [ "No Adapter Found", "Off" ]:
|
2020-05-03 11:15:52 +02:00
|
|
|
state.append("critical")
|
2023-11-10 16:50:16 +01:00
|
|
|
elif self._status == "0":
|
2020-05-03 11:15:52 +02:00
|
|
|
state.append("warning")
|
2020-04-29 20:19:45 +02:00
|
|
|
else:
|
2023-11-10 16:50:16 +01:00
|
|
|
state.append("ON")
|
|
|
|
|
2020-04-29 20:19:45 +02:00
|
|
|
return state
|
|
|
|
|
|
|
|
def get_connected_devices(self):
|
|
|
|
devices = 0
|
|
|
|
objects = dbus.Interface(
|
2020-05-03 11:15:52 +02:00
|
|
|
self._bus.get_object("org.bluez", "/"), "org.freedesktop.DBus.ObjectManager"
|
2020-04-29 20:19:45 +02:00
|
|
|
).GetManagedObjects()
|
|
|
|
for path, interfaces in objects.items():
|
2020-05-03 11:15:52 +02:00
|
|
|
if "org.bluez.Device1" in interfaces:
|
2023-11-10 16:50:16 +01:00
|
|
|
if dbus.Interface(self._bus.get_object("org.bluez", path), "org.freedesktop.DBus.Properties", ).Get("org.bluez.Device1", "Connected"):
|
2020-04-29 20:19:45 +02:00
|
|
|
devices += 1
|
|
|
|
return devices
|
2020-04-29 20:23:10 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|