Skip to content

Commit

Permalink
Extract reusable battery watcher
Browse files Browse the repository at this point in the history
...from ConnectionNotifier
  • Loading branch information
cschramm committed Sep 3, 2023
1 parent 41780ab commit 229ed51
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
11 changes: 10 additions & 1 deletion blueman/bluez/Battery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from blueman.bluez.AnyBase import AnyBase

from blueman.bluez.Base import Base

_INTERFACE = "org.bluez.Battery1"


class Battery(Base):
_interface_name = "org.bluez.Battery1"
_interface_name = _INTERFACE


class AnyBattery(AnyBase):
def __init__(self) -> None:
super().__init__(_INTERFACE)
29 changes: 29 additions & 0 deletions blueman/main/BatteryWatcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import weakref
from typing import Callable

from blueman.bluez.Battery import Battery, AnyBattery
from blueman.bluez.Manager import Manager


class BatteryWatcher:
def __init__(self, callback: Callable[[str, int], None]) -> None:
super().__init__()
manager = Manager()
weakref.finalize(
self,
manager.disconnect_signal,
manager.connect_signal(
"battery-created",
lambda _manager, obj_path: callback(obj_path, Battery(obj_path=obj_path)["Percentage"])
)
)

any_battery = AnyBattery()
weakref.finalize(
self,
any_battery.disconnect_signal,
any_battery.connect_signal(
"property-changed",
lambda _any_battery, key, value, path: callback(path, value) if key == "Percentage" else None
)
)
3 changes: 2 additions & 1 deletion blueman/main/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ blueman_PYTHON = \
Services.py \
Tray.py \
DBusProxies.py \
NetworkManager.py
NetworkManager.py \
BatteryWatcher.py

if HAVE_PULSEAUDIO
blueman_PYTHON += PulseAudioUtils.py
Expand Down
37 changes: 9 additions & 28 deletions blueman/plugins/applet/ConnectionNotifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,39 @@
from typing import Any, Dict, Union

from blueman.bluez.Device import Device
from blueman.bluez.Battery import Battery
from blueman.bluez.Manager import Manager
from blueman.gui.Notification import Notification, _NotificationBubble, _NotificationDialog
from blueman.main.BatteryWatcher import BatteryWatcher
from blueman.plugins.AppletPlugin import AppletPlugin
from gi.repository import GLib


class ConnectionNotifier(AppletPlugin):
__author__ = "cschramm"
__icon__ = "bluetooth-symbolic"
__description__ = _("Shows desktop notifications when devices get connected or disconnected.")

_sig = None
_notifications: Dict[str, Union[_NotificationBubble, _NotificationDialog]] = {}

def on_load(self) -> None:
self._manager = Manager()
self._sig = self._manager.connect_signal("battery-created", self._on_battery_created)
self._battery_watcher = BatteryWatcher(self._on_battery_update)

def on_unload(self) -> None:
if self._sig is not None:
self._manager.disconnect_signal(self._sig)
del self._battery_watcher

def on_device_property_changed(self, path: str, key: str, value: Any) -> None:
device = Device(obj_path=path)
battery = Battery(obj_path=path)

if key == "Connected":
device = Device(obj_path=path)
if value:
self._notifications[path] = notification = Notification(
device.display_name,
_('Connected'),
icon_name=device["Icon"]
)
notification.show()

sig = battery.connect_signal("property-changed", self._on_battery_property_changed)

def disconnect_signal() -> bool:
battery.disconnect_signal(sig)
return False
GLib.timeout_add_seconds(5, disconnect_signal)
else:
Notification(device.display_name, _('Disconnected'), icon_name=device["Icon"]).show()

def _on_battery_created(self, _manager: Manager, obj_path: str) -> None:
battery = Battery(obj_path=obj_path)
self._on_battery_property_changed(battery, "Percentage", battery["Percentage"], obj_path)

def _on_battery_property_changed(self, _battery: Battery, key: str, value: Any, path: str) -> None:
if key == "Percentage":
notification = self._notifications[path]
if notification:
notification.set_message(f"{_('Connected')} {value}%")
notification.set_notification_icon("battery")
def _on_battery_update(self, path: str, value: int) -> None:
notification = self._notifications[path]
if notification:
notification.set_message(f"{_('Connected')} {value}%")
notification.set_notification_icon("battery")

0 comments on commit 229ed51

Please sign in to comment.