From c8e64358b1a4016ed114724decb16ca4b26db0f3 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 2 Feb 2022 17:39:33 +0100 Subject: [PATCH] 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 --- homeassistant/components/iotawatt/coordinator.py | 1 + homeassistant/components/iotawatt/sensor.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/homeassistant/components/iotawatt/coordinator.py b/homeassistant/components/iotawatt/coordinator.py index ada9c9fb346..46a0ac81d90 100644 --- a/homeassistant/components/iotawatt/coordinator.py +++ b/homeassistant/components/iotawatt/coordinator.py @@ -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() diff --git a/homeassistant/components/iotawatt/sensor.py b/homeassistant/components/iotawatt/sensor.py index 62f65741566..acc4577fa8a 100644 --- a/homeassistant/components/iotawatt/sensor.py +++ b/homeassistant/components/iotawatt/sensor.py @@ -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