Prevent battery module spamming notifications. (#581)

* Prevent battery module spamming notifications.

Raised in #578.

* Fix pep8 issues.
This commit is contained in:
Facetoe 2017-06-15 08:48:52 +07:00 committed by GitHub
parent 4c0bba59ab
commit 4ec39cacb0
2 changed files with 44 additions and 15 deletions

View File

@ -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"]]

View File

@ -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()