@@ -419,36 +419,10 @@ async def _async_update_data(
419
419
else :
420
420
_LOGGER .debug ("Failed fetching FutureConsumption, data in IEC API is corrupted" )
421
421
422
- if is_private_producer :
423
- last_meter_reading = await self ._get_last_meter_reading (self ._bp_number , contract_id ,
424
- device .device_number )
425
- last_meter_read = last_meter_reading .reading
426
- last_meter_read_date = last_meter_reading .reading_date .date ()
427
-
428
- account_id = await self ._get_account_id ()
429
- connection_size = await self ._get_connection_size (account_id )
430
- phase_count_str = connection_size .split ("X" )[0 ] \
431
- if connection_size .find ("X" ) != - 1 else "1"
432
- phase_count = int (phase_count_str )
433
-
434
- else :
435
- devices_by_id : Devices = await self ._get_devices_by_device_id (device .device_number )
436
- last_meter_read = int (devices_by_id .counter_devices [0 ].last_mr )
437
- last_meter_read_date = devices_by_id .counter_devices [0 ].last_mr_date
438
- phase_count = devices_by_id .counter_devices [0 ].connection_size .phase
439
- connection_size = (devices_by_id .counter_devices [0 ].
440
- connection_size .representative_connection_size )
441
-
442
- distribution_tariff = await self ._get_distribution_tariff (phase_count )
443
- delivery_tariff = await self ._get_delivery_tariff (phase_count )
444
- power_size = await self ._get_power_size (connection_size )
445
-
446
422
estimated_bill , fixed_price , consumption_price , total_days , delivery_price , distribution_price , \
447
- total_kva_price , estimated_kwh_consumption = (
448
- self ._calculate_estimated_bill (device .device_number , future_consumption ,
449
- last_meter_read , last_meter_read_date ,
450
- kwh_tariff , kva_tariff , distribution_tariff ,
451
- delivery_tariff , power_size , last_invoice ))
423
+ total_kva_price , estimated_kwh_consumption = await self ._estimate_bill (contract_id , device .device_number ,\
424
+ is_private_producer , future_consumption ,\
425
+ kwh_tariff , kva_tariff , last_invoice )
452
426
453
427
estimated_bill_dict = {
454
428
TOTAL_EST_BILL_ATTR_NAME : estimated_bill ,
@@ -644,6 +618,46 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
644
618
self .hass , cost_metadata , cost_statistics
645
619
)
646
620
621
+ async def _estimate_bill (self , contract_id , device_number , is_private_producer ,\
622
+ future_consumption , kwh_tariff , kva_tariff , last_invoice ):
623
+ if not is_private_producer :
624
+ try :
625
+ devices_by_id : Devices = await self ._get_devices_by_device_id (device_number )
626
+ last_meter_read = int (devices_by_id .counter_devices [0 ].last_mr )
627
+ last_meter_read_date = devices_by_id .counter_devices [0 ].last_mr_date
628
+ phase_count = devices_by_id .counter_devices [0 ].connection_size .phase
629
+ connection_size = (devices_by_id .counter_devices [0 ].
630
+ connection_size .representative_connection_size )
631
+ except Exception as e :
632
+ _LOGGER .warn ("Failed to fetch data from devices_by_id, falling back to Masa API" , e )
633
+ _LOGGER .debug (f"DevicesById Response: { devices_by_id } " )
634
+ devices_by_id = None
635
+ last_meter_read = None
636
+ last_meter_read_date = None
637
+ phase_count = None
638
+ connection_size = None
639
+
640
+ if not is_private_producer or not last_meter_read :
641
+ last_meter_reading = await self ._get_last_meter_reading (self ._bp_number , contract_id ,
642
+ device_number )
643
+ last_meter_read = last_meter_reading .reading
644
+ last_meter_read_date = last_meter_reading .reading_date .date ()
645
+
646
+ account_id = await self ._get_account_id ()
647
+ connection_size = await self ._get_connection_size (account_id )
648
+ phase_count_str = connection_size .split ("X" )[0 ] \
649
+ if connection_size .find ("X" ) != - 1 else "1"
650
+ phase_count = int (phase_count_str )
651
+
652
+ distribution_tariff = await self ._get_distribution_tariff (phase_count )
653
+ delivery_tariff = await self ._get_delivery_tariff (phase_count )
654
+ power_size = await self ._get_power_size (connection_size )
655
+
656
+ return self ._calculate_estimated_bill (device_number , future_consumption ,
657
+ last_meter_read , last_meter_read_date ,
658
+ kwh_tariff , kva_tariff , distribution_tariff ,
659
+ delivery_tariff , power_size , last_invoice )
660
+
647
661
@staticmethod
648
662
def _calculate_estimated_bill (meter_id , future_consumptions : dict [str , FutureConsumptionInfo | None ],
649
663
last_meter_read , last_meter_read_date , kwh_tariff ,
0 commit comments