From ffee10eb161160be91562149562f1819393147b0 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 4 Jun 2016 11:56:04 +0200 Subject: [PATCH] Port to python --- battery_health.2s.py | 82 +++++++++++++++++++++++++++++++++++++++++++ battery_health.60s.sh | 72 ------------------------------------- 2 files changed, 82 insertions(+), 72 deletions(-) create mode 100755 battery_health.2s.py delete mode 100755 battery_health.60s.sh diff --git a/battery_health.2s.py b/battery_health.2s.py new file mode 100755 index 0000000..a267ff9 --- /dev/null +++ b/battery_health.2s.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python2 +#- * -coding: utf - 8 - * - +# +# Battery Health +# v1.1 +# Andros Fenollosa +# tanrax +# Shows power percentaje and notice when you load + +import os, math, subprocess, pickle, tempfile + +# Get info battery +SAVE_LOCATION = os.path.join(tempfile.gettempdir(), 'batteryHealth2.pkl') +LIM_LOWER = 45 +LIM_UPPER = 85 +p = subprocess.Popen(["ioreg", "-rc", "AppleSmartBattery"], stdout = subprocess.PIPE) +output = p.communicate()[0] +b_max = 0 +o_cur = 0 +is_charging = '' +dateSave = False +alertMin = False +alertMax = False + +try: + dateFile = open(SAVE_LOCATION) + dateSave = pickle.load(dateFile) + alertMin = dateSave['alertMin'] + alertMax = dateSave['alertMax'] +except: + pass + +# Get variables +for l in output.splitlines(): + if 'MaxCapacity' in l: + o_max = l + if 'CurrentCapacity' in l: + o_cur = l + if 'IsCharging' in l: + is_charging = l + +b_max = float(o_max.rpartition('=')[-1].strip()) +b_cur = float(o_cur.rpartition('=')[-1].strip()) +is_charging = str(is_charging.rpartition('=')[-1].strip()) +if is_charging == 'Yes': + is_charging = True +else: + is_charging = False + +# Calculate porcent battery +charge = b_cur / b_max +charge_porcent = int(math.ceil(100 * charge)) + +# Logic +## Alert Min +if LIM_LOWER >= charge_porcent and not alertMin: + alertMin = True + os.system('osascript -e \'display notification "Battery too low" with title "Battery health" sound name "Blow"\'') + +## Alert Max +if LIM_UPPER <= charge_porcent and not alertMax: + alertMax = True + os.system('osascript -e \'display notification "Very charged battery" with title "Battery health" sound name "Blow"\'') + +## Reset alerts +if LIM_UPPER > charge_porcent > LIM_LOWER: + alertMin = False + alertMax = False + +# Save +dateTemp = {'alertMax': alertMax, 'alertMin': alertMin} +dateSave = open(SAVE_LOCATION, 'w+') +pickle.dump(dateTemp, dateSave) + +# Print +final = '' +if is_charging: + final += '⚡️' +elif alertMin or alertMax: + final += '🔴' +final = '{text}{charge}%'.format(text=final, charge=charge_porcent) +print(final) diff --git a/battery_health.60s.sh b/battery_health.60s.sh deleted file mode 100755 index 6864fbf..0000000 --- a/battery_health.60s.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash -# -# Battery Health -# v1.0 -# Andros Fenollosa -# tanrax -# https://programadorwebvalencia.com/wp-content/uploads/2016/04/Screen-Shot-2016-04-05-at-12.47.25.jpg -# Shows power percentaje and notice when you load - -# Variables -BATTERY=$(ioreg -l | awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.0f%";max=c["\"MaxCapacity\""];print(max>0?100*c["\"CurrentCapacity\""]/max:"?")}') -TYPE=$(pmset -g cap | sed -ne 's/^Capabilities for \(.*\) Power:$/\1/p') -POR_LOW=45 -POR_HIGH=85 -LOW=False -HIGH=False -SAVE_LOCATION=$TMPDIR/batteryHealth -BAD='🔴' -AC='⚡️' - -# Get data -if [ -f "$SAVE_LOCATION" ]; then - DATA=$(cat "$SAVE_LOCATION") -else - DATA="$LOW|$HIGH" -fi - -LOW=$(echo "$DATA" | cut -d "|" -f1) -HIGH=$(echo "$DATA" | cut -d "|" -f2) - -# Functions -function changeStatus { - osascript -e "display notification \"$2\" with title \"$1\" sound name \"$3\"" &> /dev/null -} - -function batteryLow { - LOW=True - changeStatus "Battery Low" "$BATTERY"% "Blow" -} - -function batteryNormal { - HIGH=False - LOW=False -} - -function batteryHigh { - HIGH=True - changeStatus "Battery high" "$BATTERY"% "Blow" -} - -# Logic -if [ "$BATTERY" -le $POR_LOW ] && [ $LOW = False ]; then - batteryLow -elif [ "$BATTERY" -ge $POR_HIGH ] && [ $HIGH = False ]; then - batteryHigh -elif [ "$BATTERY" -le $POR_HIGH ] && [ "$BATTERY" -ge $POR_LOW ]; then - batteryNormal -fi - -# Save data -echo "$LOW|$HIGH" > "$SAVE_LOCATION"; - -# View battery -if [ "$TYPE" = "AC" ]; then - echo $AC "$BATTERY"% -else - if [ $HIGH = True ] || [ $LOW = True ]; then - echo $BAD "$BATTERY"% - else - echo "$BATTERY"% - fi -fi