2021-08-29 11:58:28 +02:00
|
|
|
__version__ = '1.0.0'
|
2021-08-18 22:00:16 +02:00
|
|
|
|
2021-08-29 11:58:28 +02:00
|
|
|
# Dependencies
|
|
|
|
import os
|
|
|
|
from tempfile import gettempdir
|
2021-08-18 22:00:16 +02:00
|
|
|
from notifypy import Notify
|
|
|
|
import psutil
|
|
|
|
|
2021-08-29 11:58:28 +02:00
|
|
|
# Variables
|
|
|
|
LIMIT_ABOVE_BATTERY = 85
|
|
|
|
MESSAGE_LIMIT_ABOVE_BATTERY = "Disconnect your charger"
|
|
|
|
LIMIT_BELOW_BATTERY = 15
|
|
|
|
MESSAGE_LIMIT_BELOW_BATTERY = "Connect your charger"
|
|
|
|
FILE_NAME_TEMP = "alert_battery_to_maintain_health"
|
|
|
|
PATH_FILE_NAME_TEMP = os.path.join(gettempdir(), FILE_NAME_TEMP)
|
2021-09-02 13:43:58 +02:00
|
|
|
NOTIFICATION_PATH_ICON = os.path.abspath(os.path.join(__file__, "..", ".." ,"icons/battery-status.png"))
|
2021-08-29 12:13:40 +02:00
|
|
|
NOTIFICATION = Notify(
|
2021-09-02 13:43:58 +02:00
|
|
|
default_notification_title="Alert battery",
|
|
|
|
default_application_name="Alert battery",
|
|
|
|
default_notification_icon=NOTIFICATION_PATH_ICON,
|
2021-08-29 12:13:40 +02:00
|
|
|
)
|
2021-08-29 11:58:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_sensor_battery():
|
|
|
|
"""Get object data sensor battery"""
|
|
|
|
return psutil.sensors_battery()
|
|
|
|
|
|
|
|
|
|
|
|
def send_notification(message, title="Battery"):
|
|
|
|
"""Send native notification"""
|
2021-08-29 12:13:40 +02:00
|
|
|
NOTIFICATION.title = title
|
|
|
|
NOTIFICATION.message = message
|
|
|
|
NOTIFICATION.send()
|
2021-08-29 11:58:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_file_block():
|
|
|
|
"""Make temp file not repeat notification"""
|
|
|
|
open(PATH_FILE_NAME_TEMP, 'w').close()
|
|
|
|
|
|
|
|
|
|
|
|
def get_battery_percent():
|
|
|
|
"""Gets the percentage of battery charge"""
|
|
|
|
return int(get_sensor_battery().percent)
|
|
|
|
|
|
|
|
|
|
|
|
def is_plugged():
|
|
|
|
"""It tells you if it is charging or not."""
|
|
|
|
return get_sensor_battery().power_plugged
|
|
|
|
|
|
|
|
|
2021-08-18 22:00:16 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2021-08-29 11:58:28 +02:00
|
|
|
# Is battery
|
|
|
|
if get_sensor_battery() :
|
|
|
|
|
|
|
|
# It has not been previously warned
|
|
|
|
if not os.path.exists(PATH_FILE_NAME_TEMP):
|
|
|
|
|
|
|
|
# Below
|
|
|
|
if LIMIT_BELOW_BATTERY > get_battery_percent() and not is_plugged():
|
|
|
|
send_notification(MESSAGE_LIMIT_BELOW_BATTERY)
|
|
|
|
create_file_block()
|
2021-08-18 22:00:16 +02:00
|
|
|
|
2021-08-29 11:58:28 +02:00
|
|
|
# Above
|
|
|
|
if LIMIT_ABOVE_BATTERY < get_battery_percent() and is_plugged():
|
|
|
|
send_notification(MESSAGE_LIMIT_ABOVE_BATTERY)
|
|
|
|
create_file_block()
|
2021-08-18 22:00:16 +02:00
|
|
|
|
2021-08-29 11:58:28 +02:00
|
|
|
# Unlock to warned
|
|
|
|
elif LIMIT_BELOW_BATTERY < get_battery_percent() < LIMIT_ABOVE_BATTERY:
|
2021-08-31 08:36:14 +02:00
|
|
|
os.remove(PATH_FILE_NAME_TEMP)
|