Skip to content

Commit 0cb8a28

Browse files
authored
fix: get last reading from monthly report (#43)
* fix: get last reading from monthly report * fix: instead of jumping the next day, try to add 4 hrs to counter * fix: lint * fix: remove `today_reading` * fix: logs to debug * fix: some debug logs * chore: update manifest
1 parent ade900a commit 0cb8a28

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

custom_components/iec/const.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
INVOICE_DICT_NAME = "invoice"
1616
DAILY_READINGS_DICT_NAME = "daily_readings"
1717
FUTURE_CONSUMPTIONS_DICT_NAME = "future_consumption"
18-
TODAY_READING_DICT_NAME = "today_reading"
1918
STATIC_KWH_TARIFF = "kwh_tariff"
2019
STATIC_CONTRACT = "contract_number"
2120
STATIC_BP_NUMBER = "bp_number"

custom_components/iec/coordinator.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from .commons import find_reading_by_date
2929
from .const import DOMAIN, CONF_USER_ID, STATICS_DICT_NAME, STATIC_KWH_TARIFF, INVOICE_DICT_NAME, \
30-
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, STATIC_BP_NUMBER, TODAY_READING_DICT_NAME
30+
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, STATIC_BP_NUMBER
3131

3232
_LOGGER = logging.getLogger(__name__)
3333
TIMEZONE = pytz.timezone("Asia/Jerusalem")
@@ -184,8 +184,7 @@ async def _async_update_data(
184184

185185
data = {STATICS_DICT_NAME: static_data, INVOICE_DICT_NAME: last_invoice,
186186
FUTURE_CONSUMPTIONS_DICT_NAME: future_consumption,
187-
DAILY_READINGS_DICT_NAME: daily_readings,
188-
TODAY_READING_DICT_NAME: today_reading}
187+
DAILY_READINGS_DICT_NAME: daily_readings}
189188

190189
# Clean today reading for next reading cycle
191190
self._today_reading = None
@@ -197,7 +196,7 @@ async def _insert_statistics(self) -> None:
197196
# Support only smart meters at the moment
198197
return
199198

200-
_LOGGER.info(f"Updating statistics for IEC Contract {self._contract_id}")
199+
_LOGGER.debug(f"Updating statistics for IEC Contract {self._contract_id}")
201200
devices = await self.api.get_devices(self._contract_id)
202201
month_ago_time = (datetime.now() - timedelta(weeks=4))
203202

@@ -219,10 +218,17 @@ async def _insert_statistics(self) -> None:
219218
self._contract_id)
220219
else:
221220
last_stat_time = last_stat[consumption_statistic_id][0]["start"]
222-
# API returns daily data, so need to increase the start date by 1 day to get the next day
223-
from_date = datetime.fromtimestamp(last_stat_time) + timedelta(days=1)
221+
# API returns daily data, so need to increase the start date by 4 hrs to get the next day
222+
from_date = datetime.fromtimestamp(last_stat_time)
223+
_LOGGER.debug(f"Last statistics are from {from_date.strftime('%Y-%m-%d %H:%M:%S')}")
224+
225+
if from_date.hour == 23:
226+
from_date = from_date + timedelta(hours=2)
227+
228+
_LOGGER.debug(f"Calculated from_date = {from_date.strftime('%Y-%m-%d %H:%M:%S')}")
224229
if (datetime.today() - from_date).days <= 0:
225-
from_date = TIMEZONE.localize(datetime.today())
230+
_LOGGER.debug("The date to fetch is today or later, replacing it with Today at 01:00:00")
231+
from_date = TIMEZONE.localize(datetime.today().replace(hour=1, minute=0, second=0, microsecond=0))
226232

227233
_LOGGER.debug(f"Fetching consumption from {from_date.strftime('%Y-%m-%d %H:%M:%S')}")
228234
readings = await self.api.get_remote_reading(device.device_number, int(device.device_code),

custom_components/iec/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"iot_class": "cloud_polling",
1111
"issue_tracker": "https://github.com/ludeeus/iec-custom-component/issues",
1212
"requirements": ["iec-api==0.2.8"],
13-
"version": "0.0.14"
13+
"version": "0.0.15"
1414
}

custom_components/iec/sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from .commons import find_reading_by_date
2525
from .const import DOMAIN, ILS, STATICS_DICT_NAME, STATIC_KWH_TARIFF, FUTURE_CONSUMPTIONS_DICT_NAME, INVOICE_DICT_NAME, \
26-
ILS_PER_KWH, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, EMPTY_REMOTE_READING, TODAY_READING_DICT_NAME
26+
ILS_PER_KWH, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, EMPTY_REMOTE_READING
2727
from .coordinator import IecApiCoordinator
2828

2929
_LOGGER = logging.getLogger(__name__)
@@ -122,7 +122,7 @@ def _get_reading_by_date(readings: list[RemoteReading] | None, desired_date: dat
122122
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
123123
state_class=SensorStateClass.TOTAL_INCREASING,
124124
suggested_display_precision=3,
125-
value_fn=lambda data: data[TODAY_READING_DICT_NAME].future_consumption_info.total_import
125+
value_fn=lambda data: data[FUTURE_CONSUMPTIONS_DICT_NAME].total_import
126126
),
127127
)
128128

0 commit comments

Comments
 (0)