How to read battery voltage/percentage from command line/Python?

I use an awk script (whitespace added for readability):

#!/bin/sh
awk -F'=' '
  {a[$1]=$2}
  END{
    printf "%3d%% %s (%s)\n",
      a["POWER_SUPPLY_CAPACITY"], a["POWER_SUPPLY_STATUS"], a["POWER_SUPPLY_HEALTH"]
  }'  '/sys/devices/platform/ff140000.i2c/i2c-5/5-0034/axp20x-battery-power-supply/power_supply/axp20x-battery/uevent'

You can get about as much information as you want out of that interface, and they’re all $KEY=$VALUE, so that little script just builds a little table out.

For my system, it’s /sys/devices/platform/ff140000.i2c/i2c-5/5-0034/axp20x-battery-power-supply/power_supply/axp20x-battery/uevent but the filename will vary between CM4, A06, R01, etc. You can find the correct file by looking around like this:

$ find /sys/devices/platform -name uevent | grep axp20x-battery