Asyncio Duke Energy
Project description
aiodukeenergy
Documentation: https://aiodukeenergy.readthedocs.io
Source Code: https://github.com/hunterjm/aiodukeenergy
Asyncio Duke Energy
Installation
Install this via pip (or your favourite package manager):
pip install aiodukeenergy
Usage
Duke Energy uses Auth0 with CAPTCHA protection which blocks automated logins. Authentication requires a browser-based OAuth flow using a Chrome extension to capture the mobile app's custom redirect URL.
Setup
-
Install the Chrome extension:
- Download the latest chrome-extension.zip from releases (or use
./chrome-extension/from source) - Extract the zip file
- Open Chrome and navigate to
chrome://extensions/ - Enable "Developer mode"
- Click "Load unpacked" and select the extracted folder
- Download the latest chrome-extension.zip from releases (or use
-
Run the browser authentication script:
cd examples python browser_auth.py
-
The script will:
- Open your browser to Duke Energy's login page
- After you log in, the extension captures the authorization code
- Exchange the code for API tokens
- Save tokens to
duke_tokens.json
Using Tokens
Once you have tokens, use them with the library:
import asyncio
import aiohttp
from aiodukeenergy import Auth0Client, DukeEnergy, DukeEnergyAuth
async def main():
# Option 1: Load from token file
import json
with open("duke_tokens.json") as f:
tokens = json.load(f)
async with aiohttp.ClientSession() as session:
auth0_client = Auth0Client(session)
auth = DukeEnergyAuth(
session,
auth0_client,
access_token=tokens["access_token"],
refresh_token=tokens.get("refresh_token"),
id_token=tokens.get("id_token"),
)
client = DukeEnergy(auth)
accounts = await client.get_accounts()
print(accounts)
meters = await client.get_meters()
for serial, meter in meters.items():
print(f"Meter: {serial} ({meter['serviceType']})")
asyncio.run(main())
Browser OAuth Flow (Programmatic)
You can also integrate the OAuth flow into your own application:
import asyncio
import webbrowser
import aiohttp
from aiodukeenergy import Auth0Client, DukeEnergy, DukeEnergyAuth
async def main():
async with aiohttp.ClientSession() as session:
auth0_client = Auth0Client(session)
auth = DukeEnergyAuth(session, auth0_client)
# Step 1: Get authorization URL with PKCE
auth_url, state, code_verifier = auth0_client.get_authorization_url()
# Step 2: Open browser for user login
webbrowser.open(auth_url)
# Step 3: Get the authorization code (captured by Chrome extension)
code = input("Enter the authorization code: ")
# Step 4: Exchange code for tokens
await auth.authenticate_with_code(code, code_verifier)
# Now you can make API calls
client = DukeEnergy(auth)
accounts = await client.get_accounts()
print(accounts)
asyncio.run(main())
Available Classes
Auth0Client- OAuth2/OIDC client for Duke Energy's Auth0get_authorization_url()- Generate browser OAuth URL with PKCE (returns url, state, code_verifier)exchange_code(code, code_verifier)- Exchange authorization code for tokensrefresh_token(refresh_token)- Refresh expired tokens
AbstractDukeEnergyAuth- Abstract base class for auth providersasync_get_id_token()- Get a valid ID token (abstract)async_get_access_token()- Get a valid access tokenrequest(method, url, **kwargs)- Make authenticated requests
DukeEnergyAuth- Concrete auth implementation using Auth0Clientauthenticate_with_code(code, code_verifier)- Complete OAuth flowtoken- Property to get current token dictionary
DukeEnergy- API client for Duke Energyget_accounts()- Get all accountsget_meters()- Get all metersget_energy_usage(meter_serial, interval, view, start_date, end_date)- Get usage data
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Credits
This package was created with Copier and the browniebroke/pypackage-template project template.
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 aiodukeenergy-1.1.0.tar.gz.
File metadata
- Download URL: aiodukeenergy-1.1.0.tar.gz
- Upload date:
- Size: 15.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 |
9eb06625119b1c9ea0ecb4401996bb82ec945da5329e3dd092ce138e98528989
|
|
| MD5 |
e95122db00a1fdc21d13e41a4b4c64f1
|
|
| BLAKE2b-256 |
e1f7610b7e0d919c68f15c10a96af703ebf7fe65d766e417a9bc1bd684077559
|
Provenance
The following attestation bundles were made for aiodukeenergy-1.1.0.tar.gz:
Publisher:
ci.yml on hunterjm/aiodukeenergy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiodukeenergy-1.1.0.tar.gz -
Subject digest:
9eb06625119b1c9ea0ecb4401996bb82ec945da5329e3dd092ce138e98528989 - Sigstore transparency entry: 1009890088
- Sigstore integration time:
-
Permalink:
hunterjm/aiodukeenergy@c1f073dceac481580235be0fe626aa3c4d8acc35 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hunterjm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@c1f073dceac481580235be0fe626aa3c4d8acc35 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiodukeenergy-1.1.0-py3-none-any.whl.
File metadata
- Download URL: aiodukeenergy-1.1.0-py3-none-any.whl
- Upload date:
- Size: 15.8 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 |
598f19cdbd156f906c77e39e78d9f6f04fc9fd9c9337a74d5250d8524786ca26
|
|
| MD5 |
e50c46b6601cb321f99538f0e31b238a
|
|
| BLAKE2b-256 |
3646d60af1f48aee58b728da4a1fd395857b5e11f52cf4e00090d9f688ad93da
|
Provenance
The following attestation bundles were made for aiodukeenergy-1.1.0-py3-none-any.whl:
Publisher:
ci.yml on hunterjm/aiodukeenergy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiodukeenergy-1.1.0-py3-none-any.whl -
Subject digest:
598f19cdbd156f906c77e39e78d9f6f04fc9fd9c9337a74d5250d8524786ca26 - Sigstore transparency entry: 1009890138
- Sigstore integration time:
-
Permalink:
hunterjm/aiodukeenergy@c1f073dceac481580235be0fe626aa3c4d8acc35 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hunterjm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@c1f073dceac481580235be0fe626aa3c4d8acc35 -
Trigger Event:
push
-
Statement type: