-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattery_notifier.py
executable file
·128 lines (100 loc) · 3.71 KB
/
battery_notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python3
# Version - 0.1
# Date - 31/Dec/2018
# This Program works with Battery Sensors and gets the Battery charge details.
# It also plays alarm to notify low or full battery.
# Sample file downloaded from the below link:
# http://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3
# https://www.zedge.net/ringtone/723978da-9659-3f1b-803b-3183eee3c8e2
# Version - 0.2
# Date - 1/Jan/2019
# Fixed minor bugs.
__Author__ = "Rajesh Kumar N"
__version__ = "0.2"
import json
from playsound import playsound, PlaysoundException
import psutil
import os
from time import sleep
class BatteryNotifier:
current_dir = os.path.dirname(os.path.abspath(__file__))
def __init__(self):
self.battery = psutil.sensors_battery()
self.low_battery_flag = True
self.__low_value = 20
self.full_charge_flag = True
self.__full_value = 99
self.alarm_json_file = os.path.join(self.current_dir, "alarm_file.json")
self.get_alarm_file_path()
def get_alarm_file_path(self):
with open(self.alarm_json_file) as alarm_file:
self.alarm_file = json.load(alarm_file)
def set_alarm_file_path(self, file_path):
if os.path.exists(file_path):
with open(self.alarm_json_file, 'w') as alarm_out_file:
json.dump(file_path, alarm_out_file)
with open(self.alarm_json_file) as alarm_file:
self.alarm_file = json.load(alarm_file)
def is_power_plugged(self):
try:
return self.battery.power_plugged
except AttributeError:
return None
def get_battery_percentage(self):
try:
return self.battery.percent
except AttributeError:
return None
def set_low_battery_flag(self):
if not self.low_battery_flag:
self.low_battery_flag = True
else:
self.low_battery_flag = False
def get_low_battery_flag(self):
return self.low_battery_flag
@property
def low_value(self):
return self.__low_value
@low_value.setter
def low_value(self, value):
self.__low_value = value
def low_battery_notification(self):
try:
if not self.is_power_plugged() and self.get_battery_percentage() <= self.low_value:
try:
playsound(self.alarm_file)
except PlaysoundException:
print("Exception occurred while playing %s" % self.alarm_file)
except TypeError:
pass
def set_full_charge_flag(self):
if not self.full_charge_flag:
self.full_charge_flag = True
else:
self.full_charge_flag = False
def get_full_charge_flag(self):
return self.full_charge_flag
@property
def full_value(self):
return self.__full_value
def full_charge_notification(self):
try:
if self.is_power_plugged() and self.get_battery_percentage() >= self.full_value:
try:
playsound(self.alarm_file)
except PlaysoundException:
print("Exception occurred while playing %s" % self.alarm_file)
except TypeError:
pass
def change_alarm_file(self, new_file_path):
if os.path.exists(new_file_path):
self.alarm_file = new_file_path
else:
raise FileNotFoundError
if __name__ == "__main__":
while True:
notifier_obj = BatteryNotifier()
print("Current Battery Percentage: %s" % notifier_obj.get_battery_percentage())
notifier_obj.low_battery_notification()
notifier_obj.full_charge_notification()
sleep(60)