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