Skip to main content

A python library to interface with the KUB api

Project description

KUB

An async Python library for retrieving utility usage data from the Knoxville Utilities Board (KUB) API.

Features

  • Authenticates via Azure AD B2C using PKCE OAuth2, mirroring the KUB web app flow
  • Supports electricity, gas, water, and wastewater services
  • Retrieves hourly usage and cost data for any date range
  • Session management with automatic token refresh
  • Compatible with Home Assistant custom components

Requirements

  • Python 3.12 or higher
  • aiohttp 3.9 or higher

Installation

pip install kub

Usage

Basic setup

import asyncio
from kub import KubUtility

async def main():
    utility = KubUtility("your@email.com", "yourpassword")

    # Retrieve usage for the last 31 days
    usage = await utility.retrieve_last_31_days()
    print(usage)

asyncio.run(main())

Available methods

Method Description
retrieve_last_31_days() Fetches hourly usage for the past 31 days across all services
retrieve_monthly_usage() Fetches hourly usage from the first of the current month to today
retrieve_usage_by_range(start_date, end_date) Fetches usage for a custom range; dates are YYYY-MM-DD strings
retrieve_account_info() Fetches account metadata (account ID, person ID, service list) without pulling usage data

Usage data structure

The usage attribute is a dictionary keyed by utility type, then by date, then by time:

{
    "electricity": {
        "2026-05-01": {
            "08:00:00": {
                "id": "...",
                "readDateTime": "2026-05-01T08:00:00",
                "utilityUsed": 0.42,
                "uom": "kWh",
                "cost": 0.06
            },
            ...
        },
        ...
    },
    "gas": { ... },
    "water": { ... },
    "wastewater": { ... }
}

Monthly totals (for the current calendar month) are available via monthly_total:

utility.monthly_total["electricity"]
# {"usage": 312.5, "cost": 42.18}

Supported utility types

The KUBUtilityTypes enum represents the services KUB provides:

from kub import KUBUtilityTypes

KUBUtilityTypes.ELECTRICITY
KUBUtilityTypes.GAS
KUBUtilityTypes.WATER
KUBUtilityTypes.WASTEWATER

Only the services active on your account are populated. Check utility.service_list after calling any retrieval method to see which types are present.

Home Assistant

This library is designed to work as a dependency for a Home Assistant custom component. Place the kub directory inside custom_components/ and import from it using standard relative imports.

Development

Install development dependencies:

pip install -e ".[dev]"

Run the test suite:

pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kub-0.6.5.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kub-0.6.5-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file kub-0.6.5.tar.gz.

File metadata

  • Download URL: kub-0.6.5.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kub-0.6.5.tar.gz
Algorithm Hash digest
SHA256 ac5fb17d0df739d2954aee75a28c1084d97b8055290b45d9e06af77dbb46e0b1
MD5 8765ef8ad73201bc7fd9c5797b4c351c
BLAKE2b-256 00bf80cada82e8307f268a4b560e9018e23976ca984cab1a0271bc31ee0f4030

See more details on using hashes here.

Provenance

The following attestation bundles were made for kub-0.6.5.tar.gz:

Publisher: release.yml on JackJPowell/KUB

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kub-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: kub-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kub-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2ff2cbb88d19bbade9b953ef1089f0e1d419f9ca1cf1b9b8e6d4cadb3180282f
MD5 56476f3a51d9ee6a13ace0489daa2191
BLAKE2b-256 92cb5ccb0fdbe67791f8f85169114adf85723684df226aa5dcaf906728bac7d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for kub-0.6.5-py3-none-any.whl:

Publisher: release.yml on JackJPowell/KUB

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page