battery-warn: no need to check whole battery info.

This commit is contained in:
2026-01-01 01:19:20 +08:00
parent f27a5ff07b
commit 630f196a5e

View File

@@ -46,9 +46,9 @@ class UPowerManager():
interface = self.UPOWER_NAME
return dbus.Interface(upower_proxy, interface)
def __battery(self, battery):
battery_proxy = self.bus.get_object(self.UPOWER_NAME, battery)
return dbus.Interface(battery_proxy, self.DBUS_PROPERTIES)
def __device(self, devpath):
devproxy = self.bus.get_object(self.UPOWER_NAME, devpath)
return dbus.Interface(devproxy, self.DBUS_PROPERTIES)
def detect_devices(self):
return self.__upower().EnumerateDevices()
@@ -59,44 +59,40 @@ class UPowerManager():
def get_critical_action(self):
return self.__upower().GetCriticalAction()
def get_device_percentage(self, battery):
return self.__battery(battery).Get(
self.UPOWER_NAME + ".Device", "Percentage")
def get_full_device_information(self, battery):
def get_property(prop_name):
return self.__battery(battery).Get(
self.UPOWER_NAME + ".Device", prop_name)
def get_device_info(self, dev, property):
return self.__device(dev).Get(
self.UPOWER_NAME + ".Device", property)
def get_full_device_info(self, dev):
return {
'HasHistory': get_property("HasHistory"),
'HasStatistics': get_property("HasStatistics"),
'IsPresent': get_property("IsPresent"),
'IsRechargeable': get_property("IsRechargeable"),
'Online': get_property("Online"),
'PowerSupply': get_property("PowerSupply"),
'Capacity': get_property("Capacity"),
'Energy': get_property("Energy"),
'EnergyEmpty': get_property("EnergyEmpty"),
'EnergyFull': get_property("EnergyFull"),
'EnergyFullDesign': get_property("EnergyFullDesign"),
'EnergyRate': get_property("EnergyRate"),
'Luminosity': get_property("Luminosity"),
'Percentage': get_property("Percentage"),
'Temperature': get_property("Temperature"),
'Voltage': get_property("Voltage"),
'TimeToEmpty': get_property("TimeToEmpty"),
'TimeToFull': get_property("TimeToFull"),
'IconName': get_property("IconName"),
'Model': get_property("Model"),
'NativePath': get_property("NativePath"),
'Serial': get_property("Serial"),
'Vendor': get_property("Vendor"),
'State': get_property("State"),
'Technology': get_property("Technology"),
'Type': get_property("Type"),
'WarningLevel': get_property("WarningLevel"),
'UpdateTime': get_property("UpdateTime")
'HasHistory': self.get_device_info(dev, "HasHistory"),
'HasStatistics': self.get_device_info(dev, "HasStatistics"),
'IsPresent': self.get_device_info(dev, "IsPresent"),
'IsRechargeable': self.get_device_info(dev, "IsRechargeable"),
'Online': self.get_device_info(dev, "Online"),
'PowerSupply': self.get_device_info(dev, "PowerSupply"),
'Capacity': self.get_device_info(dev, "Capacity"),
'Energy': self.get_device_info(dev, "Energy"),
'EnergyEmpty': self.get_device_info(dev, "EnergyEmpty"),
'EnergyFull': self.get_device_info(dev, "EnergyFull"),
'EnergyFullDesign': self.get_device_info(dev, "EnergyFullDesign"),
'EnergyRate': self.get_device_info(dev, "EnergyRate"),
'Luminosity': self.get_device_info(dev, "Luminosity"),
'Percentage': self.get_device_info(dev, "Percentage"),
'Temperature': self.get_device_info(dev, "Temperature"),
'Voltage': self.get_device_info(dev, "Voltage"),
'TimeToEmpty': self.get_device_info(dev, "TimeToEmpty"),
'TimeToFull': self.get_device_info(dev, "TimeToFull"),
'IconName': self.get_device_info(dev, "IconName"),
'Model': self.get_device_info(dev, "Model"),
'NativePath': self.get_device_info(dev, "NativePath"),
'Serial': self.get_device_info(dev, "Serial"),
'Vendor': self.get_device_info(dev, "Vendor"),
'State': self.get_device_info(dev, "State"),
'Technology': self.get_device_info(dev, "Technology"),
'Type': self.get_device_info(dev, "Type"),
'WarningLevel': self.get_device_info(dev, "WarningLevel"),
'UpdateTime': self.get_device_info(dev, "UpdateTime")
}
def is_lid_present(self):
@@ -130,15 +126,9 @@ class UPowerManager():
).GetTotal()
def is_loading(self, battery):
state = int(self.__battery(battery).Get(
self.UPOWER_NAME + ".Device", "State"))
return state == 1
return int(self.get_device_info(battery, "State")) == 1
def get_state(self, battery):
state = int(self.__battery(battery).Get(
self.UPOWER_NAME + ".Device", "State"))
return {
0: "Unknown",
1: "Loading",
@@ -147,13 +137,18 @@ class UPowerManager():
4: "Fully charged",
5: "Pending charge",
6: "Pending discharge"
}.get(state, "Unknown")
}.get(int(self.get_device_info(battery, "State")), "Unknown")
def is_supplying_battery(self, battery):
return (
int(self.get_device_info(battery, "Type")) == 2
and bool(self.get_device_info(battery, "PowerSupply"))
)
def push_notification(title, message, timeout=10000):
BUS_NAME = "org.freedesktop.Notifications"
BUS_NAME = INTERFACE = "org.freedesktop.Notifications"
OBJECT_PATH = "/org/freedesktop/Notifications"
INTERFACE = BUS_NAME
notify = dbus.Interface(
dbus.SessionBus().get_object(BUS_NAME, OBJECT_PATH),
@@ -176,17 +171,16 @@ if __name__ == "__main__":
if not upowr.on_battery():
exit(0)
devPaths = upowr.detect_devices()
low_power_detected = False
for devPath in devPaths:
info = upowr.get_full_device_information(devPath)
if info['Type'] != 2: # 2 means battery
for device in upowr.detect_devices():
if not upowr.is_supplying_battery(device):
continue
# seems waybar visual is 1% lower than actual.
if info['Percentage'] < 21:
if upowr.get_device_info(device, "Percentage") < 21:
bat_id = upowr.get_device_info(device, "NativePath")
push_notification(
"Power Hint",
f"{info['NativePath']} is running out. Recharge soon!"
f"{bat_id} is running out. Recharge soon!"
)
low_power_detected = True
if low_power_detected: