Function: battery-upower
battery-upower is a byte-compiled function defined in battery.el.gz.
Signature
(battery-upower)
Documentation
Get battery status from UPower D-Bus interface.
This function works only in systems that provide a UPower D-Bus service.
The following %-sequences are provided:
%c Current capacity (mWh)
%r Current rate of charge or discharge
%L AC line status (verbose)
%B Battery status (verbose)
%b Battery status: empty means high, - means low,
! means critical, and + means charging
%d Temperature (in degrees Celsius)
%p Battery load percentage
%s Remaining time (to charge or discharge) in seconds
%m Remaining time (to charge or discharge) in minutes
%h Remaining time (to charge or discharge) in hours
%t Remaining time (to charge or discharge) in the form h:min
Probably introduced at or before Emacs version 28.1.
Source Code
;; Defined in /usr/src/emacs/lisp/battery.el.gz
(defun battery-upower ()
"Get battery status from UPower D-Bus interface.
This function works only in systems that provide a UPower D-Bus
service.
The following %-sequences are provided:
%c Current capacity (mWh)
%r Current rate of charge or discharge
%L AC line status (verbose)
%B Battery status (verbose)
%b Battery status: empty means high, `-' means low,
`!' means critical, and `+' means charging
%d Temperature (in degrees Celsius)
%p Battery load percentage
%s Remaining time (to charge or discharge) in seconds
%m Remaining time (to charge or discharge) in minutes
%h Remaining time (to charge or discharge) in hours
%t Remaining time (to charge or discharge) in the form `h:min'"
(let ((count 0) props type line-status state load temperature
secs mins hrs total-energy total-rate total-tte total-ttf)
;; Merge information from all available or specified UPower
;; devices like other `battery-status-function's.
(dolist (device (battery--upower-devices))
(setq props (battery--upower-device-properties device))
(setq type (cdr (assoc "Type" props)))
(cond
((and (eq type 1) (not (eq line-status 'online)))
;; It's a line power device: `online' if currently providing
;; power, any other non-nil value if simply present.
(setq line-status (if (cdr (assoc "Online" props)) 'online t)))
((and (eq type 2) (cdr (assoc "IsPresent" props)))
;; It's a battery.
(setq count (1+ count))
(setq state (battery--upower-state props state))
(let ((energy (cdr (assoc "Energy" props)))
(rate (cdr (assoc "EnergyRate" props)))
(percent (cdr (assoc "Percentage" props)))
(temp (cdr (assoc "Temperature" props)))
(tte (cdr (assoc "TimeToEmpty" props)))
(ttf (cdr (assoc "TimeToFull" props))))
(when energy (setq total-energy (+ (or total-energy 0) energy)))
(when rate (setq total-rate (+ (or total-rate 0) rate)))
(when percent (setq load (+ (or load 0) percent)))
(when temp (setq temperature (+ (or temperature 0) temp)))
(when tte (setq total-tte (+ (or total-tte 0) tte)))
(when ttf (setq total-ttf (+ (or total-ttf 0) ttf)))))))
(when (> count 1)
;; Averages over multiple batteries.
(when load (setq load (/ load count)))
(when temperature (setq temperature (/ temperature count))))
(when (setq secs (if (eq line-status 'online) total-ttf total-tte))
(setq mins (/ secs 60))
(setq hrs (/ secs 3600)))
(list (cons ?c (if total-energy
(format "%.0f" (* total-energy 1000))
"N/A"))
(cons ?r (if total-rate (format "%.1f W" total-rate) "N/A"))
(cons ?L (cond ((eq line-status 'online) "on-line")
(line-status "off-line")
("N/A")))
(cons ?B (format "%s" (or state 'unknown)))
(cons ?b (cond ((eq state 'charging) "+")
((and load (< load battery-load-critical)) "!")
((and load (< load battery-load-low)) "-")
("")))
;; Zero usually means unknown.
(cons ?d (if (and temperature (/= temperature 0))
(format "%.0f" temperature)
"N/A"))
(cons ?p (if load (format "%.0f" load) "N/A"))
(cons ?s (if secs (number-to-string secs) "N/A"))
(cons ?m (if mins (number-to-string mins) "N/A"))
(cons ?h (if hrs (number-to-string hrs) "N/A"))
(cons ?t (if hrs (format "%d:%02d" hrs (% mins 60)) "N/A")))))