Prevent battery module spamming notifications. (#581)
* Prevent battery module spamming notifications. Raised in #578. * Fix pep8 issues.
This commit is contained in:
parent
4c0bba59ab
commit
4ec39cacb0
@ -1,11 +1,11 @@
|
|||||||
|
import configparser
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import configparser
|
|
||||||
|
|
||||||
from i3pystatus import IntervalModule, formatp
|
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.command import run_through_shell
|
||||||
|
from i3pystatus.core.desktop import DesktopNotification
|
||||||
|
from i3pystatus.core.util import lchop, TimeWrapper, make_bar
|
||||||
|
|
||||||
|
|
||||||
class UEventParser(configparser.ConfigParser):
|
class UEventParser(configparser.ConfigParser):
|
||||||
@ -192,7 +192,8 @@ class BatteryChecker(IntervalModule):
|
|||||||
("charging_color", "The charging color"),
|
("charging_color", "The charging color"),
|
||||||
("critical_color", "The critical color"),
|
("critical_color", "The critical color"),
|
||||||
("not_present_color", "The not present 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%"),
|
("no_text_full", "Don't display text when battery is full - 100%"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -225,6 +226,8 @@ class BatteryChecker(IntervalModule):
|
|||||||
path = None
|
path = None
|
||||||
paths = []
|
paths = []
|
||||||
|
|
||||||
|
notification = None
|
||||||
|
|
||||||
def percentage(self, batteries, design=False):
|
def percentage(self, batteries, design=False):
|
||||||
total_now = [battery.wh_remaining() for battery in batteries]
|
total_now = [battery.wh_remaining() for battery in batteries]
|
||||||
total_full = [battery.wh_total() 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)
|
run_through_shell(self.critical_level_command, enable_shell=True)
|
||||||
|
|
||||||
if self.alert and fdict["status"] == "DIS" and fdict["percentage"] <= self.alert_percentage:
|
if self.alert and fdict["status"] == "DIS" and fdict["percentage"] <= self.alert_percentage:
|
||||||
DesktopNotification(
|
title, body = formatp(self.alert_format_title, **fdict), formatp(self.alert_format_body, **fdict)
|
||||||
title=formatp(self.alert_format_title, **fdict),
|
if not self.notification:
|
||||||
body=formatp(self.alert_format_body, **fdict),
|
self.notification = DesktopNotification(
|
||||||
icon="battery-caution",
|
title=title,
|
||||||
urgency=2,
|
body=body,
|
||||||
timeout=self.alert_timeout,
|
icon="battery-caution",
|
||||||
).display()
|
urgency=2,
|
||||||
|
timeout=self.alert_timeout,
|
||||||
|
)
|
||||||
|
self.notification.display()
|
||||||
|
else:
|
||||||
|
self.notification.update(title=title,
|
||||||
|
body=body)
|
||||||
|
|
||||||
fdict["status"] = self.status[fdict["status"]]
|
fdict["status"] = self.status[fdict["status"]]
|
||||||
|
|
||||||
|
@ -35,6 +35,18 @@ class BaseDesktopNotification:
|
|||||||
"""
|
"""
|
||||||
return False
|
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):
|
class DesktopNotification(BaseDesktopNotification):
|
||||||
pass
|
pass
|
||||||
@ -42,6 +54,7 @@ class DesktopNotification(BaseDesktopNotification):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import gi
|
import gi
|
||||||
|
|
||||||
gi.require_version('Notify', '0.7')
|
gi.require_version('Notify', '0.7')
|
||||||
from gi.repository import Notify
|
from gi.repository import Notify
|
||||||
except (ImportError, ValueError):
|
except (ImportError, ValueError):
|
||||||
@ -61,16 +74,23 @@ else:
|
|||||||
Notify.Urgency.CRITICAL,
|
Notify.Urgency.CRITICAL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self.notification = Notify.Notification.new(self.title, self.body, self.icon)
|
||||||
|
|
||||||
def display(self):
|
def display(self):
|
||||||
notification = Notify.Notification.new(self.title, self.body, self.icon)
|
|
||||||
if self.timeout:
|
if self.timeout:
|
||||||
notification.set_timeout(self.timeout)
|
self.notification.set_timeout(self.timeout)
|
||||||
notification.set_urgency(self.URGENCY_LUT[self.urgency])
|
self.notification.set_urgency(self.URGENCY_LUT[self.urgency])
|
||||||
try:
|
try:
|
||||||
return notification.show()
|
return self.notification.show()
|
||||||
except Exception:
|
except Exception:
|
||||||
self.logger.exception(
|
self.logger.exception(
|
||||||
'Failed to display desktop notification (is a '
|
'Failed to display desktop notification (is a '
|
||||||
'notification daemon running?)'
|
'notification daemon running?)'
|
||||||
)
|
)
|
||||||
return False
|
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user