Set last_reset for integrated entities in IoTaWatt (#65143)

* 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

* Update homeassistant/components/iotawatt/sensor.py
* Update homeassistant/components/iotawatt/coordinator.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Stefan Agner 2022-02-02 17:39:33 +01:00 committed by GitHub
parent 530fc8a9af
commit c8e64358b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 0 deletions

View File

@ -51,6 +51,7 @@ class IotawattUpdater(DataUpdateCoordinator):
httpx_client.get_async_client(self.hass),
self.entry.data.get(CONF_USERNAME),
self.entry.data.get(CONF_PASSWORD),
integratedInterval="d",
)
try:
is_authenticated = await api.connect()

View File

@ -8,6 +8,7 @@ import logging
from iotawattpy.sensor import Sensor
from homeassistant.components.sensor import (
ATTR_LAST_RESET,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
@ -219,6 +220,9 @@ class IotaWattSensor(update_coordinator.CoordinatorEntity, SensorEntity):
attrs = {"type": data.getType()}
if attrs["type"] == "Input":
attrs["channel"] = data.getChannel()
if (begin := data.getBegin()) and (last_reset := dt.parse_datetime(begin)):
attrs[ATTR_LAST_RESET] = last_reset.isoformat()
return attrs
@property