Skip to main content

Async Python client for Energy Tracker API

Project description

Energy Tracker API Client

Python client for the Energy Tracker public REST API.

Installation

pip install energy-tracker-api

Requirements

  • Python 3.10+
  • Personal Access Token from Energy Tracker
  • Async/await support (asyncio)

Usage

import asyncio
from energy_tracker_api import EnergyTrackerClient, CreateMeterReadingDto
from datetime import datetime

async def main():
    client = EnergyTrackerClient(access_token="your-token")

    meter_reading = CreateMeterReadingDto(
        value=123.45,
        timestamp=datetime.now(),     # Optional - server uses current time if omitted
        note="Manual reading"         # Optional
    )

    await client.meter_readings.create(
        device_id="your-device-id",
        meter_reading=meter_reading,
        allow_rounding=True           # Optional
    )

    await client.close()

asyncio.run(main())

Async Context Manager

import asyncio
from energy_tracker_api import EnergyTrackerClient, CreateMeterReadingDto

async def main():
    async with EnergyTrackerClient(access_token="your-token") as client:
        reading = CreateMeterReadingDto(value=456.78)
        await client.meter_readings.create("your-device-id", reading)

asyncio.run(main())

Configuration

client = EnergyTrackerClient(
    access_token="your-token",
    base_url="https://custom-api.example.com",  # Optional
    timeout=30                                  # Optional, default: 10 seconds
)

Error Handling

from energy_tracker_api import (
    EnergyTrackerAPIError,
    ValidationError,
    AuthenticationError,
    ForbiddenError,
    ResourceNotFoundError,
    ConflictError,
    RateLimitError
)

try:
    client.meter_readings.create(device_id, meter_reading)
except ValidationError as e:
    print(f"Validation error: {e}")
    print(f"Details: {e.api_message}")
except AuthenticationError:
    print("Authentication failed - check access token")
except ForbiddenError:
    print("Insufficient permissions")
except ResourceNotFoundError:
    print("No valid meter for timestamp")
except ConflictError:
    print("Meter reading already exists for timestamp")
except RateLimitError as e:
    print(f"Rate limit exceeded")
    if e.retry_after:
        print(f"Retry after {e.retry_after} seconds")
except EnergyTrackerAPIError as e:
    print(f"API error: {e}")

API Reference

EnergyTrackerClient

__init__(access_token, base_url=None, timeout=10)

  • access_token (str): Personal Access Token
  • base_url (str, optional): API base URL
  • timeout (int, optional): Request timeout in seconds

MeterReadingResource

meter_readings.create(device_id, meter_reading, allow_rounding=None)

Create a new meter reading.

  • device_id (str): Device identifier
  • meter_reading (CreateMeterReadingDto): Meter reading data
  • allow_rounding (bool, optional): Allow rounding to meter precision

CreateMeterReadingDto

CreateMeterReadingDto(value, timestamp=None, note=None)

  • value (float): Meter reading value
  • timestamp (datetime, optional): Reading timestamp (server uses current time if omitted)
  • note (str, optional): Note for the meter reading

Development

make install-dev  # Install dependencies
make test         # Run tests
make format       # Format code
make lint         # Run linters

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

energy_tracker_api-1.0.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

energy_tracker_api-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file energy_tracker_api-1.0.0.tar.gz.

File metadata

  • Download URL: energy_tracker_api-1.0.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for energy_tracker_api-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e21f98438d193aa57f46b1afd95024d4e87c8673476676e56088dbd31f0038c1
MD5 81bd679d84680d99f06ecd4c4765540b
BLAKE2b-256 014a66c10a0fca9a03ea3146844a6970239d4d4eb73144bbbe31f6ac12a9f38a

See more details on using hashes here.

File details

Details for the file energy_tracker_api-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for energy_tracker_api-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6eaa0fe2c9d773d98e8ad9bdee26757b52bd7c42b9a3ae53067c9bf0efb9d73
MD5 b0a3b60bc12d051eab2457df152df972
BLAKE2b-256 0262d6261193ea0224b3fb0f0820fe3aeef6966053ff96bce60a0ce195946003

See more details on using hashes here.

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