Added support for Duke Energy smart meters#15165
Conversation
| def update(self): | ||
| """Update meter.""" | ||
| self.duke_meter.update() | ||
|
|
|
|
||
| try: | ||
| duke = DukeEnergy(config.get(CONF_USERNAME), | ||
| config.get(CONF_PASSWORD), |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| from homeassistant.const import CONF_USERNAME, CONF_PASSWORD | ||
| from homeassistant.helpers.entity import Entity | ||
| import homeassistant.helpers.config_validation as cv | ||
| import homeassistant.util as util |
There was a problem hiding this comment.
'homeassistant.util' imported but unused
|
Note: The docs seem to be missing. Added label. |
|
@frenck correct, I haven't created the .io PR yet. I'll try to do that later today. |
|
Have been using this for almost a month now with great success. Thank you w1ll1iam23 for the hard work and effort. |
| @property | ||
| def name(self): | ||
| """Return the name.""" | ||
| return "duke_energy_" + self.duke_meter.id |
There was a problem hiding this comment.
I'm betting the meter id to be unique.
Why not implement unique_id ?
There was a problem hiding this comment.
It has been awhile since I added a new platform forgot that was even a thing. I'll add it.
There was a problem hiding this comment.
Use new style string formatting instead of string concatenation. That's what @pvizeli meant. But unique id is the way to go.
| def device_state_attributes(self): | ||
| """Return the state attributes.""" | ||
| attributes = { | ||
| "lastBillsUsage": self.duke_meter.get_total(), |
There was a problem hiding this comment.
its common to move any string from the body of the class to the beginning of the file and use a constant, eg.
ATTR_LAST_BILLS_USAGE = 'lastBillsUsage'
| meters = duke.get_meters() | ||
| sensors = [] | ||
| for meter in meters: | ||
| sensors.append(DukeEnergyMeter(meter)) |
There was a problem hiding this comment.
add_devices([DukeEnergyMeter(meter) for meter in duke.get_meters()])
| sensors.append(DukeEnergyMeter(meter)) | ||
| add_devices(sensors) | ||
|
|
||
| return True |
There was a problem hiding this comment.
don't need to return anything (no one is checking)
There was a problem hiding this comment.
I thought you had to return False if setup failed? Is that not true anymore?
|
|
||
| @property | ||
| def unique_id(self): | ||
| """Return the unique ID." |
| @@ -0,0 +1,84 @@ | |||
| """ | |||
There was a problem hiding this comment.
TokenError: EOF in multi-line string
pvizeli
left a comment
There was a problem hiding this comment.
Please use formated strings...
|
@pvizeli not sure what you mean? I also just found a bug in the pydukeenergy library and I am testing the fix now. I am going to run with the new version for a day or two and then update the version. Moving this to a WIP. |
| Support for Duke Energy Gas and Electric meters. | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor/duke_energy/ |
| from pydukeenergy.api import DukeEnergy, DukeEnergyException | ||
|
|
||
| try: | ||
| duke = DukeEnergy(config.get(CONF_USERNAME), |
There was a problem hiding this comment.
Don't use dict.get for required keys.
| update_interval=500) | ||
| except DukeEnergyException: | ||
| _LOGGER.error("Failed to setup Duke Energy") | ||
| return False |
| @property | ||
| def name(self): | ||
| """Return the name.""" | ||
| return "duke_energy_" + self.duke_meter.id |
There was a problem hiding this comment.
Use new style string formatting instead of string concatenation. That's what @pvizeli meant. But unique id is the way to go.
| @property | ||
| def state(self): | ||
| """Return yesterdays usage.""" | ||
| return self.duke_meter.get_usage() |
There was a problem hiding this comment.
Is this doing I/O? No I/O in properties is allowed.
There was a problem hiding this comment.
No all of the values are stored in a dict in the library and are only updated when update is called.
| vol.Required(CONF_PASSWORD): cv.string, | ||
| }) | ||
|
|
||
| LAST_BILL_USAGE = "lastBillsUsage" |
There was a problem hiding this comment.
Please use lowercase snakecase for state attribute strings.
* Added support for Duke Energy smart meters * Fixed hound * Added function docstring * Moved strings to constants, implemented unique_id, and cleaned up setup. * Added doc string. * Fixed review issues. * Updated pydukenergy to 0.0.6 and set update interval to 2 hours * Updated requirements_all
* Added support for Duke Energy smart meters * Fixed hound * Added function docstring * Moved strings to constants, implemented unique_id, and cleaned up setup. * Added doc string. * Fixed review issues. * Updated pydukenergy to 0.0.6 and set update interval to 2 hours * Updated requirements_all
Description:
This adds support for reading previous days smart meter readings, both gas and electric. Each meter will be represented as a sensor
sensor.duke_energy_123456where 123456 is the meter ID.Each meter will also include attributes for total usage for last bill, average usage for last bill, and total number of days billed.
This will only work for users who have a smart meter installed and actively reporting data here https://www.duke-energy.com/my-account/usage-analysis
The only issue I have seen while testing this (only happened once) is that the API took really long to respond but didn't timeout or get any errors. The entity never showed up in HA. It showed up after doing another reboot. Is there something that is needed to let HA know setup can take a long time?
Couple of notes:
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5644
Example entry for
configuration.yaml(if applicable):Checklist:
tox. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
REQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.