Metadata-Version: 2.5
Name: usms
Version: 1.2.0
Summary: An unofficial Python library to interface with your USMS account and smart meters.
Project-URL: homepage, https://github.com/azsaurr/usms
Project-URL: source, https://github.com/azsaurr/usms
Project-URL: changelog, https://github.com/azsaurr/usms/blob/main/CHANGELOG.md
Project-URL: releasenotes, https://github.com/azsaurr/usms/releases
Project-URL: documentation, https://github.com/azsaurr/usms
Project-URL: issues, https://github.com/azsaurr/usms/issues
Author-email: AZ <102905929+azsaurr@users.noreply.github.com>
License: MIT License
        
        Copyright (c) 2024 AZ
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: <4.0,>=3.10
Requires-Dist: httpx[http2]>=0.28.1
Requires-Dist: numpy>=1.26.0
Requires-Dist: pillow>=10.1.0
Description-Content-Type: text/markdown

# USMS

An unofficial Python library to interface with your [USMS](https://www.usms.com.bn/smartmeter/about.html) account and smart meters.

## Getting Started

### Installation

```sh
python -m pip install usms
```

### Quickstart

```sh
python -m usms --help
```

```sh
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
```

> [!NOTE]
> The `username` parameter is the login ID that you use to log-in on the USMS website/app, i.e. your IC Number.

As an example, you can use the following command to get the current remaining unit:

```sh
python -m usms -u <ic_number> -p <password> -m <meter> --unit
```

You can also use environment variables for the login information:

```sh
export USMS_USERNAME="<ic_number>"
export USMS_PASSWORD="<password>"
python -m usms -m <meter> --unit
```

Or:

```sh
USMS_USERNAME="<ic_number>" USMS_PASSWORD="<password>" python -m usms -m <meter> --unit
```

## Usage

```py
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:

1. 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
   `sc` query parameter. Wrong credentials still get the usual error message here.
2. That `ResLogin?sc=...` page serves the captcha that will actually be checked:
   five characters, lowercase letters and digits, warped and overlapping.
3. The credentials and the answer are posted to `ResLogin?sc=...`. A correct answer
   redirects through `LoginSession` with the `Sig` token, 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](https://github.com/sml2h3/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:

```py
account = initialize_usms_account(username="...", password="...", captcha_solver=my_solver)
```

> [!NOTE]
> This keeps an account holder's own access to their own meter data working. It
> sends no more requests than a person logging in would, and should USMS offer an
> official API, that would be the better route.

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

```py
# 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
* [x] Support for water meter
* [ ] Support for commercial/corporate accounts

## Contributing

### Prerequisites

1. [Generate an SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key) and [add the SSH key to your GitHub account](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account).
1. Configure SSH to automatically load your SSH keys:

    ```sh
    cat << EOF >> ~/.ssh/config
    
    Host *
      AddKeysToAgent yes
      IgnoreUnknown UseKeychain
      UseKeychain yes
      ForwardAgent yes
    EOF
    ```

1. [Install Docker Desktop](https://www.docker.com/get-started).
1. [Install VS Code](https://code.visualstudio.com/) and [VS Code's Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). Alternatively, install [PyCharm](https://www.jetbrains.com/pycharm/download/).
1. _Optional:_ install a [Nerd Font](https://www.nerdfonts.com/font-downloads) such as [FiraCode Nerd Font](https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/FiraCode) and [configure VS Code](https://github.com/tonsky/FiraCode/wiki/VS-Code-Instructions) or [PyCharm](https://github.com/tonsky/FiraCode/wiki/Intellij-products-instructions) to use it.

### Development Environments

The following development environments are supported:

1. ⭐️ _GitHub Codespaces_: click on [Open in GitHub Codespaces](https://github.com/codespaces/new/user/user) to start developing in your browser.
1. ⭐️ _VS Code Dev Container (with container volume)_: click on [Open in Dev Containers](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/user/user) to clone this repository in a container volume and create a Dev Container with VS Code.
1. ⭐️ _uv_: clone this repository and run the following from root of the repository:

    ```sh
    # 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
    ```

1. _VS Code Dev Container_: clone this repository, open it with VS Code, and run <kbd>Ctrl/⌘</kbd> + <kbd>⇧</kbd> + <kbd>P</kbd> → _Dev Containers: Reopen in Container_.
1. _PyCharm Dev Container_: clone this repository, open it with PyCharm, [create a Dev Container with Mount Sources](https://www.jetbrains.com/help/pycharm/start-dev-container-inside-ide.html), and [configure an existing Python interpreter](https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#widget) at `/opt/venv/bin/python`.
2. _nix develop_: clone this repository and run `nix develop`.

### Developing

* This project follows the [Conventional Commits](https://www.conventionalcommits.org/) standard to automate [Semantic Versioning](https://semver.org/) and [Keep A Changelog](https://keepachangelog.com/) with [Commitizen](https://github.com/commitizen-tools/commitizen).
* Run `poe` from within the development environment to print a list of [Poe the Poet](https://github.com/nat-n/poethepoet) 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

* [USMS](https://www.usms.com.bn/smartmeter/about.html)
* [ddddocr](https://github.com/sml2h3/ddddocr) by [sml2h3](https://github.com/sml2h3), MIT
  licensed. The captcha solver's model (`src/usms/captcha/graph.json` and
  `weights.npz`) is derived from ddddocr's `common_old.onnx`, reduced to the
  characters USMS uses. ddddocr's licence is reproduced in
  `src/usms/captcha/LICENSE-ddddocr`.
* [ONNX](https://onnx.ai/) and [ONNX Runtime](https://onnxruntime.ai/), whose operator
  specifications (including the `com.microsoft` contrib operators such as
  `DynamicQuantizeLSTM`) the numpy interpreter in `usms.captcha.model` follows.

### Project Template

This project was built using the [superlinear-ai/substrate](https://github.com/superlinear-ai/substrate) template.
