Skip to main content

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

  1. Generate an SSH key and add the SSH key to your GitHub account.

  2. Configure SSH to automatically load your SSH keys:

    cat << EOF >> ~/.ssh/config
    
    Host *
      AddKeysToAgent yes
      IgnoreUnknown UseKeychain
      UseKeychain yes
      ForwardAgent yes
    EOF
    
  3. Install Docker Desktop.

  4. Install VS Code and VS Code's Dev Containers extension. Alternatively, install PyCharm.

  5. 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:

  1. ⭐️ GitHub Codespaces: click on Open in GitHub Codespaces to start developing in your browser.

  2. ⭐️ 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.

  3. ⭐️ 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
    
  4. VS Code Dev Container: clone this repository, open it with VS Code, and run Ctrl/⌘ + ⇧ + P → Dev Containers: Reopen in Container.

  5. 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.

  6. 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 poe from 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 to pyproject.toml and uv.lock. Add --dev to install a development dependency.
  • Run uv sync --upgrade from within the development environment to upgrade all dependencies to the latest versions allowed by pyproject.toml. Add --only-dev to upgrade the development dependencies only.
  • Run cz bump to bump the package's version, update the CHANGELOG.md, and create a git tag. Then push the changes and the git tag with git 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.1.2

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

Source distribution (sdist)

Source distribution for usms 1.1.2
File Size Uploaded
usms-1.1.2.tar.gz 123.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for usms 1.1.2
File Interpreter ABI Platform
usms-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 178.8 kB

Release files / usms-1.1.2.tar.gz

Download URL usms-1.1.2.tar.gz
Size 123.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9e3085a27fe98a9031ef1058ecb58a3595e2f0be37995e1daa4e25705e4f510d
BLAKE2b-256 checksum
How to use checksums
8b6976f6c2368f3a2abf80af53906f8e358835b10c0ee9ade975097a21f75627
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","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.1.2-py3-none-any.whl

Download URL usms-1.1.2-py3-none-any.whl
Size 55.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
253a2d2ea91d2f12bb0566be95e5259b032090a41540c8ece262b414a19d7cdc
BLAKE2b-256 checksum
How to use checksums
fbd1b93860dc1db0751b983ad51e947621c68cfc949ef9e4042252aef0d1d5a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","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 history Release notifications | RSS feed

1.2.0

2 release files

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.10

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

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