Skip to main content

Asynchronous Python API for TNS-Energo

Project description

aioTNSE

Asynchronous Python API for TNS-Energo.

Installation

Use pip to install the library:

pip install aiotnse

Usage

import asyncio
from pprint import pprint

import aiohttp

from aiotnse import SimpleTNSEAuth, TNSEApi


async def main(email: str, password: str, region: str) -> None:
    """Create the aiohttp session and run the example."""
    async with aiohttp.ClientSession() as session:
        auth = SimpleTNSEAuth(
            session,
            region=region,
            email=email,
            password=password,
        )

        # Login first
        await auth.async_login()

        api = TNSEApi(auth)

        # Get user accounts
        accounts = await api.async_get_accounts()
        pprint(accounts)

        # Get counters for an account
        account_number = accounts["data"][0]["number"]
        counters = await api.async_get_counters(account_number)
        pprint(counters)


if __name__ == "__main__":
    _email = input("Email: ")
    _password = input("Password: ")
    _region = input("Region (e.g. rostov, penza, yar): ")
    asyncio.run(main(_email, _password, _region))

You can also restore a session with saved tokens. The async_get_access_token() method automatically refreshes expired tokens or re-authenticates when needed:

from datetime import datetime


def on_token_update(token_data: dict) -> None:
    """Called after login or token refresh — persist the new tokens."""
    save_to_storage(token_data)


auth = SimpleTNSEAuth(
    session,
    region="rostov",
    email=email,
    password=password,
    access_token="saved_access_token",
    refresh_token="saved_refresh_token",
    access_token_expires=datetime.fromisoformat("2026-06-09T19:42:16"),
    refresh_token_expires=datetime.fromisoformat("2026-10-09T19:42:16"),
    token_update_callback=on_token_update,
)
# No need to call async_login() — tokens will be refreshed automatically on first API call

Contributors

Timeouts

aiotnse does not specify any timeouts for any requests. You will need to specify them in your own code. We recommend the timeout from asyncio package:

import asyncio

async with asyncio.timeout(10):
    data = await api.async_get_counters(account)

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

aiotnse-2.0.2.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

aiotnse-2.0.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file aiotnse-2.0.2.tar.gz.

File metadata

  • Download URL: aiotnse-2.0.2.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiotnse-2.0.2.tar.gz
Algorithm Hash digest
SHA256 61b037d1d439b76e3d31168b77efc579886ba796df6034ab92526687ae76587b
MD5 1c284474a37665cd7f279d4664a05466
BLAKE2b-256 62cda8c205b472dc6ae3044de40c74642689a8ae240acac11317744e2f14c4d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiotnse-2.0.2.tar.gz:

Publisher: publish.yml on lizardsystems/aiotnse

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

File details

Details for the file aiotnse-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: aiotnse-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiotnse-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8a233992147eb739cad7bf8b56630d71c03531529322542cdc1e4c5f2493f386
MD5 3d631ba7aaa581bfb0ca8a63bdac9167
BLAKE2b-256 688fa84945a9925c3620a710498e0bf29539614d3e72b769a84db1cbdbfe6929

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiotnse-2.0.2-py3-none-any.whl:

Publisher: publish.yml on lizardsystems/aiotnse

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