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.1.tar.gz (14.4 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.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiotnse-2.0.1.tar.gz
  • Upload date:
  • Size: 14.4 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.1.tar.gz
Algorithm Hash digest
SHA256 1204c652794981651dae1015a9c4f4f56b4b27590801570d317a8293f436f60a
MD5 b3bb74918e0b05de8412241af0079268
BLAKE2b-256 6d9b5bb00bf41f7e9c9ccc9821934dac7562b28fe31c7f03f728475806b0e1c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiotnse-2.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: aiotnse-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4add278d24f3e2548dc70458cc0b02ef2e58a1aee07647239ddd894fd202b97a
MD5 b29a1248bad8c918adf2a73d87d39f85
BLAKE2b-256 c6bfd16effaffb712820d79b966b526e55c06ebae8aafc22a26bb267253fa4f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiotnse-2.0.1-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