Package: pyduke-energy
pyDuke-EnergyPython3 wrapper for the unofficial Duke Energy API.Designed to work with Home Assistant. Unlikely to ever be fully implemented. The primary goal is to expose Duke Energy Gateway usage information.The library supports access to the real-time power usage implemented via an MQTT over websockets connection, or a more traditional REST API to poll near-real-time data.IMPORTANT - Deprecation WarningDuke Energy is shutting down the Gateway pilot program at the end of June. This library will stop functioning and will be deprecated.Before You BeginDisclaimerThe API is unofficial, not documented, and probably not supposed to be used by third-parties. It is only accessible as it powers the Duke Energy phone app and we can mimic the app requests.In addition, the Gateway functionality is part of a pilot program. There is no guarantee on availability going forward.With that said, please see this disclaimer:This library could stop working at any time. Some functionality may be removed from the API entirely. Duke Energy may even request we stop hitting this API.Along those same lines,please do not abuse their API.Gateway RequirementIn order to call the gateway related functions, including getting energy usage, you will need to have a Duke Energy Gateway. This is currently available via a pilot program.It is possible to queryyesterday'senergy usage without a gateway; however, I have not implemented that as I did not find it useful.LimitationsSome limitations I've identified:Non-real-time Energy usage data is down to the minute, but is only updated once every 15 minutes. Meaning, the last 15 minutes of minute-by-minute data will all arrive at once after 15 minutes. My best guess is that the gateway only sends data to Duke Energy every 15 minutes. This is a limitation in the app as well.UsageInstallationThe latest version is available on PyPi.pipinstallpyduke-energyExamplesVarious usage examples can be found in theexamples/folder of this repo.Example REST API UsageThe quick example below shows how to retrieve non-real-time usage info using the traditional REST API.asyncwithaiohttp.ClientSession()asclient:duke_energy=DukeEnergyClient(email,password,client)awaitduke_energy.select_default_meter()# NB: can also use MeterInfo from API with select_meter()usage=awaitduke_energy.get_gateway_usage(datetime.date(2021,1,1),datetime.date(2021,1,2))A more detailed example is inexamples/example_rest.py.Example Real-time UsageReal-time usage can be retrieved using an MQTT connection. The quick example below shows how to do this using the default real-time client.asyncwithaiohttp.ClientSession()asclient:duke_energy=DukeEnergyClient(email,password,client)duke_rt=DukeEnergyRealtime(duke_energy)awaitduke_rt.select_default_meter()awaitduke_rt.connect_and_subscribe_forever()More detailed examples can be found inexamples/example_realtime.pyandexamples/example_realtime_kafka.py.Running ExamplesIf you want to run the examples, you will need to install extra dependencies.pipinstall.[example]pythonexamples/example_rest.pyDevelopmentEnvironment SetupDev ContainerThe preferred method of development is using the Visual Studio Code devcontainer, which will handle setting up your environment including all dependencies.Open repo in VS Code (e.g.code .from repo root)Re-open in container when the pop-up appears in VS CodeWait for the container to buildDone!Manual InstallationYou can also develop outside of a dev container if desired. The example below uses avirtualenvwhich is optional but recommended.virtualenvvenvsourcevenv/bin/activate
pipinstall-e.[example,tests]TestingThe test command will run the test suite and also run linting against all source files.toxTo run just the tests:tox-epy310To run just the linting:tox-elintAndroid App DebuggingIf you want to debug requests via the Android app, the following general approach works well for me.InstallHTTP Toolkiton your computer.Set up Android Studio so that you can use an emulator through it.Start Android Studio emulator and use HTTP Toolkit to connect to it via ADB. Make sure to set up the HTTPS request intercepting.Install Duke Energy APK on the emulator. You can download anxapkfile from a site like APKPure. To install anxapkfile on an emulator, extract thexapkfile using something like WinRAR and drag the insideapkfile to the emulator.Start Duke Energy app on the emulator and logs should now be requested in your HTTP Toolkit app.