3
3
import itertools
4
4
import logging
5
5
import socket
6
- from datetime import datetime , timedelta
6
+ from datetime import datetime , timedelta , date
7
7
from typing import cast , Any # noqa: UP035
8
8
from collections import Counter
9
9
from uuid import UUID
@@ -68,8 +68,8 @@ def __init__(
68
68
self ._devices_by_contract_id = {}
69
69
self ._last_meter_reading = {}
70
70
self ._devices_by_meter_id = {}
71
- self ._delivery_tariff_by_pahse = {}
72
- self ._distribution_tariff_by_pahse = {}
71
+ self ._delivery_tariff_by_phase = {}
72
+ self ._distribution_tariff_by_phase = {}
73
73
self ._power_size_by_connection_size = {}
74
74
self ._kwh_tariff : float | None = None
75
75
self ._kva_tariff : float | None = None
@@ -152,21 +152,21 @@ async def _get_kva_tariff(self) -> float:
152
152
return self ._kva_tariff or 0.0
153
153
154
154
async def _get_delivery_tariff (self , phase ) -> float :
155
- delivery_tariff = self ._delivery_tariff_by_pahse .get (phase )
155
+ delivery_tariff = self ._delivery_tariff_by_phase .get (phase )
156
156
if not delivery_tariff :
157
157
try :
158
158
delivery_tariff = await self .api .get_delivery_tariff (phase )
159
- self ._delivery_tariff_by_pahse [phase ] = delivery_tariff
159
+ self ._delivery_tariff_by_phase [phase ] = delivery_tariff
160
160
except IECError as e :
161
161
_LOGGER .exception (f"Failed fetching Delivery Tariff by phase { phase } " , e )
162
162
return delivery_tariff or 0.0
163
163
164
164
async def _get_distribution_tariff (self , phase ) -> float :
165
- distribution_tariff = self ._distribution_tariff_by_pahse .get (phase )
165
+ distribution_tariff = self ._distribution_tariff_by_phase .get (phase )
166
166
if not distribution_tariff :
167
167
try :
168
168
distribution_tariff = await self .api .get_distribution_tariff (phase )
169
- self ._distribution_tariff_by_pahse [phase ] = distribution_tariff
169
+ self ._distribution_tariff_by_phase [phase ] = distribution_tariff
170
170
except IECError as e :
171
171
_LOGGER .exception (f"Failed fetching Distribution Tariff by phase { phase } " , e )
172
172
return distribution_tariff or 0.0
@@ -198,28 +198,28 @@ async def _get_power_size(self, connection_size) -> float:
198
198
_LOGGER .exception (f"Failed fetching Power Size by Connection Size { connection_size } " , e )
199
199
return power_size or 0.0
200
200
201
- async def _get_readings (self , contract_id : int , device_id : str | int , device_code : str | int , date : datetime ,
201
+ async def _get_readings (self , contract_id : int , device_id : str | int , device_code : str | int , reading_date : datetime ,
202
202
resolution : ReadingResolution ):
203
203
204
- date_key = date .strftime ("%Y" )
204
+ date_key = reading_date .strftime ("%Y" )
205
205
match resolution :
206
206
case ReadingResolution .DAILY :
207
- date_key += date .strftime ("-%m-%d" )
207
+ date_key += reading_date .strftime ("-%m-%d" )
208
208
case ReadingResolution .WEEKLY :
209
- date_key += "/" + str (date .isocalendar ().week )
209
+ date_key += "/" + str (reading_date .isocalendar ().week )
210
210
case ReadingResolution .MONTHLY :
211
- date_key += date .strftime ("-%m" )
211
+ date_key += reading_date .strftime ("-%m" )
212
212
case _:
213
213
_LOGGER .warning ("Unexpected resolution value" )
214
- date_key += date .strftime ("-%m-%d" )
214
+ date_key += reading_date .strftime ("-%m-%d" )
215
215
216
216
key = (contract_id , int (device_id ), date_key )
217
217
reading = self ._readings .get (key )
218
218
if not reading :
219
219
try :
220
220
reading = await self .api .get_remote_reading (device_id , int (device_code ),
221
- date ,
222
- date ,
221
+ reading_date ,
222
+ reading_date ,
223
223
resolution ,
224
224
str (contract_id ))
225
225
self ._readings [key ] = reading
@@ -420,8 +420,8 @@ async def _async_update_data(
420
420
_LOGGER .debug ("Failed fetching FutureConsumption, data in IEC API is corrupted" )
421
421
422
422
estimated_bill , fixed_price , consumption_price , total_days , delivery_price , distribution_price , \
423
- total_kva_price , estimated_kwh_consumption = await self ._estimate_bill (contract_id , device .device_number ,\
424
- is_private_producer , future_consumption ,\
423
+ total_kva_price , estimated_kwh_consumption = await self ._estimate_bill (contract_id , device .device_number ,
424
+ is_private_producer , future_consumption ,
425
425
kwh_tariff , kva_tariff , last_invoice )
426
426
427
427
estimated_bill_dict = {
@@ -618,8 +618,14 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
618
618
self .hass , cost_metadata , cost_statistics
619
619
)
620
620
621
- async def _estimate_bill (self , contract_id , device_number , is_private_producer ,\
622
- future_consumption , kwh_tariff , kva_tariff , last_invoice ):
621
+ async def _estimate_bill (self , contract_id , device_number , is_private_producer ,
622
+ future_consumption , kwh_tariff , kva_tariff , last_invoice ):
623
+ last_meter_read : int | None = None
624
+ last_meter_read_date : date | None = None
625
+ phase_count : int | None = None
626
+ connection_size : str | None = None
627
+ devices_by_id : Devices | None = None
628
+
623
629
if not is_private_producer :
624
630
try :
625
631
devices_by_id : Devices = await self ._get_devices_by_device_id (device_number )
@@ -629,9 +635,8 @@ async def _estimate_bill(self, contract_id, device_number, is_private_producer,\
629
635
connection_size = (devices_by_id .counter_devices [0 ].
630
636
connection_size .representative_connection_size )
631
637
except Exception as e :
632
- _LOGGER .warn ("Failed to fetch data from devices_by_id, falling back to Masa API" , e )
638
+ _LOGGER .warning ("Failed to fetch data from devices_by_id, falling back to Masa API" , e )
633
639
_LOGGER .debug (f"DevicesById Response: { devices_by_id } " )
634
- devices_by_id = None
635
640
last_meter_read = None
636
641
last_meter_read_date = None
637
642
phase_count = None
0 commit comments