Skip to main content

thuhome-alert

Monitor water and electricity balance on Tsinghua University's myhome portal (myhome.tsinghua.edu.cn) and send email alerts when balances fall below user-defined thresholds.

Python port of the R thuhomeAlert package. The two share the same account.csv schema — existing R users can pip install thuhome-alert and keep their current config without migration.

Features

  • Login to Tsinghua myhome portal
  • Fetch water and electricity balances
  • Support monitoring water-only or electricity-only (for single-account users)
  • Customizable threshold alerts
  • Local history storage (append-only CSV)
  • Schedule daily checks (Linux/macOS crontab, Windows Task Scheduler) via a single unified schedule command that auto-detects the OS
  • CLI: thuhome-alert setup | run | schedule | migrate
  • Two runtime dependencies only — requests and beautifulsoup4. SMTP, cron, paths, dates all via Python stdlib.

Installation

pip install thuhome-alert

Or with pipx for an isolated install:

pipx install thuhome-alert

Requires Python 3.12+.

Quick Start

1. Configure Account

thuhome-alert setup \
  --user your_username \
  --passwd your_password \
  --sender sender@qq.com \
  --token your_smtp_token \
  --recipient recipient@email.com \
  --water 20 \
  --electr 20 \
  --no-test-email

Or from Python:

import thuhome_alert

# Monitor both water and electricity
thuhome_alert.setup(
    user_name="your_username",
    user_pswd="your_password",
    email_sender="sender@qq.com",
    sender_token="your_smtp_token",
    email_recipient="recipient@email.com",
    water_threshold=20,
    electr_threshold=20,
    test_email=False,
)

# Monitor electricity only (set water_threshold = None)
thuhome_alert.setup(
    user_name="your_username",
    user_pswd="your_password",
    email_sender="sender@qq.com",
    sender_token="your_smtp_token",
    email_recipient="recipient@email.com",
    water_threshold=None,
    electr_threshold=20,
    test_email=False,
)

# Monitor water only (set electr_threshold = None)
thuhome_alert.setup(
    user_name="your_username",
    user_pswd="your_password",
    email_sender="sender@qq.com",
    sender_token="your_smtp_token",
    email_recipient="recipient@email.com",
    water_threshold=20,
    electr_threshold=None,
    test_email=False,
)

Config is saved to $XDG_CONFIG_HOME/thuhomeAlert/account.csv (default ~/.config/thuhomeAlert/account.csv, XDG-compliant). Balance history is stored at $XDG_DATA_HOME/thuhomeAlert/stat.dorm.csv (default ~/.local/share/thuhomeAlert/stat.dorm.csv).

2. Run Monitor

# Check balances and send alerts if needed
thuhome-alert run

# Check only (no recording, no alerts)
thuhome-alert run --no-record --no-alert

Or from Python:

import thuhome_alert

# Check balances and send alerts if needed
thuhome_alert.run_monitor()

# Check only (no recording, no alerts)
thuhome_alert.run_monitor(record=False, alert=False)

3. Schedule Daily Checks

# Run daily at 8 AM (auto-detects OS: crontab on Linux/macOS, schtasks on Windows)
thuhome-alert schedule --time 08:00

# Run daily at 6 PM
thuhome-alert schedule --time 18:00

# Uninstall the scheduled job
thuhome-alert schedule --uninstall

Or from Python:

import thuhome_alert

# Run daily at 8 AM
thuhome_alert.schedule_daily("08:00")

# Run daily at 6 PM
thuhome_alert.schedule_daily("18:00")

4. Migrate (no-op for compatibility)

thuhome-alert migrate

The config schema is already compatible with this version, so this command is a no-op that simply reports the config/data paths. It exists to provide a forward-compatible migration hook for future versions.

Configuration

The account.csv file (CSV format) has these columns — names match the R package exactly, including the non-obvious passwd and plural recipients:

column meaning
user portal username
passwd portal password (note: not password)
sender SMTP sender email
recipients alert recipient email (note: plural)
token SMTP password/token
water_threshold RMB; empty disables water monitoring
electr_threshold kWh; empty disables electricity monitoring
report_freq none / weekly / monthly
last_report ISO date string; updated after each report

Behavior notes

  • setup(update=True) merges into existing config (only overwrites provided fields); setup(force=True) overwrites the whole file. With neither, an existing config is left untouched.
  • setup() is interactive — it prompts via input() / getpass.getpass() for any missing param. In non-interactive contexts (cron, tests, agents) pass every param explicitly.
  • run_monitor() will auto-disable a threshold by writing None back to account.csv if the portal returns no balance for that utility (e.g. user has no water account). Both thresholds None triggers a RuntimeError.
  • report_freq = "weekly" fires when ≥7 days since last_report; "monthly" fires when the calendar month/year changes. After sending, last_report is overwritten in account.csv.

SMTP limitation

send_alert() is hardcoded to smtp.qq.com:587 — only works with a QQ Mail sender account. This mirrors the R package's behavior (per its AGENTS.md) and is intentionally not parameterized. Do not try to change this without explicit coordination — the limitation is documented as a known issue in both implementations.

Dependencies

  • Python >= 3.12
  • requests (HTTP client)
  • beautifulsoup4 (HTML parsing)

Optional (only for development):

  • pytest, pytest-cov, pytest-mock, responses (testing)
  • ruff (lint + format)
  • mypy (typecheck)
  • build (sdist/wheel building)

Development

# Clone
git clone https://github.com/bill0628/thuhome-alert-py.git
cd thuhome-alert-py

# Install with dev dependencies (uses uv)
uv sync --extra dev

# Run tests
uv run pytest

# Lint
uv run ruff check src/ tests/
uv run ruff format --check src/ tests/

# Typecheck
uv run mypy --strict src/thuhome_alert

# Build sdist + wheel
uv build

Comparison with R package

Aspect R thuhomeAlert Python thuhome-alert
Language R >= 4.1 Python >= 3.12
Runtime deps 8 packages + rJava JVM 2 packages (requests, beautifulsoup4)
SMTP mailR (requires JVM) stdlib smtplib
Cron scheduling cronR (Linux/mac) + taskscheduleR (Windows), separate functions stdlib subprocess calling crontab / schtasks, single unified schedule_daily()
Interactive prompts readline() / getPass::getPass() stdlib input() / getpass.getpass()
Data frames tibble / dplyr dataclass(frozen=True)
Cold start ~1-2s (R + tidyverse load) ~150ms
account.csv schema (canonical) byte-for-byte identical
CLI none (R functions only) thuhome-alert console script

The two implementations are designed to coexist: users can switch freely between them since the config and data files are interchangeable.

Scraping brittleness

The portal scraping depends on ASP.NET form field names and DOM element IDs (mirrored exactly from the R implementation):

  • Login form fields: net_Default_LoginCtrl1$txtUserName, net_Default_LoginCtrl1$txtPassword
  • Login success is detected by absence of noLogin in the redirect URL.
  • Balance elements:
    • #Netweb_Home_water_DetailCtrl1_lblele (water, suffix 元)
    • #Netweb_Home_electricity_DetailCtrl1_lblele (electricity, numeric)

If scraping breaks, these selectors are the first thing to verify against the live portal HTML.

License

MIT — see LICENSE.

Contributing

Issues and Pull Requests are welcome at github.com/bill0628/thuhome-alert-py.

See also

Metadata

Release files for thuhome-alert 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thuhome-alert 0.1.0
File Size Uploaded Provenance Actions
thuhome_alert-0.1.0.tar.gz 26.2 kB Without provenance Details for thuhome_alert-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thuhome-alert 0.1.0
File Interpreter ABI Platform Actions
thuhome_alert-0.1.0-py3-none-any.whl Python 3 none any Details for thuhome_alert-0.1.0-py3-none-any.whl

Total release size: 46.8 kB

Release files / thuhome_alert-0.1.0.tar.gz

Download URL thuhome_alert-0.1.0.tar.gz
Size 26.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ac05d4c41e061e17d622027f7c3253cd44b13513b140efa4f427df23ec06e23e
BLAKE2b-256 checksum
How to use checksums
2d317b79183f1fbdbbc805dd53ce4c06d840856c347893578a6d8e4a169b8e01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.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":null}

Release files / thuhome_alert-0.1.0-py3-none-any.whl

Download URL thuhome_alert-0.1.0-py3-none-any.whl
Size 20.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f940751397ccbe57afdac3e82843698048b0a004fa4af35d2c8e242726e2b178
BLAKE2b-256 checksum
How to use checksums
75a75e3f7d79b3064f206477081ad1bcf38b8c4f807bf09864c3c56153581d56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.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":null}

Security for thuhome-alert 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page