Skip to content

Commit 32c13c6

Browse files
authored
feat: add Cost Estimation Long Time statistics (#49)
* feat: add Cost Estimation Long Time statistics * feat: add Cost Estimation Long Time statistics
1 parent 252c980 commit 32c13c6

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

custom_components/iec/coordinator.py

Lines changed: 40 additions & 4 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
30+
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, STATIC_BP_NUMBER, ILS
3131

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

139139
future_consumption: FutureConsumptionInfo | None = None
140140
daily_readings: list[RemoteReading] | None = None
141-
today_reading: RemoteReadingResponse| None = None
141+
today_reading: RemoteReadingResponse | None = None
142142
if self.is_smart_meter:
143143
# For some reason, there are differences between sending 2024-03-01 and sending 2024-03-07 (Today)
144144
# So instead of sending the 1st day of the month, just sending today date
@@ -184,7 +184,8 @@ async def _async_update_data(
184184
ReadingResolution.DAILY, self._contract_id)
185185
self._today_reading = today_reading
186186

187-
await _verify_daily_readings_exist(daily_readings, datetime.today(), device, self._contract_id, self.api,
187+
await _verify_daily_readings_exist(daily_readings, datetime.today(), device, self._contract_id,
188+
self.api,
188189
today_reading)
189190

190191
# fallbacks for future consumption since IEC api is broken :/
@@ -230,9 +231,12 @@ async def _insert_statistics(self) -> None:
230231
devices = await self.api.get_devices(self._contract_id)
231232
month_ago_time = (datetime.now() - timedelta(weeks=4))
232233

234+
kwh_price = await self.api.get_kwh_tariff() / 100
235+
233236
for device in devices:
234237
id_prefix = f"iec_meter_{device.device_number}"
235238
consumption_statistic_id = f"{DOMAIN}:{id_prefix}_energy_consumption"
239+
cost_statistic_id = f"{DOMAIN}:{id_prefix}_energy_est_cost"
236240

237241
last_stat = await get_instance(self.hass).async_add_executor_job(
238242
get_last_statistics, self.hass, 1, consumption_statistic_id, True, set()
@@ -276,7 +280,7 @@ async def _insert_statistics(self) -> None:
276280
self.hass,
277281
readings.data[0].date - timedelta(hours=1),
278282
None,
279-
{consumption_statistic_id},
283+
{cost_statistic_id, consumption_statistic_id},
280284
"hour",
281285
None,
282286
{"sum"},
@@ -288,6 +292,15 @@ async def _insert_statistics(self) -> None:
288292
else:
289293
consumption_sum = cast(float, stats[consumption_statistic_id][0]["sum"])
290294

295+
if not stats.get(cost_statistic_id):
296+
if not stats.get(consumption_statistic_id):
297+
_LOGGER.debug("No recent cost data")
298+
cost_sum = 0.0
299+
else:
300+
cost_sum = cast(float, stats[consumption_statistic_id][0]["sum"]) * kwh_price
301+
else:
302+
cost_sum = cast(float, stats[cost_statistic_id][0]["sum"])
303+
291304
new_readings: list[RemoteReading] = filter(lambda reading:
292305
reading.date >= TIMEZONE.localize(
293306
datetime.fromtimestamp(last_stat_time)),
@@ -308,9 +321,20 @@ async def _insert_statistics(self) -> None:
308321
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR
309322
)
310323

324+
cost_metadata = StatisticMetaData(
325+
has_mean=False,
326+
has_sum=True,
327+
name=f"IEC Meter {device.device_number} Estimated Cost",
328+
source=DOMAIN,
329+
statistic_id=cost_statistic_id,
330+
unit_of_measurement=ILS
331+
)
332+
311333
consumption_statistics = []
334+
cost_statistics = []
312335
for key, value in readings_by_hour.items():
313336
consumption_sum += value
337+
cost_sum += value * kwh_price
314338
consumption_statistics.append(
315339
StatisticData(
316340
start=key,
@@ -319,6 +343,18 @@ async def _insert_statistics(self) -> None:
319343
)
320344
)
321345

346+
cost_statistics.append(
347+
StatisticData(
348+
start=key,
349+
sum=cost_sum,
350+
state=value * kwh_price
351+
)
352+
)
353+
322354
async_add_external_statistics(
323355
self.hass, consumption_metadata, consumption_statistics
324356
)
357+
358+
async_add_external_statistics(
359+
self.hass, cost_metadata, cost_statistics
360+
)

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/guykh/iec-custom-component/issues",
1212
"requirements": ["iec-api==0.2.8"],
13-
"version": "0.0.16"
13+
"version": "0.0.17"
1414
}

0 commit comments

Comments
 (0)