36
36
37
37
38
38
async def _verify_daily_readings_exist (daily_readings : list [RemoteReading ], desired_date : datetime , device : Device ,
39
- contract_id : str , api : IecClient ,
39
+ contract_id : int , api : IecClient ,
40
40
prefetched_reading : RemoteReadingResponse | None = None ):
41
41
desired_date = desired_date .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
42
42
daily_reading = next (filter (lambda x : find_reading_by_date (x , desired_date ), daily_readings ), None )
@@ -80,7 +80,7 @@ def __init__(
80
80
)
81
81
self ._config_entry = config_entry
82
82
self ._bp_number = config_entry .data .get (CONF_BP_NUMBER )
83
- self ._contracts = config_entry .data .get (CONF_SELECTED_CONTRACTS )
83
+ self ._contract_ids = config_entry .data .get (CONF_SELECTED_CONTRACTS )
84
84
self ._entry_data = config_entry .data
85
85
self ._today_readings = {}
86
86
self .api = IecClient (
@@ -127,28 +127,32 @@ async def _async_update_data(
127
127
self ._bp_number = customer .bp_number
128
128
129
129
all_contracts : list [Contract ] = await self .api .get_contracts (self ._bp_number )
130
- if not self ._contracts :
131
- self ._contracts = [contract .contract_id for contract in all_contracts if contract .status == 1 ]
130
+ if not self ._contract_ids :
131
+ self ._contract_ids = [int ( contract .contract_id ) for contract in all_contracts if contract .status == 1 ]
132
132
133
- contracts : dict [str , Contract ] = {c .contract_id : c for c in all_contracts }
133
+ contracts : dict [int , Contract ] = {int (c .contract_id ): c for c in all_contracts if c .status == 1
134
+ and int (c .contract_id ) in self ._contract_ids }
134
135
135
136
tariff = await self .api .get_kwh_tariff () / 100
136
137
data = {STATICS_DICT_NAME : {
137
138
STATIC_KWH_TARIFF : tariff ,
138
139
STATIC_BP_NUMBER : self ._bp_number
139
140
}}
140
141
141
- for contract_id in self ._contracts :
142
+ _LOGGER .debug (f"All Contract Ids: { list (contracts .keys ())} " )
143
+
144
+ for contract_id in self ._contract_ids :
142
145
# Because IEC API provides historical usage/cost with a delay of a couple of days
143
146
# we need to insert data into statistics.
147
+ _LOGGER .debug (f"Processing { contract_id } " )
144
148
await self ._insert_statistics (contract_id , contracts .get (contract_id ).smart_meter )
145
149
billing_invoices = await self .api .get_billing_invoices (self ._bp_number , contract_id )
146
150
billing_invoices .invoices .sort (key = lambda inv : inv .full_date , reverse = True )
147
151
last_invoice = billing_invoices .invoices [0 ]
148
152
149
153
future_consumption : FutureConsumptionInfo | None = None
150
154
daily_readings : list [RemoteReading ] | None = None
151
- today_reading : RemoteReadingResponse | None = None
155
+
152
156
if contracts .get (contract_id ).smart_meter :
153
157
# For some reason, there are differences between sending 2024-03-01 and sending 2024-03-07 (Today)
154
158
# So instead of sending the 1st day of the month, just sending today date
@@ -219,25 +223,27 @@ async def _async_update_data(
219
223
else :
220
224
_LOGGER .debug ("Failed fetching FutureConsumption, data in IEC API is corrupted" )
221
225
222
- data [contract_id ] = {CONTRACT_DICT_NAME : contracts .get (contract_id ),
223
- INVOICE_DICT_NAME : last_invoice ,
224
- FUTURE_CONSUMPTIONS_DICT_NAME : future_consumption ,
225
- DAILY_READINGS_DICT_NAME : daily_readings ,
226
- STATICS_DICT_NAME : {STATIC_KWH_TARIFF : tariff } # workaround
227
- }
226
+ data [str (contract_id )] = {CONTRACT_DICT_NAME : contracts .get (contract_id ),
227
+ INVOICE_DICT_NAME : last_invoice ,
228
+ FUTURE_CONSUMPTIONS_DICT_NAME : future_consumption ,
229
+ DAILY_READINGS_DICT_NAME : daily_readings ,
230
+ STATICS_DICT_NAME : {STATIC_KWH_TARIFF : tariff } # workaround
231
+ }
232
+
233
+ # Clean today reading for next reading cycle
234
+ self ._today_readings = {}
228
235
229
- # Clean today reading for next reading cycle
230
- self ._today_readings = {}
236
+ _LOGGER .debug (f"Data Keys: { list (data .keys ())} " )
231
237
return data
232
238
233
- async def _insert_statistics (self , contract_id : str , is_smart_meter : bool ) -> None :
239
+ async def _insert_statistics (self , contract_id : int , is_smart_meter : bool ) -> None :
234
240
if not is_smart_meter :
235
241
_LOGGER .info (f"IEC Contract { contract_id } doesn't contain Smart Meters, not adding statistics" )
236
242
# Support only smart meters at the moment
237
243
return
238
244
239
245
_LOGGER .debug (f"Updating statistics for IEC Contract { contract_id } " )
240
- devices = await self .api .get_devices (contract_id )
246
+ devices = await self .api .get_devices (str ( contract_id ) )
241
247
month_ago_time = (datetime .now () - timedelta (weeks = 4 ))
242
248
243
249
kwh_price = await self .api .get_kwh_tariff () / 100
@@ -258,7 +264,7 @@ async def _insert_statistics(self, contract_id: str, is_smart_meter: bool) -> No
258
264
readings = await self .api .get_remote_reading (device .device_number , int (device .device_code ),
259
265
month_ago_time ,
260
266
month_ago_time , ReadingResolution .DAILY ,
261
- contract_id )
267
+ str ( contract_id ) )
262
268
else :
263
269
last_stat_time = last_stat [consumption_statistic_id ][0 ]["start" ]
264
270
# API returns daily data, so need to increase the start date by 4 hrs to get the next day
@@ -277,7 +283,7 @@ async def _insert_statistics(self, contract_id: str, is_smart_meter: bool) -> No
277
283
_LOGGER .debug (f"Fetching consumption from { from_date .strftime ('%Y-%m-%d %H:%M:%S' )} " )
278
284
readings = await self .api .get_remote_reading (device .device_number , int (device .device_code ),
279
285
from_date , from_date ,
280
- ReadingResolution .DAILY , contract_id )
286
+ ReadingResolution .DAILY , str ( contract_id ) )
281
287
if from_date .date () == today .date ():
282
288
self ._today_readings [contract_id ] = readings
283
289
0 commit comments