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:
await 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 Tokenbase_url(str, optional): API base URLtimeout(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 identifiermeter_reading(CreateMeterReadingDto): Meter reading dataallow_rounding(bool, optional): Allow rounding to meter precision
CreateMeterReadingDto
CreateMeterReadingDto(value, timestamp=None, note=None)
value(float): Meter reading valuetimestamp(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file energy_tracker_api-1.1.0.tar.gz.
File metadata
- Download URL: energy_tracker_api-1.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d5fe64d0e48f26d9d774c4d326894103b7eac16617b52813ff7f6aade5dfa21
|
|
| MD5 |
544f375c072cfd419ca0fcdcf95b1341
|
|
| BLAKE2b-256 |
313f1e44b8147fd1a617e88575ad85b22be84dc515d23ee97b21cd111d5a2133
|
Provenance
The following attestation bundles were made for energy_tracker_api-1.1.0.tar.gz:
Publisher:
release.yml on energy-tracker/api-client-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
energy_tracker_api-1.1.0.tar.gz -
Subject digest:
4d5fe64d0e48f26d9d774c4d326894103b7eac16617b52813ff7f6aade5dfa21 - Sigstore transparency entry: 953237753
- Sigstore integration time:
-
Permalink:
energy-tracker/api-client-python@b8c36a68b23b697d9f01b303367315d90c83e6b3 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/energy-tracker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b8c36a68b23b697d9f01b303367315d90c83e6b3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file energy_tracker_api-1.1.0-py3-none-any.whl.
File metadata
- Download URL: energy_tracker_api-1.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb4582540bb100a8175eeab0d8f92592a59a92e57be27266e515470920a9d353
|
|
| MD5 |
ce864d677b4abb96d06bba2b6e8acdee
|
|
| BLAKE2b-256 |
5e85882b18177f0f7b48b71c5abe4ac4937f1b575d77b551a86e4aa872c22208
|
Provenance
The following attestation bundles were made for energy_tracker_api-1.1.0-py3-none-any.whl:
Publisher:
release.yml on energy-tracker/api-client-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
energy_tracker_api-1.1.0-py3-none-any.whl -
Subject digest:
eb4582540bb100a8175eeab0d8f92592a59a92e57be27266e515470920a9d353 - Sigstore transparency entry: 953237754
- Sigstore integration time:
-
Permalink:
energy-tracker/api-client-python@b8c36a68b23b697d9f01b303367315d90c83e6b3 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/energy-tracker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b8c36a68b23b697d9f01b303367315d90c83e6b3 -
Trigger Event:
push
-
Statement type: