Title: Check the laptop's battery status in Python
This is a less graphical post than many so it isn't very pretty, but it's useful if you're using a laptop that's not plugged in. The program gets battery information including the percent charge and the approximate amount of time you might be able to work without plugging in.
seconds_to_hms
The program defines the following function, which converts a number of seconds into hours, minutes, and seconds.
def seconds_to_hms(seconds):
'''Convert seconds into hours, minutes, and seconds.'''
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return hours, minutes, seconds
The first statement passes the number of seconds into the divmod function. That function divides its first argument by its second and returns the integer result and the remainder. In this case, it divides the number of seconds by 60 to get the number of minutes plus the number of seconds left over.
It then calls divmod(minutes, 60) to convert the number of minutes into hours with the number of minutes left over.
The function then returns the number of hours, minutes, and seconds.
Main Program
The following code shows how the main program uses the seconds_to_hms function.
import psutil
# Get battery information.
battery = psutil.sensors_battery()
if battery is None:
print('No battery information')
else:
print(f'Battery: {battery.percent}%')
print(f'Plugged In: {battery.power_plugged}')
if not battery.power_plugged:
seconds = battery.secsleft
if seconds == psutil.POWER_TIME_UNLIMITED:
print('Unlimited (charging)')
elif seconds == psutil.POWER_TIME_UNKNOWN:
print('Unknown')
else:
print(f'Time: {seconds} seconds')
hours, minutes, seconds = seconds_to_hms(seconds)
print(f'Time: {hours}:{minutes:02d}:{seconds:02d}')
The code calls psutil.sensors_battery to get battery information. If that function returns None, then something's wrong so we can't get battery information.
If that call doesn't return None, the program displays the battery's percent charge and an indication of whether the computer is plugged in.
If the computer is not plugged in, the code gets the approximate number of seconds remaining. If the computer is plugged in (it should not be if we're executing this piece of code), then the number of seconds will be the special value psutil.POWER_TIME_UNLIMITED and the program displays an appropriate message.
If cannot give an approximation, the number of seconds will have the special value psutil.POWER_TIME_UNKNOWN and again the program displays a message.
If sensors_battery returned something meaningful, the code calls seconds_to_hms to convert the estimated time remaining into hours, minutes, and seconds, and displays the results.
Conclusion
This example isn't super glamorous, but it can be useful if you're running unplugged and would like an estimate of how long you can remain that way. Of course your actual time remaining depends on what you're doing on your computer.
Download the example to experiment with it and to see additional details.
|