Skip to main content

macOS power monitoring tool with auto-updating TUI

Project description

Power Monitor

macOS power monitoring tool with auto-updating TUI for real-time battery and charging status.

Features

  • ๐Ÿ–ฅ๏ธ Auto-updating TUI with 3-panel layout (Textual framework)
  • โšก Real-time monitoring - Updates every 2 seconds automatically
  • ๐Ÿ“Š Live power metrics - Voltage, amperage, wattage, battery %
  • ๐Ÿ“ˆ Historical visualization - Power chart and statistics
  • ๐Ÿ”‹ Battery tracking - Capacity, charging status, charger info
  • ๐Ÿ’พ SQLite persistence - Automatic background data logging
  • ๐ŸŽฏ IOKit/SMC access - Direct macOS API integration via ctypes
  • ๐Ÿ”„ Auto-fallback - Graceful fallback to subprocess-based collection

Installation

Install with uv (recommended)

uv tool install powermonitor

Install with pipx

pipx install powermonitor

Usage

Launch the TUI

# Single command launches auto-updating TUI
powermonitor

The TUI displays:

โ”Œโ”€ powermonitor โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Real-Time Power                           โ”‚
โ”‚ โšก 45.2W / 67W    ๐Ÿ”‹ 72%    โšก Charging   โ”‚
โ”‚ 20.0V ร— 2.26A                             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Statistics (Last 100 readings)            โ”‚
โ”‚ Avg: 42.3W  Max: 55.1W  Min: 12.4W       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Power Chart (Last 60 readings)            โ”‚
โ”‚     55W โ”ค      โ•ญโ”€โ”€โ•ฎ                       โ”‚
โ”‚     45W โ”ค  โ•ญโ”€โ”€โ”€โ•ฏ  โ•ฐโ”€โ”€โ•ฎ                    โ”‚
โ”‚     35W โ”คโ”€โ”€โ•ฏ         โ•ฐโ”€                   โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€               โ”‚
โ”‚ [q] Quit  [r] Refresh  [c] Clear History โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Keyboard Controls

  • q or ESC - Quit application
  • r - Force refresh data
  • c - Clear history (with confirmation)

Development Mode

# Run with verbose collector info
uv run python -c "from powermonitor.collector import default_collector; collector = default_collector(verbose=True); print(collector.collect())"

# Test data collection
uv run python -c "from powermonitor.collector import default_collector; print(default_collector().collect())"

Requirements

  • macOS: 12.0+ (Monterey or later)
  • Python: 3.13+ (uses modern type hints)
  • Dependencies: textual, rich, textual-plotext (auto-installed by uv)

Architecture

TUI Layout (3 Panels)

  1. LiveDataPanel (green) - Real-time power data

    • Status: โšก Charging / ๐Ÿ”Œ AC Power / ๐Ÿ”‹ On Battery
    • Power: watts_actual / watts_negotiated
    • Battery: percentage, capacity (mAh)
    • Electrical: voltage, amperage
    • Charger info (if available)
  2. StatsPanel (cyan) - Historical statistics

    • Time range (earliest/latest)
    • Average/min/max power
    • Average battery percentage
    • Based on last 100 readings
  3. ChartWidget (blue) - Power over time

    • Line chart with 60 data points
    • Shows actual power and max negotiated power
    • Auto-scales based on data

Data Collection

powermonitor uses two collectors with automatic fallback:

  1. IOKitCollector (preferred) - Direct IOKit/SMC API via ctypes

    • Reads 7 SMC sensors: PPBR, PDTR, PSTR, PHPC, PDBR, TB0T, CHCC
    • Most accurate power readings (PDTR sensor)
    • Zero overhead (no subprocess)
  2. IORegCollector (fallback) - Subprocess-based

    • Executes ioreg -rw0 -c AppleSmartBattery -a
    • Parses plist output using Python's plistlib
    • Works on all Macs without special permissions

Database

All readings automatically saved to SQLite:

Default location: ~/.powermonitor/powermonitor.db

Custom location (via environment variable):

export POWERMONITOR_DB_PATH=/path/to/custom.db
powermonitor

Schema:

CREATE TABLE power_readings (
    id INTEGER PRIMARY KEY,
    timestamp TEXT,
    watts_actual REAL,
    watts_negotiated INTEGER,
    voltage REAL,
    amperage REAL,
    current_capacity INTEGER,
    max_capacity INTEGER,
    battery_percent INTEGER,
    is_charging INTEGER,
    external_connected INTEGER,
    charger_name TEXT,
    charger_manufacturer TEXT
);

Project Structure

powermonitor/
โ”œโ”€โ”€ pyproject.toml              # uv project config
โ”œโ”€โ”€ uv.lock                     # Dependency lock file
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ powermonitor/
โ”‚       โ”œโ”€โ”€ cli.py              # Entry point
โ”‚       โ”œโ”€โ”€ models.py           # PowerReading dataclass
โ”‚       โ”œโ”€โ”€ database.py         # SQLite operations
โ”‚       โ”œโ”€โ”€ collector/          # Data collection
โ”‚       โ”‚   โ”œโ”€โ”€ base.py         # PowerCollector protocol
โ”‚       โ”‚   โ”œโ”€โ”€ ioreg.py        # Subprocess collector
โ”‚       โ”‚   โ”œโ”€โ”€ factory.py      # Auto-fallback logic
โ”‚       โ”‚   โ””โ”€โ”€ iokit/          # IOKit/SMC FFI
โ”‚       โ”‚       โ”œโ”€โ”€ bindings.py # ctypes bindings
โ”‚       โ”‚       โ”œโ”€โ”€ structures.py # SMC data structures
โ”‚       โ”‚       โ”œโ”€โ”€ parser.py   # Binary parsing
โ”‚       โ”‚       โ”œโ”€โ”€ connection.py # SMCConnection
โ”‚       โ”‚       โ””โ”€โ”€ collector.py # IOKitCollector
โ”‚       โ””โ”€โ”€ tui/                # Textual TUI
โ”‚           โ”œโ”€โ”€ app.py          # PowerMonitorApp
โ”‚           โ””โ”€โ”€ widgets.py      # Custom widgets
โ””โ”€โ”€ tests/
    โ””โ”€โ”€ fixtures/               # Test data

Development

Code Quality

# Type checking
uv run ty check .

# Linting
uv run ruff check src/

# Auto-formatting
uv run ruff format src/

# Run all checks
uv run ty check . && uv run ruff check src/ && uv run ruff format src/

Testing

# Run tests (when available)
uv run pytest

# Manual testing
uv run powermonitor

Performance

  • Memory: <50MB RAM
  • CPU: <1% when idle
  • Update interval: 2 seconds (configurable)
  • Database: Indexed for fast queries

Migration Notes

  • To: Python TUI with unified auto-updating interface
  • Reason: Better rapid development, easier maintenance, similar performance for 2s intervals
  • Preserved: All data collection logic, database schema, SMC sensor access (via ctypes)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

powermonitor-0.0.3-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file powermonitor-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: powermonitor-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for powermonitor-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d9251d93b41c4a40828860d8adcb728df3f371739871cbeabe004c4992c64674
MD5 b6183af0b5643520c2842c697d030e4e
BLAKE2b-256 2f8abee9e8836188fbf587b68a1ea384ab39b272ae6e4f1d393c7f14c24a7d62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page