Skip to content

Commit b79ab31

Browse files
authored
fix: attempt on fixing negative values (#87)
* fix: attempt on fixing negative values * fix: fix change of day * fix: fetch last statistics from last_stat_time * fix: fix increment on every fetch * fix: Issue with remote_reading * feat: Group entities by Contract and Meter Devices (#89) * feat: Group entities by Contract and Meter Devices * fix: Fix and Lint * fix: Issue with coordinator on 1st on the month (#90) * chore: bump to 0.0.24 * fix: reduce number of calls to RemoteReadings by unique date (and not datetime) * fix: empty Devices list * chore: bump requirement to iec-api 0.2.13 * Update manifest.json
1 parent 6cd261e commit b79ab31

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

custom_components/iec/coordinator.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ async def _get_kwh_tariff(self) -> float:
9797

9898
async def _get_readings(self, contract_id: int, device_id: str | int, device_code: str | int, date: datetime,
9999
resolution: ReadingResolution):
100-
key = (contract_id, int(device_id), int(device_code), date, resolution)
100+
date_key = date.date()
101+
key = (contract_id, int(device_id), int(device_code), date_key, resolution)
101102
reading = self._readings.get(key)
102103
if not reading:
103104
try:
@@ -314,6 +315,10 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
314315
devices = await self._get_devices_by_contract_id(contract_id)
315316
kwh_price = await self._get_kwh_tariff()
316317

318+
if not devices:
319+
_LOGGER.error(f"Failed fetching devices for IEC Contract {contract_id}")
320+
return
321+
317322
for device in devices:
318323
id_prefix = f"iec_meter_{device.device_number}"
319324
consumption_statistic_id = f"{DOMAIN}:{id_prefix}_energy_consumption"
@@ -358,10 +363,14 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
358363
_LOGGER.debug("No recent usage data. Skipping update")
359364
continue
360365

366+
last_stat_hour = datetime.fromtimestamp(last_stat_time) if last_stat_time else readings.data[0].date
367+
last_stat_req_hour = last_stat_hour if last_stat_hour.hour > 0 else (last_stat_hour - timedelta(hours=1))
368+
369+
_LOGGER.debug(f"Fetching LongTerm Statistics since {last_stat_req_hour}")
361370
stats = await get_instance(self.hass).async_add_executor_job(
362371
statistics_during_period,
363372
self.hass,
364-
readings.data[0].date - timedelta(hours=1),
373+
last_stat_req_hour,
365374
None,
366375
{cost_statistic_id, consumption_statistic_id},
367376
"hour",
@@ -395,8 +404,16 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
395404
grouped_new_readings_by_hour = itertools.groupby(new_readings,
396405
key=lambda reading: reading.date
397406
.replace(minute=0, second=0, microsecond=0))
398-
readings_by_hour: dict[datetime, float] = {key: sum(reading.value for reading in list(group))
399-
for key, group in grouped_new_readings_by_hour}
407+
readings_by_hour: dict[datetime, float] = {}
408+
for key, group in grouped_new_readings_by_hour:
409+
group_list = list(group)
410+
if len(group_list) < 4:
411+
_LOGGER.debug(f"LongTerm Statistics - Skipping {key} since it's partial for the hour")
412+
continue
413+
if key <= TIMEZONE.localize(last_stat_req_hour):
414+
_LOGGER.debug(f"LongTerm Statistics -Skipping {key} data since it's already reported")
415+
continue
416+
readings_by_hour[key] = sum(reading.value for reading in group_list)
400417

401418
consumption_metadata = StatisticMetaData(
402419
has_mean=False,

custom_components/iec/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"iot_class": "cloud_polling",
1111
"issue_tracker": "https://github.com/guykh/iec-custom-component/issues",
1212
"loggers": ["iec_api"],
13-
"requirements": ["iec-api==0.2.12"],
14-
"version": "0.0.24"
13+
"requirements": ["iec-api==0.2.13"],
14+
"version": "0.0.25-beta1"
1515
}

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
colorlog>=6.8.2
22
homeassistant==2024.2.0
3-
iec-api==0.2.12
3+
iec-api==0.2.13
44
pip>=21.0
55
ruff>=0.2.2

0 commit comments

Comments
 (0)