2323from iec_api .models .device import Device
2424from iec_api .models .exceptions import IECError
2525from iec_api .models .jwt import JWT
26- from iec_api .models .remote_reading import ReadingResolution , RemoteReading , FutureConsumptionInfo
26+ from iec_api .models .remote_reading import ReadingResolution , RemoteReading , FutureConsumptionInfo , RemoteReadingResponse
2727
2828from .commons import find_reading_by_date
2929from .const import DOMAIN , CONF_USER_ID , STATICS_DICT_NAME , STATIC_KWH_TARIFF , INVOICE_DICT_NAME , \
3434
3535
3636async def _verify_daily_readings_exist (daily_readings : list [RemoteReading ], desired_date : datetime , device : Device ,
37- contract_id : str , api : IecClient , prefetched_reading : RemoteReading | None = None ):
37+ contract_id : str , api : IecClient ,
38+ prefetched_reading : RemoteReadingResponse | None = None ):
3839 desired_date = desired_date .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
3940 daily_reading = next (filter (lambda x : find_reading_by_date (x , desired_date ), daily_readings ), None )
4041 if not daily_reading :
@@ -137,7 +138,7 @@ async def _async_update_data(
137138
138139 future_consumption : FutureConsumptionInfo | None = None
139140 daily_readings : list [RemoteReading ] | None = None
140- today_reading : RemoteReading | None = None
141+ today_reading : RemoteReadingResponse | None = None
141142 if self .is_smart_meter :
142143 # For some reason, there are differences between sending 2024-03-01 and sending 2024-03-07 (Today)
143144 # So instead of sending the 1st day of the month, just sending today date
@@ -155,6 +156,7 @@ async def _async_update_data(
155156 future_consumption = remote_reading .future_consumption_info
156157 daily_readings = remote_reading .data
157158
159+ weekly_future_consumption = None
158160 if datetime .today ().day == 1 :
159161 # if today's the 1st of the month, "yesterday" is on a different month
160162 yesterday : datetime = monthly_report_req_date - timedelta (days = 1 )
@@ -163,6 +165,7 @@ async def _async_update_data(
163165 ReadingResolution .WEEKLY , self ._contract_id )
164166 if remote_reading :
165167 daily_readings += remote_reading .data
168+ weekly_future_consumption = remote_reading .future_consumption_info
166169
167170 # Remove duplicates
168171 daily_readings = list (dict .fromkeys (daily_readings ))
@@ -179,9 +182,30 @@ async def _async_update_data(
179182 today_reading = await self .api .get_remote_reading (device .device_number , int (device .device_code ),
180183 datetime .today (), datetime .today (),
181184 ReadingResolution .DAILY , self ._contract_id )
185+ self ._today_reading = today_reading
186+
182187 await _verify_daily_readings_exist (daily_readings , datetime .today (), device , self ._contract_id , self .api ,
183188 today_reading )
184189
190+ # fallbacks for future consumption since IEC api is broken :/
191+ if not future_consumption .future_consumption :
192+ if weekly_future_consumption and weekly_future_consumption .future_consumption :
193+ future_consumption = weekly_future_consumption
194+ elif self ._today_reading and self ._today_reading .future_consumption_info .future_consumption :
195+ future_consumption = self ._today_reading .future_consumption_info
196+ else :
197+ req_date = datetime .today () - timedelta (days = 2 )
198+ two_days_ago_reading = await self .api .get_remote_reading (device .device_number ,
199+ int (device .device_code ),
200+ req_date , req_date ,
201+ ReadingResolution .DAILY ,
202+ self ._contract_id )
203+
204+ if two_days_ago_reading :
205+ future_consumption = two_days_ago_reading .future_consumption_info
206+ else :
207+ _LOGGER .debug ("Failed fetching FutureConsumption, data in IEC API is corrupted" )
208+
185209 static_data = {
186210 STATIC_KWH_TARIFF : (await self .api .get_kwh_tariff ()) / 100 ,
187211 STATIC_CONTRACT : self ._contract_id ,
0 commit comments