USMS
An unofficial Python library to interface with your USMS account and smart meters.
Getting Started
Installation
python -m pip install usms
Quickstart
python -m usms --help
usage: __main__.py [-h] [-l LOG] -u USERNAME -p PASSWORD [-m METER] [--unit] [--consumption] [--credit]
options:
-h, --help show this help message and exit
-l LOG, --log LOG
-u USERNAME, --username USERNAME
-p PASSWORD, --password PASSWORD
-m METER, --meter METER
--unit
--consumption
--credit
As an example, you can use the following command to get the current remaining unit:
python -m usms -u <ic_number> -p <password> -m <meter> --unit
You can also use environment variables for the login information:
export USMS_USERNAME="<ic_number>"
export USMS_PASSWORD="<password>"
python -m usms -m <meter> --unit
Or:
USMS_USERNAME="<ic_number>" USMS_PASSWORD="<password>" python -m usms -m <meter> --unit
Usage
import httpx
from usms import initialize_usms_account
username = "01001234" # your ic number
password = "hunter1"
# initialize the account
account = initialize_usms_account(
username=username,
password=password,
client=httpx.Client(), # or httpx.AsyncClient(), optional
)
# if you let the library create the client, use a context manager so it gets
# closed. A client you pass in stays yours and is never closed for you.
with initialize_usms_account(username=username, password=password) as account:
... # async: `async with await initialize_usms_account(..., async_mode=True)`
# print out the account information
print(account.name)
# print out info on all meters under the account
for meter in account.meters:
print(meter.no)
print(meter.type)
print(meter.address)
print(meter.remaining_unit)
print(meter.remaining_credit)
print(meter.unit)
# get the number of the second meter
meter_no = account.meters[1].no
# to get info from a specific meter
meter = account.get_meter(meter_no)
# get today's consumptions
print(meter.get_last_n_days_hourly_consumptions(n=0))
# getting daily breakdown of last month's comsumptions
daily_consumptions = meter.get_previous_n_month_consumptions(n=1)
print(daily_consumptions)
# get last month's total cost based on total consumption
print(meter.calculate_total_cost(daily_consumptions))
# water meters have no hourly report, only a daily one
if not meter.supports_hourly_consumptions:
print(meter.get_all_daily_consumptions())
# debt and customer details, from the meter's Top Up page
meter.fetch_payment_info()
print(meter.customer_type, meter.total_debt_owing, meter.has_debt)
# topping up cannot be automated - USMS redirects to the bank - so open this
print(meter.topup_url)
Login captcha
What changed
In October 2026 USMS added an image captcha (a DevExpress ASPxCaptcha) to the
residential login. Every login since then has failed with USMSLoginError: Invalid login., even with the correct password. The captcha is a second step, not a field
of the first form:
- The username and password are posted with the captcha left empty. If they are
correct, USMS answers with a redirect back to the login page, now carrying an
scquery parameter. Wrong credentials still get the usual error message here. - That
ResLogin?sc=...page serves the captcha that will actually be checked: five characters, lowercase letters and digits, warped and overlapping. - The credentials and the answer are posted to
ResLogin?sc=.... A correct answer redirects throughLoginSessionwith theSigtoken, as before.
USMS also added a field to the meter card at the same time, which shifted the position of every field after it, so meter details are now read by their label rather than by position.
How the library gets past it
The library performs the step itself. It reads the captcha with a small text-recognition network bundled with the package, fetches a fresh captcha instead of submitting when a guess is not five characters long, and retries with a new one when an answer is rejected. Measured against live USMS captchas, about nine in ten submitted answers are accepted, and three attempts are allowed per login.
The network is ddddocr's general-purpose
OCR model (common_old.onnx). ddddocr itself needs onnxruntime, which publishes
no wheels for musl Linux and so cannot be installed on Home Assistant OS. The
model is therefore exported once (see tools/export_captcha_model.py) into a
node list and weight arrays, and usms.captcha evaluates it with numpy alone. Its
8210 output classes are cut down to the CTC blank plus a-z and 0-9, which shrinks
the model to 3.7 MB and means it can never answer with a character USMS does
not draw. The only new dependencies are numpy and Pillow.
If every attempt fails, the login raises USMSCaptchaError. It is deliberately
not a USMSLoginError: the credentials were accepted, and retrying later with
fresh captchas usually works, whereas prompting for the password again would not.
To use your own solver, pass any callable that takes the captcha image as PNG bytes and returns its text:
account = initialize_usms_account(username="...", password="...", captcha_solver=my_solver)
Migrating from 0.x
Consumptions are now plain dictionaries. Every method that returned a
pandas.Series now returns a dict[datetime, float] keyed by the start of each
period, and pandas is no longer a dependency at all. The keys are always
timezone-aware and in Brunei time.
# 0.x
consumptions.empty consumptions.sum() consumptions.index.min()
# 1.0
not consumptions sum(consumptions.values()) min(consumptions)
Gaps are simply absent rather than materialised as NaN rows, so summing and
iterating skip them without special handling.
Hourly readings were shifted by an hour. 0.x mapped the first row of a day's report to 23:00 of the previous day. Besides misdating every reading, that stray timestamp made the cache believe it already held data for the previous day, so fetching that day returned a single row instead of 24. If you have stored consumptions or imported statistics from 0.x, re-fetch them to correct both.
Other changes. calculate_unit() takes an optional consumed_units so credit
can be priced from the tier a meter has actually reached, rather than from zero.
An exhausted re-authentication now raises USMSLoginError instead of quietly
returning the login page as if it were data.
To-Do
- Add more test coverage
- Support for water meter
- Support for commercial/corporate accounts
Contributing
Prerequisites
-
Generate an SSH key and add the SSH key to your GitHub account.
-
Configure SSH to automatically load your SSH keys:
cat << EOF >> ~/.ssh/config Host * AddKeysToAgent yes IgnoreUnknown UseKeychain UseKeychain yes ForwardAgent yes EOF
-
Install VS Code and VS Code's Dev Containers extension. Alternatively, install PyCharm.
-
Optional: install a Nerd Font such as FiraCode Nerd Font and configure VS Code or PyCharm to use it.
Development Environments
The following development environments are supported:
-
⭐️ GitHub Codespaces: click on Open in GitHub Codespaces to start developing in your browser.
-
⭐️ VS Code Dev Container (with container volume): click on Open in Dev Containers to clone this repository in a container volume and create a Dev Container with VS Code.
-
⭐️ uv: clone this repository and run the following from root of the repository:
# Create and install a virtual environment uv sync --python 3.10 --all-extras # Activate the virtual environment source .venv/bin/activate # Install the pre-commit hooks pre-commit install --install-hooks
-
VS Code Dev Container: clone this repository, open it with VS Code, and run Ctrl/⌘ + ⇧ + P → Dev Containers: Reopen in Container.
-
PyCharm Dev Container: clone this repository, open it with PyCharm, create a Dev Container with Mount Sources, and configure an existing Python interpreter at
/opt/venv/bin/python. -
nix develop: clone this repository and run
nix develop.
Developing
- This project follows the Conventional Commits standard to automate Semantic Versioning and Keep A Changelog with Commitizen.
- Run
poefrom within the development environment to print a list of Poe the Poet tasks available to run on this project. - Run
uv add {package}from within the development environment to install a run time dependency and add it topyproject.tomlanduv.lock. Add--devto install a development dependency. - Run
uv sync --upgradefrom within the development environment to upgrade all dependencies to the latest versions allowed bypyproject.toml. Add--only-devto upgrade the development dependencies only. - Run
cz bumpto bump the package's version, update theCHANGELOG.md, and create a git tag. Then push the changes and the git tag withgit push origin main --tags.
License
Distributed under the MIT License. See LICENSE for more information.
Acknowledgments
- USMS
- ddddocr by sml2h3, MIT
licensed. The captcha solver's model (
src/usms/captcha/graph.jsonandweights.npz) is derived from ddddocr'scommon_old.onnx, reduced to the characters USMS uses. ddddocr's licence is reproduced insrc/usms/captcha/LICENSE-ddddocr. - ONNX and ONNX Runtime, whose operator
specifications (including the
com.microsoftcontrib operators such asDynamicQuantizeLSTM) the numpy interpreter inusms.captcha.modelfollows.
Project Template
This project was built using the superlinear-ai/substrate template.
Metadata
Release files for usms 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| usms-1.2.0.tar.gz | 3.8 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| usms-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.5 MB
Release files / usms-1.2.0.tar.gz
| Download URL | usms-1.2.0.tar.gz |
|---|---|
| Size | 3.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa080bc92c81b8fa740428a532b1e0156b69d79ee93b2a0029f004d2b790a83a
|
|
BLAKE2b-256 checksum How to use checksums |
a870548fd9798f832c3cb86f62da3b3a0d61ca2bfa2a022aad85ea334d5a4e35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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}
|
Release files / usms-1.2.0-py3-none-any.whl
| Download URL | usms-1.2.0-py3-none-any.whl |
|---|---|
| Size | 3.7 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
327723a8494ef2f6aa6c9c53cf2252149bf133cf71c2f1263a44888b130c2ad5
|
|
BLAKE2b-256 checksum How to use checksums |
456d3c38e3bfc7f1ecdfb8722aacb05688e36656abae1bd85415177c851cfecd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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}
|