@@ -178,7 +178,7 @@ async def _async_update_data(
178
178
179
179
contracts : dict [int , Contract ] = {int (c .contract_id ): c for c in all_contracts if c .status == 1
180
180
and int (c .contract_id ) in self ._contract_ids }
181
-
181
+ localized_today = TIMEZONE . localize ( datetime . today ())
182
182
tariff = await self ._get_kwh_tariff ()
183
183
184
184
data = {STATICS_DICT_NAME : {
@@ -219,8 +219,8 @@ async def _async_update_data(
219
219
# For some reason, there are differences between sending 2024-03-01 and sending 2024-03-07 (Today)
220
220
# So instead of sending the 1st day of the month, just sending today date
221
221
222
- monthly_report_req_date : datetime = TIMEZONE . localize ( datetime . today () .replace (hour = 1 , minute = 0 ,
223
- second = 0 , microsecond = 0 ) )
222
+ monthly_report_req_date : datetime = localized_today .replace (hour = 1 , minute = 0 ,
223
+ second = 0 , microsecond = 0 )
224
224
225
225
devices = await self ._get_devices_by_contract_id (contract_id )
226
226
@@ -235,7 +235,7 @@ async def _async_update_data(
235
235
daily_readings [device .device_number ] = remote_reading .data
236
236
237
237
weekly_future_consumption = None
238
- if TIMEZONE . localize ( datetime . today ()) .day == 1 :
238
+ if localized_today .day == 1 :
239
239
# if today's the 1st of the month, "yesterday" is on a different month
240
240
yesterday : datetime = monthly_report_req_date - timedelta (days = 1 )
241
241
remote_reading = await self ._get_readings (contract_id , device .device_number , device .device_code ,
@@ -253,20 +253,21 @@ async def _async_update_data(
253
253
daily_readings [device .device_number ].sort (key = lambda x : x .date )
254
254
255
255
await self ._verify_daily_readings_exist (daily_readings [device .device_number ],
256
- TIMEZONE . localize ( datetime . today ()) - timedelta (days = 1 ),
256
+ localized_today - timedelta (days = 1 ),
257
257
device , contract_id )
258
258
259
259
today_reading_key = str (contract_id ) + "-" + device .device_number
260
260
today_reading = self ._today_readings .get (today_reading_key )
261
261
262
262
if not today_reading :
263
263
today_reading = await self ._get_readings (contract_id , device .device_number , device .device_code ,
264
- TIMEZONE . localize ( datetime . today ()) ,
264
+ localized_today ,
265
265
ReadingResolution .DAILY )
266
266
self ._today_readings [today_reading_key ] = today_reading
267
267
268
268
await self ._verify_daily_readings_exist (daily_readings [device .device_number ],
269
- TIMEZONE .localize (datetime .today ()), device , contract_id , today_reading )
269
+ localized_today , device , contract_id ,
270
+ today_reading )
270
271
271
272
# fallbacks for future consumption since IEC api is broken :/
272
273
if not future_consumption [device .device_number ].future_consumption :
@@ -278,7 +279,7 @@ async def _async_update_data(
278
279
future_consumption [device .device_number ] = (
279
280
self ._today_readings .get (today_reading_key ).future_consumption_info )
280
281
else :
281
- req_date = TIMEZONE . localize ( datetime . today ()) - timedelta (days = 2 )
282
+ req_date = localized_today - timedelta (days = 2 )
282
283
two_days_ago_reading = await self ._get_readings (contract_id , device .device_number ,
283
284
device .device_code ,
284
285
req_date ,
@@ -314,6 +315,7 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
314
315
_LOGGER .debug (f"Updating statistics for IEC Contract { contract_id } " )
315
316
devices = await self ._get_devices_by_contract_id (contract_id )
316
317
kwh_price = await self ._get_kwh_tariff ()
318
+ localized_today = TIMEZONE .localize (datetime .today ())
317
319
318
320
if not devices :
319
321
_LOGGER .error (f"Failed fetching devices for IEC Contract { contract_id } " )
@@ -347,16 +349,15 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
347
349
if from_date .hour == 23 :
348
350
from_date = from_date + timedelta (hours = 2 )
349
351
350
- today = TIMEZONE .localize (datetime .today ())
351
- if today .date () == from_date .date ():
352
+ if localized_today .date () == from_date .date ():
352
353
_LOGGER .debug ("The date to fetch is today or later, replacing it with Today at 01:00:00" )
353
- from_date = TIMEZONE . localize ( today . replace (hour = 1 , minute = 0 , second = 0 , microsecond = 0 ) )
354
+ from_date = localized_today . replace (hour = 1 , minute = 0 , second = 0 , microsecond = 0 )
354
355
355
356
_LOGGER .debug (f"Fetching consumption from { from_date .strftime ('%Y-%m-%d %H:%M:%S' )} " )
356
357
readings = await self ._get_readings (contract_id , device .device_number , device .device_code ,
357
358
from_date ,
358
359
ReadingResolution .DAILY )
359
- if from_date .date () == today .date ():
360
+ if from_date .date () == localized_today .date ():
360
361
self ._today_readings [str (contract_id ) + "-" + device .device_number ] = readings
361
362
362
363
if not readings or not readings .data :
0 commit comments