Debian Codegraph @

README.md

raw
1 # aiodukeenergy
2
3 <p align="center">
4 <a href="https://github.com/hunterjm/aiodukeenergy/actions/workflows/ci.yml?query=branch%3Amain">
5 <img src="https://img.shields.io/github/actions/workflow/status/hunterjm/aiodukeenergy/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >
6 </a>
7 <a href="https://aiodukeenergy.readthedocs.io">
8 <img src="https://img.shields.io/readthedocs/aiodukeenergy.svg?logo=read-the-docs&logoColor=fff&style=flat-square" alt="Documentation Status">
9 </a>
10 <a href="https://codecov.io/gh/hunterjm/aiodukeenergy">
11 <img src="https://img.shields.io/codecov/c/github/hunterjm/aiodukeenergy.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">
12 </a>
13 </p>
14 <p align="center">
15 <a href="https://python-poetry.org/">
16 <img src="https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json" alt="Poetry">
17 </a>
18 <a href="https://github.com/astral-sh/ruff">
19 <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff">
20 </a>
21 <a href="https://github.com/pre-commit/pre-commit">
22 <img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit">
23 </a>
24 </p>
25 <p align="center">
26 <a href="https://pypi.org/project/aiodukeenergy/">
27 <img src="https://img.shields.io/pypi/v/aiodukeenergy.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">
28 </a>
29 <img src="https://img.shields.io/pypi/pyversions/aiodukeenergy.svg?style=flat-square&logo=python&amp;logoColor=fff" alt="Supported Python versions">
30 <img src="https://img.shields.io/pypi/l/aiodukeenergy.svg?style=flat-square" alt="License">
31 </p>
32
33 ---
34
35 **Documentation**: <a href="https://aiodukeenergy.readthedocs.io" target="_blank">https://aiodukeenergy.readthedocs.io </a>
36
37 **Source Code**: <a href="https://github.com/hunterjm/aiodukeenergy" target="_blank">https://github.com/hunterjm/aiodukeenergy </a>
38
39 ---
40
41 Asyncio Duke Energy
42
43 ## Installation
44
45 Install this via pip (or your favourite package manager):
46
47 `pip install aiodukeenergy`
48
49 ## Usage
50
51 Duke Energy uses Auth0 with CAPTCHA protection which blocks automated logins.
52 Authentication requires a browser-based OAuth flow using a Chrome extension to
53 capture the mobile app's custom redirect URL.
54
55 ### Setup
56
57 1. Install the Chrome extension:
58
59 - Download the [latest chrome-extension.zip](https://github.com/hunterjm/aiodukeenergy/releases/latest/download/chrome-extension.zip) from releases (or use `./chrome-extension/` from source)
60 - Extract the zip file
61 - Open Chrome and navigate to `chrome://extensions/`
62 - Enable "Developer mode"
63 - Click "Load unpacked" and select the extracted folder
64
65 2. Run the browser authentication script:
66
67 ```bash
68 cd examples
69 python browser_auth.py
70 ```
71
72 3. The script will:
73 - Open your browser to Duke Energy's login page
74 - After you log in, the extension captures the authorization code
75 - Exchange the code for API tokens
76 - Save tokens to `duke_tokens.json`
77
78 ### Using Tokens
79
80 Once you have tokens, use them with the library:
81
82 ```python
83 import asyncio
84 import aiohttp
85 from aiodukeenergy import Auth0Client, DukeEnergy, DukeEnergyAuth
86
87
88 async def main():
89 # Option 1: Load from token file
90 import json
91
92 with open("duke_tokens.json") as f:
93 tokens = json.load(f)
94
95 async with aiohttp.ClientSession() as session:
96 auth0_client = Auth0Client(session)
97 auth = DukeEnergyAuth(
98 session,
99 auth0_client,
100 access_token=tokens["access_token"],
101 refresh_token=tokens.get("refresh_token"),
102 id_token=tokens.get("id_token"),
103 )
104
105 client = DukeEnergy(auth)
106
107 accounts = await client.get_accounts()
108 print(accounts)
109
110 meters = await client.get_meters()
111 for serial, meter in meters.items():
112 print(f"Meter: {serial} ({meter['serviceType']})")
113
114
115 asyncio.run(main())
116 ```
117
118 ### Browser OAuth Flow (Programmatic)
119
120 You can also integrate the OAuth flow into your own application:
121
122 ```python
123 import asyncio
124 import webbrowser
125 import aiohttp
126 from aiodukeenergy import Auth0Client, DukeEnergy, DukeEnergyAuth
127
128
129 async def main():
130 async with aiohttp.ClientSession() as session:
131 auth0_client = Auth0Client(session)
132 auth = DukeEnergyAuth(session, auth0_client)
133
134 # Step 1: Get authorization URL with PKCE
135 auth_url, state, code_verifier = auth0_client.get_authorization_url()
136
137 # Step 2: Open browser for user login
138 webbrowser.open(auth_url)
139
140 # Step 3: Get the authorization code (captured by Chrome extension)
141 code = input("Enter the authorization code: ")
142
143 # Step 4: Exchange code for tokens
144 await auth.authenticate_with_code(code, code_verifier)
145
146 # Now you can make API calls
147 client = DukeEnergy(auth)
148 accounts = await client.get_accounts()
149 print(accounts)
150
151
152 asyncio.run(main())
153 ```
154
155 ### Available Classes
156
157 - `Auth0Client` - OAuth2/OIDC client for Duke Energy's Auth0
158 - `get_authorization_url()` - Generate browser OAuth URL with PKCE (returns url, state, code_verifier)
159 - `exchange_code(code, code_verifier)` - Exchange authorization code for tokens
160 - `refresh_token(refresh_token)` - Refresh expired tokens
161 - `AbstractDukeEnergyAuth` - Abstract base class for auth providers
162 - `async_get_id_token()` - Get a valid ID token (abstract)
163 - `async_get_access_token()` - Get a valid access token
164 - `request(method, url, **kwargs)` - Make authenticated requests
165 - `DukeEnergyAuth` - Concrete auth implementation using Auth0Client
166 - `authenticate_with_code(code, code_verifier)` - Complete OAuth flow
167 - `token` - Property to get current token dictionary
168 - `DukeEnergy` - API client for Duke Energy
169 - `get_accounts()` - Get all accounts
170 - `get_meters()` - Get all meters
171 - `get_energy_usage(meter_serial, interval, view, start_date, end_date)` - Get usage data
172
173 ## Contributors ✨
174
175 Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
176
177 <!-- prettier-ignore-start -->
178 <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
179 <!-- markdownlint-disable -->
180 <!-- markdownlint-enable -->
181 <!-- ALL-CONTRIBUTORS-LIST:END -->
182 <!-- prettier-ignore-end -->
183
184 This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
185
186 ## Credits
187
188 This package was created with
189 [Copier](https://copier.readthedocs.io/) and the
190 [browniebroke/pypackage-template](https://github.com/browniebroke/pypackage-template)
191 project template.