diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-02-02 23:15:08 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-02-02 23:15:08 +0100 |
commit | 0a02bdcf7b3fcfe9ab328148187ed9f4b9f93a00 (patch) | |
tree | 12d6fbf9edd60202e861b6ef59ad7fb5e63012fa | |
parent | 1f30d13ced7faec37946b0eb7f8127b86fed4116 (diff) | |
download | psutil-0a02bdcf7b3fcfe9ab328148187ed9f4b9f93a00.tar.gz |
#966: sensors_battery().power_plugged may lie if AC0/online is not there; fallback on using /BAT0/status instead
-rw-r--r-- | HISTORY.rst | 3 | ||||
-rw-r--r-- | docs/index.rst | 3 | ||||
-rw-r--r-- | psutil/_pslinux.py | 23 |
3 files changed, 24 insertions, 5 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index a8868266..b24f6444 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -16,7 +16,8 @@ decoding character on Python 3. - 965_: [Linux] disk_io_counters() may miscalculate sector size and report the wrong number of bytes read and written. -- 966_: [Linux] sensors_battery() fails with no such file error. +- 966_: [Linux] sensors_battery() may fail with "no such file error". +- 966_: [Linux] sensors_battery().power_plugged may lie. 5.1.0 diff --git a/docs/index.rst b/docs/index.rst index 2c4c87ed..2e362272 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -665,7 +665,8 @@ Sensors :data:`psutil.POWER_TIME_UNLIMITED <psutil.POWER_TIME_UNLIMITED>`. If it can't be determined it is set to :data:`psutil.POWER_TIME_UNKNOWN <psutil.POWER_TIME_UNKNOWN>`. - - **power_plugged**: ``True`` if the AC power cable is connected. + - **power_plugged**: ``True`` if the AC power cable is connected, ``False`` + if not or ``None`` if it can't be determined. Example:: diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 2b724fd3..1d5005eb 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1146,9 +1146,26 @@ def sensors_battery(): if percent == null: return None - # Secs left. - power_plugged = cat(os.path.join(POWER_SUPPLY_PATH, "AC0/online"), - fallback=b"0") == b"1" + # Is AC power cable plugged in? + if os.path.exists(os.path.join(POWER_SUPPLY_PATH, "AC0/online")): + power_plugged = cat( + os.path.join(POWER_SUPPLY_PATH, "AC0/online"), + fallback=b"0") == b"1" + elif os.path.exists(root + "/status"): + status = cat(root + "/status", fallback="").lower() + if status == "discharging": + power_plugged = False + elif status in ("charging", "full"): + power_plugged = True + else: + power_plugged = None + else: + power_plugged = None + + # Seconds left. + # Note to self: we may also calculate the charging ETA as per: + # https://github.com/thialfihar/dotfiles/blob/ + # 013937745fd9050c30146290e8f963d65c0179e6/bin/battery.py#L55 if power_plugged: secsleft = _common.POWER_TIME_UNLIMITED else: |