27
27
28
28
from .commons import find_reading_by_date
29
29
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
31
31
32
32
_LOGGER = logging .getLogger (__name__ )
33
33
TIMEZONE = pytz .timezone ("Asia/Jerusalem" )
@@ -138,7 +138,7 @@ async def _async_update_data(
138
138
139
139
future_consumption : FutureConsumptionInfo | None = None
140
140
daily_readings : list [RemoteReading ] | None = None
141
- today_reading : RemoteReadingResponse | None = None
141
+ today_reading : RemoteReadingResponse | None = None
142
142
if self .is_smart_meter :
143
143
# For some reason, there are differences between sending 2024-03-01 and sending 2024-03-07 (Today)
144
144
# So instead of sending the 1st day of the month, just sending today date
@@ -184,7 +184,8 @@ async def _async_update_data(
184
184
ReadingResolution .DAILY , self ._contract_id )
185
185
self ._today_reading = today_reading
186
186
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 ,
188
189
today_reading )
189
190
190
191
# fallbacks for future consumption since IEC api is broken :/
@@ -230,9 +231,12 @@ async def _insert_statistics(self) -> None:
230
231
devices = await self .api .get_devices (self ._contract_id )
231
232
month_ago_time = (datetime .now () - timedelta (weeks = 4 ))
232
233
234
+ kwh_price = await self .api .get_kwh_tariff () / 100
235
+
233
236
for device in devices :
234
237
id_prefix = f"iec_meter_{ device .device_number } "
235
238
consumption_statistic_id = f"{ DOMAIN } :{ id_prefix } _energy_consumption"
239
+ cost_statistic_id = f"{ DOMAIN } :{ id_prefix } _energy_est_cost"
236
240
237
241
last_stat = await get_instance (self .hass ).async_add_executor_job (
238
242
get_last_statistics , self .hass , 1 , consumption_statistic_id , True , set ()
@@ -276,7 +280,7 @@ async def _insert_statistics(self) -> None:
276
280
self .hass ,
277
281
readings .data [0 ].date - timedelta (hours = 1 ),
278
282
None ,
279
- {consumption_statistic_id },
283
+ {cost_statistic_id , consumption_statistic_id },
280
284
"hour" ,
281
285
None ,
282
286
{"sum" },
@@ -288,6 +292,15 @@ async def _insert_statistics(self) -> None:
288
292
else :
289
293
consumption_sum = cast (float , stats [consumption_statistic_id ][0 ]["sum" ])
290
294
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
+
291
304
new_readings : list [RemoteReading ] = filter (lambda reading :
292
305
reading .date >= TIMEZONE .localize (
293
306
datetime .fromtimestamp (last_stat_time )),
@@ -308,9 +321,20 @@ async def _insert_statistics(self) -> None:
308
321
unit_of_measurement = UnitOfEnergy .KILO_WATT_HOUR
309
322
)
310
323
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
+
311
333
consumption_statistics = []
334
+ cost_statistics = []
312
335
for key , value in readings_by_hour .items ():
313
336
consumption_sum += value
337
+ cost_sum += value * kwh_price
314
338
consumption_statistics .append (
315
339
StatisticData (
316
340
start = key ,
@@ -319,6 +343,18 @@ async def _insert_statistics(self) -> None:
319
343
)
320
344
)
321
345
346
+ cost_statistics .append (
347
+ StatisticData (
348
+ start = key ,
349
+ sum = cost_sum ,
350
+ state = value * kwh_price
351
+ )
352
+ )
353
+
322
354
async_add_external_statistics (
323
355
self .hass , consumption_metadata , consumption_statistics
324
356
)
357
+
358
+ async_add_external_statistics (
359
+ self .hass , cost_metadata , cost_statistics
360
+ )
0 commit comments