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)
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
Project Template
This project was built using the superlinear-ai/substrate template.
Metadata
Release files for usms 1.0.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.0.0.tar.gz | 118.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| usms-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 171.4 kB
Release files / usms-1.0.0.tar.gz
| Download URL | usms-1.0.0.tar.gz |
|---|---|
| Size | 118.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff65c37e503954235048ed79f66861a2ff2daa58fd7ea4e31ef655174bd9b97d
|
|
BLAKE2b-256 checksum How to use checksums |
fa3b1e2a1e848508ae0e44e6481b3025f8fb53472486bb79d4e9a758a00ee079
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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.0.0-py3-none-any.whl
| Download URL | usms-1.0.0-py3-none-any.whl |
|---|---|
| Size | 52.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
362e8393d844db5ef53ba68c1140103c2810bd40e5581fd96b60bebff4bc736d
|
|
BLAKE2b-256 checksum How to use checksums |
181433b4b949e3a52b0d2ac3eaebf6bde44cfd45c2a71352972124968f4da2f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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}
|