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.
Status
Fully Migrated to Python: Complete TUI implementation with auto-updating display, IOKit/SMC FFI, and SQLite history tracking.
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
qorESC- Quit applicationr- Force refresh datac- 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)
-
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)
-
StatsPanel (cyan) - Historical statistics
- Time range (earliest/latest)
- Average/min/max power
- Average battery percentage
- Based on last 100 readings
-
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:
-
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)
-
IORegCollector (fallback) - Subprocess-based
- Executes
ioreg -rw0 -c AppleSmartBattery -a - Parses plist output using Python's plistlib
- Works on all Macs without special permissions
- Executes
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file powermonitor-0.0.2-py3-none-any.whl.
File metadata
- Download URL: powermonitor-0.0.2-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed10b60fc2e2f6e7b271839322a127b9a35de8ec18c18c58e3c480435de61803
|
|
| MD5 |
d0ec37cbf2975feacf0ef4efb2ecbca4
|
|
| BLAKE2b-256 |
6c6ce3b9ed5cc39768d9873784d662ca4120ae6b65cc4d73b56452cd4d5a4bd1
|