An asyncio-based Python3 library for interacting with WattTime
Project description
🌎 aiowatttime: an asyncio-based, Python3 library for WattTime emissions data
aiowatttime is a Python 3, asyncio-friendly library for interacting with
WattTime emissions data.
Python Versions
aiowatttime is currently supported on:
- Python 3.10
- Python 3.11
- Python 3.12
Installation
pip install aiowatttime
Usage
Getting an API Key
Simply clone this repo and run the included interactive script:
$ script/register
Note that WattTime offers three plans: Visitors, Analyst, and Pro. The type you use will determine which elements of this library are available to use. You can read more details here.
Creating and Using a Client
The Client is the primary method of interacting with the API:
import asyncio
from aiowatttime import Client
async def main() -> None:
client = await Client.login("<USERNAME>", "<PASSWORD>")
# ...
asyncio.run(main())
By default, the library creates a new connection to the API with each coroutine. If
you are calling a large number of coroutines (or merely want to squeeze out every second
of runtime savings possible), an aiohttp ClientSession can be used for
connection pooling:
import asyncio
from aiohttp import ClientSession
from aiowatttime import Client
async def main() -> None:
async with ClientSession() as session:
client = await Client.login("<USERNAME>", "<PASSWORD>", session=session)
# ...
asyncio.run(main())
Programmatically Requesting a Password Reset
await client.async_request_password_reset()
Getting Emissions Data
Grid Region
It may be useful to first get the "grid region" (i.e., geographical info) for the area you care about:
await client.emissions.async_get_grid_region(
"<LATITUDE>", "<LONGITUDE>", "<SIGNAL_TYPE>"
)
# >>> { "region": "PSCO", "region_full_name": "Public Service Co of Colorado", "signal_type": "co2_moer" }
Getting emissions data will require the region abbreviation (PSCO in the example above).
Realtime Data
await client.emissions.async_get_realtime_emissions("<REGION>")
# >>>
{"data": [...]}
Forecasted Data
from datetime import datetime
await client.emissions.async_get_forecasted_emissions(
"<REGION>", "<SIGNAL_TYPE>", datetime(2021, 1, 1), datetime(2021, 2, 1)
)
# >>> { "data": [ ... ] }
Historical Data
await client.emissions.async_get_forecasted_emissions(
"<REGION>", "<SIGNAL_TYPE>", datetime(2021, 1, 1), datetime(2021, 2, 1)
)
# >>> [ { "point_time": "2019-02-21T00:15:00.000Z", "value": 844, ... } ]
Retry Logic
By default, aiowatttime will handle expired access tokens for you. When a token expires,
the library will attempt the following sequence 3 times:
- Request a new token
- Pause for 1 second (to be respectful of the API rate limiting)
- Execute the original request again
Both the number of retries and the delay between retries can be configured when instantiating a client:
import asyncio
from aiohttp import ClientSession
from aiowatttime import Client
async def main() -> None:
async with ClientSession() as session:
client = await Client.async_login(
"user",
"password",
session=session,
# Make 7 retry attempts:
request_retries=7,
# Delay 4 seconds between attempts:
request_retry_delay=4,
)
asyncio.run(main())
As always, an invalid username/password combination will immediately throw an exception.
Custom Logger
By default, aiowatttime provides its own logger. If you should wish to use your own, you
can pass it to the client during instantiation:
import asyncio
import logging
from aiohttp import ClientSession
from aiowatttime import Client
CUSTOM_LOGGER = logging.getLogger("my_custom_logger")
async def main() -> None:
async with ClientSession() as session:
client = await Client.async_login(
"user",
"password",
session=session,
logger=logger,
)
asyncio.run(main())
Contributing
Thanks to all of our contributors so far!
- Check for open features/bugs or initiate a discussion on one.
- Fork the repository.
- (optional, but highly recommended) Create a virtual environment:
python3 -m venv .venv - (optional, but highly recommended) Enter the virtual environment:
source ./.venv/bin/activate - Install the dev environment:
script/setup - Code your new feature or bug fix on a new branch.
- Write tests that cover your new functionality.
- Run tests and ensure 100% code coverage:
poetry run pytest --cov aiowatttime tests - Update
README.mdwith any new documentation. - Submit a pull request!
Project details
Release history Release notifications | RSS feed
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 aiowatttime-2024.6.0.tar.gz.
File metadata
- Download URL: aiowatttime-2024.6.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af689438b17254ea2e16f17fff39737abd57c46500302c0cf4a2844ed4d26829
|
|
| MD5 |
c2bccd9ce54ba7ad610adf99b56e9f9d
|
|
| BLAKE2b-256 |
0d4f86f9aa9116e7d7da803acbc4d8d3c95cd830afd9d87bdf07d82b0cfde022
|
File details
Details for the file aiowatttime-2024.6.0-py3-none-any.whl.
File metadata
- Download URL: aiowatttime-2024.6.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efbd8628c7a8bd6eda078669ec361648a634def900942b33b0952312d9570c7c
|
|
| MD5 |
a2e539f5139a188f01529fa816db14e6
|
|
| BLAKE2b-256 |
bdbd44b2a74281a3f8a8e56be4f5eede16732fda66ee4109eda0d1a164841478
|