# 🌎 aiowatttime: an asyncio-based, Python3 library for WattTime emissions data
[![CI][ci-badge]][ci]
[![PyPI][pypi-badge]][pypi]
[![Version][version-badge]][version]
[![License][license-badge]][license]
[![Code Coverage][codecov-badge]][codecov]
[![Maintainability][maintainability-badge]][maintainability]
<a href="https://www.buymeacoffee.com/bachya1208P" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>
`aiowatttime` is a Python 3, asyncio-friendly library for interacting with
[WattTime](https://www.watttime.org) emissions data.
- [Python Versions](#python-versions)
- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)
# Python Versions
`aiowatttime` is currently supported on:
- Python 3.10
- Python 3.11
- Python 3.12
# Installation
```bash
pip install aiowatttime
```
# Usage
## Getting an API Key
Simply clone this repo and run the included interactive script:
```bash
$ 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][watttime-data-plans].
## Creating and Using a Client
The `Client` is the primary method of interacting with the API:
```python
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`][aiohttp] `ClientSession` can be used for
connection pooling:
```python
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
```python
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:
```python
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
```python
await client.emissions.async_get_realtime_emissions("<REGION>")
# >>>
{"data": [...]}
```
### Forecasted Data
```python
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
```python
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:
```python
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:
```python
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][contributors] so far!
1. [Check for open features/bugs][issues] or [initiate a discussion on one][new-issue].
2. [Fork the repository][fork].
3. (_optional, but highly recommended_) Create a virtual environment: `python3 -m venv .venv`
4. (_optional, but highly recommended_) Enter the virtual environment: `source ./.venv/bin/activate`
5. Install the dev environment: `script/setup`
6. Code your new feature or bug fix on a new branch.
7. Write tests that cover your new functionality.
8. Run tests and ensure 100% code coverage: `poetry run pytest --cov aiowatttime tests`
9. Update `README.md` with any new documentation.
10. Submit a pull request!
[aiohttp]: https://github.com/aio-libs/aiohttp
[ci-badge]: https://img.shields.io/github/actions/workflow/status/bachya/aiowatttime/test.yml
[ci]: https://github.com/bachya/aiowatttime/actions
[codecov-badge]: https://codecov.io/gh/bachya/aiowatttime/branch/dev/graph/badge.svg
[codecov]: https://codecov.io/gh/bachya/aiowatttime
[contributors]: https://github.com/bachya/aiowatttime/graphs/contributors
[fork]: https://github.com/bachya/aiowatttime/fork
[issues]: https://github.com/bachya/aiowatttime/issues
[license-badge]: https://img.shields.io/pypi/l/aiowatttime.svg
[license]: https://github.com/bachya/aiowatttime/blob/main/LICENSE
[maintainability-badge]: https://api.codeclimate.com/v1/badges/781e64940b1302ae9ac3/maintainability
[maintainability]: https://codeclimate.com/github/bachya/aiowatttime/maintainability
[new-issue]: https://github.com/bachya/aiowatttime/issues/new
[new-issue]: https://github.com/bachya/aiowatttime/issues/new
[pypi-badge]: https://img.shields.io/pypi/v/aiowatttime.svg
[pypi]: https://pypi.python.org/pypi/aiowatttime
[version-badge]: https://img.shields.io/pypi/pyversions/aiowatttime.svg
[version]: https://pypi.python.org/pypi/aiowatttime
[watttime]: https://www.watttime.org
[watttime-data-plans]: https://www.watttime.org/get-the-data/data-plans/
Raw data
{
"_id": null,
"home_page": "https://github.com/bachya/aiowatttime",
"name": "aiowatttime",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Aaron Bach",
"author_email": "bachya1208@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0d/4f/86f9aa9116e7d7da803acbc4d8d3c95cd830afd9d87bdf07d82b0cfde022/aiowatttime-2024.6.0.tar.gz",
"platform": null,
"description": "# \ud83c\udf0e aiowatttime: an asyncio-based, Python3 library for WattTime emissions data\n\n[![CI][ci-badge]][ci]\n[![PyPI][pypi-badge]][pypi]\n[![Version][version-badge]][version]\n[![License][license-badge]][license]\n[![Code Coverage][codecov-badge]][codecov]\n[![Maintainability][maintainability-badge]][maintainability]\n\n<a href=\"https://www.buymeacoffee.com/bachya1208P\" target=\"_blank\"><img src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy Me A Coffee\" height=\"41\" width=\"174\"></a>\n\n`aiowatttime` is a Python 3, asyncio-friendly library for interacting with\n[WattTime](https://www.watttime.org) emissions data.\n\n- [Python Versions](#python-versions)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Contributing](#contributing)\n\n# Python Versions\n\n`aiowatttime` is currently supported on:\n\n- Python 3.10\n- Python 3.11\n- Python 3.12\n\n# Installation\n\n```bash\npip install aiowatttime\n```\n\n# Usage\n\n## Getting an API Key\n\nSimply clone this repo and run the included interactive script:\n\n```bash\n$ script/register\n```\n\nNote that WattTime offers three plans: Visitors, Analyst, and Pro. The type you use\nwill determine which elements of this library are available to use. You can read more\ndetails [here][watttime-data-plans].\n\n## Creating and Using a Client\n\nThe `Client` is the primary method of interacting with the API:\n\n```python\nimport asyncio\n\nfrom aiowatttime import Client\n\n\nasync def main() -> None:\n client = await Client.login(\"<USERNAME>\", \"<PASSWORD>\")\n # ...\n\n\nasyncio.run(main())\n```\n\nBy default, the library creates a new connection to the API with each coroutine. If\nyou are calling a large number of coroutines (or merely want to squeeze out every second\nof runtime savings possible), an [`aiohttp`][aiohttp] `ClientSession` can be used for\nconnection pooling:\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aiowatttime import Client\n\n\nasync def main() -> None:\n async with ClientSession() as session:\n client = await Client.login(\"<USERNAME>\", \"<PASSWORD>\", session=session)\n # ...\n\n\nasyncio.run(main())\n```\n\n## Programmatically Requesting a Password Reset\n\n```python\nawait client.async_request_password_reset()\n```\n\n## Getting Emissions Data\n\n### Grid Region\n\nIt may be useful to first get the \"grid region\" (i.e., geographical info) for the area\nyou care about:\n\n```python\nawait client.emissions.async_get_grid_region(\n \"<LATITUDE>\", \"<LONGITUDE>\", \"<SIGNAL_TYPE>\"\n)\n# >>> { \"region\": \"PSCO\", \"region_full_name\": \"Public Service Co of Colorado\", \"signal_type\": \"co2_moer\" }\n```\n\nGetting emissions data will require the region abbreviation (`PSCO` in the example above).\n\n### Realtime Data\n\n```python\nawait client.emissions.async_get_realtime_emissions(\"<REGION>\")\n# >>>\n{\"data\": [...]}\n```\n\n### Forecasted Data\n\n```python\nfrom datetime import datetime\n\nawait client.emissions.async_get_forecasted_emissions(\n \"<REGION>\", \"<SIGNAL_TYPE>\", datetime(2021, 1, 1), datetime(2021, 2, 1)\n)\n# >>> { \"data\": [ ... ] }\n```\n\n### Historical Data\n\n```python\nawait client.emissions.async_get_forecasted_emissions(\n \"<REGION>\", \"<SIGNAL_TYPE>\", datetime(2021, 1, 1), datetime(2021, 2, 1)\n)\n# >>> [ { \"point_time\": \"2019-02-21T00:15:00.000Z\", \"value\": 844, ... } ]\n```\n\n## Retry Logic\n\nBy default, `aiowatttime` will handle expired access tokens for you. When a token expires,\nthe library will attempt the following sequence 3 times:\n\n- Request a new token\n- Pause for 1 second (to be respectful of the API rate limiting)\n- Execute the original request again\n\nBoth the number of retries and the delay between retries can be configured when\ninstantiating a client:\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aiowatttime import Client\n\n\nasync def main() -> None:\n async with ClientSession() as session:\n client = await Client.async_login(\n \"user\",\n \"password\",\n session=session,\n # Make 7 retry attempts:\n request_retries=7,\n # Delay 4 seconds between attempts:\n request_retry_delay=4,\n )\n\n\nasyncio.run(main())\n```\n\nAs always, an invalid username/password combination will immediately throw an exception.\n\n## Custom Logger\n\nBy default, `aiowatttime` provides its own logger. If you should wish to use your own, you\ncan pass it to the client during instantiation:\n\n```python\nimport asyncio\nimport logging\n\nfrom aiohttp import ClientSession\n\nfrom aiowatttime import Client\n\nCUSTOM_LOGGER = logging.getLogger(\"my_custom_logger\")\n\n\nasync def main() -> None:\n async with ClientSession() as session:\n client = await Client.async_login(\n \"user\",\n \"password\",\n session=session,\n logger=logger,\n )\n\n\nasyncio.run(main())\n```\n\n# Contributing\n\nThanks to all of [our contributors][contributors] so far!\n\n1. [Check for open features/bugs][issues] or [initiate a discussion on one][new-issue].\n2. [Fork the repository][fork].\n3. (_optional, but highly recommended_) Create a virtual environment: `python3 -m venv .venv`\n4. (_optional, but highly recommended_) Enter the virtual environment: `source ./.venv/bin/activate`\n5. Install the dev environment: `script/setup`\n6. Code your new feature or bug fix on a new branch.\n7. Write tests that cover your new functionality.\n8. Run tests and ensure 100% code coverage: `poetry run pytest --cov aiowatttime tests`\n9. Update `README.md` with any new documentation.\n10. Submit a pull request!\n\n[aiohttp]: https://github.com/aio-libs/aiohttp\n[ci-badge]: https://img.shields.io/github/actions/workflow/status/bachya/aiowatttime/test.yml\n[ci]: https://github.com/bachya/aiowatttime/actions\n[codecov-badge]: https://codecov.io/gh/bachya/aiowatttime/branch/dev/graph/badge.svg\n[codecov]: https://codecov.io/gh/bachya/aiowatttime\n[contributors]: https://github.com/bachya/aiowatttime/graphs/contributors\n[fork]: https://github.com/bachya/aiowatttime/fork\n[issues]: https://github.com/bachya/aiowatttime/issues\n[license-badge]: https://img.shields.io/pypi/l/aiowatttime.svg\n[license]: https://github.com/bachya/aiowatttime/blob/main/LICENSE\n[maintainability-badge]: https://api.codeclimate.com/v1/badges/781e64940b1302ae9ac3/maintainability\n[maintainability]: https://codeclimate.com/github/bachya/aiowatttime/maintainability\n[new-issue]: https://github.com/bachya/aiowatttime/issues/new\n[new-issue]: https://github.com/bachya/aiowatttime/issues/new\n[pypi-badge]: https://img.shields.io/pypi/v/aiowatttime.svg\n[pypi]: https://pypi.python.org/pypi/aiowatttime\n[version-badge]: https://img.shields.io/pypi/pyversions/aiowatttime.svg\n[version]: https://pypi.python.org/pypi/aiowatttime\n[watttime]: https://www.watttime.org\n[watttime-data-plans]: https://www.watttime.org/get-the-data/data-plans/\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An asyncio-based Python3 library for interacting with WattTime",
"version": "2024.6.0",
"project_urls": {
"Bug Tracker": "https://github.com/bachya/aiowatttime/issues",
"Changelog": "https://github.com/bachya/aiowatttime/releases",
"Homepage": "https://github.com/bachya/aiowatttime",
"Repository": "https://github.com/bachya/aiowatttime"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bdbd44b2a74281a3f8a8e56be4f5eede16732fda66ee4109eda0d1a164841478",
"md5": "a2e539f5139a188f01529fa816db14e6",
"sha256": "efbd8628c7a8bd6eda078669ec361648a634def900942b33b0952312d9570c7c"
},
"downloads": -1,
"filename": "aiowatttime-2024.6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a2e539f5139a188f01529fa816db14e6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 9067,
"upload_time": "2024-06-18T19:30:11",
"upload_time_iso_8601": "2024-06-18T19:30:11.298056Z",
"url": "https://files.pythonhosted.org/packages/bd/bd/44b2a74281a3f8a8e56be4f5eede16732fda66ee4109eda0d1a164841478/aiowatttime-2024.6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0d4f86f9aa9116e7d7da803acbc4d8d3c95cd830afd9d87bdf07d82b0cfde022",
"md5": "c2bccd9ce54ba7ad610adf99b56e9f9d",
"sha256": "af689438b17254ea2e16f17fff39737abd57c46500302c0cf4a2844ed4d26829"
},
"downloads": -1,
"filename": "aiowatttime-2024.6.0.tar.gz",
"has_sig": false,
"md5_digest": "c2bccd9ce54ba7ad610adf99b56e9f9d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 10540,
"upload_time": "2024-06-18T19:30:12",
"upload_time_iso_8601": "2024-06-18T19:30:12.999271Z",
"url": "https://files.pythonhosted.org/packages/0d/4f/86f9aa9116e7d7da803acbc4d8d3c95cd830afd9d87bdf07d82b0cfde022/aiowatttime-2024.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-18 19:30:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bachya",
"github_project": "aiowatttime",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aiowatttime"
}