From 4ec39cacb0c3128930564edbd955ac5b5a298e66 Mon Sep 17 00:00:00 2001 From: Facetoe Date: Thu, 15 Jun 2017 08:48:52 +0700 Subject: [PATCH] Prevent battery module spamming notifications. (#581) * Prevent battery module spamming notifications. Raised in #578. * Fix pep8 issues. --- i3pystatus/battery.py | 31 ++++++++++++++++++++----------- i3pystatus/core/desktop.py | 28 ++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/i3pystatus/battery.py b/i3pystatus/battery.py index f1fd8ce..4f4917b 100644 --- a/i3pystatus/battery.py +++ b/i3pystatus/battery.py @@ -1,11 +1,11 @@ +import configparser import os import re -import configparser from i3pystatus import IntervalModule, formatp -from i3pystatus.core.util import lchop, TimeWrapper, make_bar -from i3pystatus.core.desktop import DesktopNotification from i3pystatus.core.command import run_through_shell +from i3pystatus.core.desktop import DesktopNotification +from i3pystatus.core.util import lchop, TimeWrapper, make_bar class UEventParser(configparser.ConfigParser): @@ -192,7 +192,8 @@ class BatteryChecker(IntervalModule): ("charging_color", "The charging color"), ("critical_color", "The critical color"), ("not_present_color", "The not present color."), - ("not_present_text", "The text to display when the battery is not present. Provides {battery_ident} as formatting option"), + ("not_present_text", + "The text to display when the battery is not present. Provides {battery_ident} as formatting option"), ("no_text_full", "Don't display text when battery is full - 100%"), ) @@ -225,6 +226,8 @@ class BatteryChecker(IntervalModule): path = None paths = [] + notification = None + def percentage(self, batteries, design=False): total_now = [battery.wh_remaining() for battery in batteries] total_full = [battery.wh_total() for battery in batteries] @@ -341,13 +344,19 @@ class BatteryChecker(IntervalModule): run_through_shell(self.critical_level_command, enable_shell=True) if self.alert and fdict["status"] == "DIS" and fdict["percentage"] <= self.alert_percentage: - DesktopNotification( - title=formatp(self.alert_format_title, **fdict), - body=formatp(self.alert_format_body, **fdict), - icon="battery-caution", - urgency=2, - timeout=self.alert_timeout, - ).display() + title, body = formatp(self.alert_format_title, **fdict), formatp(self.alert_format_body, **fdict) + if not self.notification: + self.notification = DesktopNotification( + title=title, + body=body, + icon="battery-caution", + urgency=2, + timeout=self.alert_timeout, + ) + self.notification.display() + else: + self.notification.update(title=title, + body=body) fdict["status"] = self.status[fdict["status"]] diff --git a/i3pystatus/core/desktop.py b/i3pystatus/core/desktop.py index 8e8eb74..cb38545 100644 --- a/i3pystatus/core/desktop.py +++ b/i3pystatus/core/desktop.py @@ -35,6 +35,18 @@ class BaseDesktopNotification: """ return False + def update(self, title=None, body=None, icon=None): + """ + Update this notification. + + :param title: Title of the notification + :param body: Body text of the notification, depending on the users system configuration HTML may be used, but is not recommended + :param icon: A XDG icon name, see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html + + :return boolean indicating success + """ + return False + class DesktopNotification(BaseDesktopNotification): pass @@ -42,6 +54,7 @@ class DesktopNotification(BaseDesktopNotification): try: import gi + gi.require_version('Notify', '0.7') from gi.repository import Notify except (ImportError, ValueError): @@ -61,16 +74,23 @@ else: Notify.Urgency.CRITICAL, ) + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.notification = Notify.Notification.new(self.title, self.body, self.icon) + def display(self): - notification = Notify.Notification.new(self.title, self.body, self.icon) if self.timeout: - notification.set_timeout(self.timeout) - notification.set_urgency(self.URGENCY_LUT[self.urgency]) + self.notification.set_timeout(self.timeout) + self.notification.set_urgency(self.URGENCY_LUT[self.urgency]) try: - return notification.show() + return self.notification.show() except Exception: self.logger.exception( 'Failed to display desktop notification (is a ' 'notification daemon running?)' ) return False + + def update(self, title=None, body=None, icon=None): + self.notification.update(title or self.title, body or self.body, icon or self.icon) + return self.notification.show()