Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set last_reset for integrated entities in IoTaWatt #65143

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Set last_reset for integrated entities
For entities which integrate over time (like energy in watt hours) the
iotawattpy library uses the beginning of the year as start date by
default (using relative time specification "y", see [1]). Since
PR #56974 state class has been changed from TOTAL_INCREASING to TOTAL.
However, the relative start date of "y" causes the value to reset to
zero at the beginning of the year.

This fixes it by setting last_reset properly, which takes such resets
into account.

While at it, let's set the cycle to one day. This lowers the load on
IoTaWatt (fetching with start date beginning of the day seems to
response faster than beginning of the year).

[1]: https://docs.iotawatt.com/en/master/query.html#relative-time
  • Loading branch information
agners committed Jan 28, 2022
commit a635d550317af0236716cb422154d9bdd0c1b542
1 change: 1 addition & 0 deletions homeassistant/components/iotawatt/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async def _async_update_data(self):
httpx_client.get_async_client(self.hass),
self.entry.data.get(CONF_USERNAME),
self.entry.data.get(CONF_PASSWORD),
"d",
agners marked this conversation as resolved.
Show resolved Hide resolved
)
try:
is_authenticated = await api.connect()
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/iotawatt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from iotawattpy.sensor import Sensor

from homeassistant.components.sensor import (
ATTR_LAST_RESET,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
Expand Down Expand Up @@ -219,6 +220,9 @@ def extra_state_attributes(self) -> dict[str, str]:
attrs = {"type": data.getType()}
if attrs["type"] == "Input":
attrs["channel"] = data.getChannel()
if data.getBegin() and (last_reset := dt.parse_datetime(data.getBegin())):
agners marked this conversation as resolved.
Show resolved Hide resolved
attrs[ATTR_LAST_RESET] = last_reset.isoformat()
agners marked this conversation as resolved.
Show resolved Hide resolved

return attrs

@property
Expand Down