@@ -292,12 +292,11 @@ async def _get_readings(
292
292
async def _verify_daily_readings_exist (
293
293
self ,
294
294
daily_readings : list [RemoteReading ],
295
- desired_date : datetime ,
295
+ desired_date : date ,
296
296
device : Device ,
297
297
contract_id : int ,
298
298
prefetched_reading : RemoteReadingResponse | None = None ,
299
299
):
300
- desired_date = desired_date .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
301
300
daily_reading = next (
302
301
filter (lambda x : find_reading_by_date (x , desired_date ), daily_readings ),
303
302
None ,
@@ -312,29 +311,38 @@ async def _verify_daily_readings_exist(
312
311
contract_id ,
313
312
device .device_number ,
314
313
device .device_code ,
315
- desired_date ,
314
+ datetime . fromordinal ( desired_date . toordinal ()) ,
316
315
ReadingResolution .MONTHLY ,
317
316
)
318
317
else :
319
318
_LOGGER .debug (
320
319
f'Daily reading for date: { desired_date .strftime ("%Y-%m-%d" )} - using existing prefetched readings'
321
320
)
322
321
323
- desired_date_reading = next (
324
- filter (
325
- lambda reading : reading .date .date () == desired_date .date (),
326
- readings .data ,
327
- ),
328
- 0 ,
329
- )
330
- if desired_date_reading == 0 or desired_date_reading .value <= 0 :
331
- _LOGGER .debug (
332
- f'Couldn\' t find daily reading for: { desired_date .strftime ("%Y-%m-%d" )} '
333
- )
334
- else :
335
- daily_readings .append (
336
- RemoteReading (0 , desired_date , desired_date_reading .value )
322
+ if readings and readings .data :
323
+ daily_readings [device .device_number ] += readings .data
324
+
325
+ # Remove duplicates
326
+ daily_readings [device .device_number ] = list (dict .fromkeys (daily_readings [device .device_number ]))
327
+
328
+ # Sort by Date
329
+ daily_readings [device .device_number ].sort (key = lambda x : x .date )
330
+
331
+ desired_date_reading = next (
332
+ filter (
333
+ lambda reading : reading .date .date () == desired_date ,
334
+ readings .data ,
335
+ ),
336
+ 0 ,
337
337
)
338
+ if desired_date_reading == 0 or desired_date_reading .value <= 0 :
339
+ _LOGGER .debug (
340
+ f'Couldn\' t find daily reading for: { desired_date .strftime ("%Y-%m-%d" )} '
341
+ )
342
+ else :
343
+ daily_readings .append (
344
+ RemoteReading (0 , desired_date , desired_date_reading .value )
345
+ )
338
346
else :
339
347
_LOGGER .debug (
340
348
f'Daily reading for date: { daily_reading .date .strftime ("%Y-%m-%d" )} '
@@ -362,6 +370,7 @@ async def _update_data(
362
370
if c .status == 1 and int (c .contract_id ) in self ._contract_ids
363
371
}
364
372
localized_today = TIMEZONE .localize (datetime .now ())
373
+ localized_first_of_month = localized_today .replace (day = 1 )
365
374
kwh_tariff = await self ._get_kwh_tariff ()
366
375
kva_tariff = await self ._get_kva_tariff ()
367
376
@@ -432,63 +441,49 @@ async def _update_data(
432
441
for device in devices :
433
442
attributes_to_add [METER_ID_ATTR_NAME ] = device .device_number
434
443
435
- remote_reading = await self ._get_readings (
444
+ reading_type : ReadingResolution | None = None
445
+ reading_date : date | None = None
446
+
447
+ if localized_today .date () != localized_first_of_month .date ():
448
+ reading_type : ReadingResolution | None = ReadingResolution .MONTHLY
449
+ reading_date : date | None = localized_first_of_month
450
+ elif localized_today .date ().isoweekday () != 1 :
451
+ # If today's the 1st of the month, but not sunday, get weekly from tomorrow
452
+ yesterday = localized_today - timedelta (days = 1 )
453
+ reading_type : ReadingResolution | None = ReadingResolution .WEEKLY
454
+ reading_date : date | None = yesterday
455
+ else :
456
+ # Today is the 1st and is Monday (since monday.isoweekday==1)
457
+ last_month_first_of_the_month = (
458
+ localized_first_of_month - timedelta (days = 1 )
459
+ ).replace (day = 1 )
460
+
461
+ reading_type : ReadingResolution | None = ReadingResolution .MONTHLY
462
+ reading_date : date | None = last_month_first_of_the_month
463
+
464
+ remote_rading = await self ._get_readings (
436
465
contract_id ,
437
466
device .device_number ,
438
467
device .device_code ,
439
- localized_today ,
440
- ReadingResolution . MONTHLY ,
468
+ reading_date ,
469
+ reading_type
441
470
)
442
- if (
443
- remote_reading and remote_reading .total_import
444
- ): # use total_import as validation that reading is OK
445
- future_consumption [device .device_number ] = (
446
- remote_reading .future_consumption_info
447
- )
448
-
449
471
if remote_reading and remote_reading .data :
450
472
daily_readings [device .device_number ] = remote_reading .data
451
473
else :
452
474
_LOGGER .warning (
453
- "No Monthly readings returned for device %s in contract %s on %s" ,
475
+ f"No %s readings returned for device %s in contract %s on %s" ,
476
+ reading_type ,
454
477
device .device_number ,
455
478
contract_id ,
456
- localized_today . strftime ( "%Y-%m-%d" ) ,
479
+ reading_date ,
457
480
)
458
481
daily_readings [device .device_number ] = []
459
482
460
- weekly_future_consumption = None
461
- if localized_today .day == 1 :
462
- # if today's the 1st of the month, "yesterday" is on a different month
463
- yesterday : datetime = localized_today - timedelta (days = 1 )
464
- remote_reading = await self ._get_readings (
465
- contract_id ,
466
- device .device_number ,
467
- device .device_code ,
468
- yesterday ,
469
- ReadingResolution .WEEKLY ,
470
- )
471
- if (
472
- remote_reading and remote_reading .total_import
473
- ): # use total_import as validation that reading OK
474
- daily_readings [device .device_number ] += remote_reading .data
475
- weekly_future_consumption = (
476
- remote_reading .future_consumption_info
477
- )
478
-
479
- # Remove duplicates
480
- daily_readings [device .device_number ] = list (
481
- dict .fromkeys (daily_readings [device .device_number ])
482
- )
483
-
484
- # Sort by Date
485
- daily_readings [device .device_number ].sort (
486
- key = lambda x : x .date
487
- )
488
-
483
+ # Verify today's date appears
489
484
await self ._verify_daily_readings_exist (
490
485
daily_readings [device .device_number ],
491
- localized_today - timedelta ( days = 1 ),
486
+ localized_today . date ( ),
492
487
device ,
493
488
contract_id ,
494
489
)
@@ -514,13 +509,6 @@ async def _update_data(
514
509
].future_consumption
515
510
):
516
511
if (
517
- weekly_future_consumption
518
- and weekly_future_consumption .future_consumption
519
- ):
520
- future_consumption [device .device_number ] = (
521
- weekly_future_consumption
522
- )
523
- elif (
524
512
self ._today_readings .get (today_reading_key )
525
513
and self ._today_readings .get (
526
514
today_reading_key
0 commit comments