From baed2c4c2724f8c6ab8ac358f61702f9aed1f853 Mon Sep 17 00:00:00 2001 From: ramonsca Date: Tue, 16 Jan 2024 23:15:45 +0100 Subject: [PATCH] Fix initial configuration and control UFD api error --- custom_components/pvpc_energy/coordinator.py | 1 + custom_components/pvpc_energy/ufd.py | 21 ++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/custom_components/pvpc_energy/coordinator.py b/custom_components/pvpc_energy/coordinator.py index 6283c4a..9c3dc3b 100644 --- a/custom_components/pvpc_energy/coordinator.py +++ b/custom_components/pvpc_energy/coordinator.py @@ -155,6 +155,7 @@ async def get_data(getter, start_date, end_date, total_data, days): request_end_date -= datetime.timedelta(days=1) request_end_date += datetime.timedelta(days=1) data = await getter(request_end_date, request_start_date) + if len(data) == 0: break result |= data total_data |= data _LOGGER.debug(f"END - get_data: len(result)={len(result)}") diff --git a/custom_components/pvpc_energy/ufd.py b/custom_components/pvpc_energy/ufd.py index 4c9b43d..b951440 100644 --- a/custom_components/pvpc_energy/ufd.py +++ b/custom_components/pvpc_energy/ufd.py @@ -69,12 +69,19 @@ async def getHeaders(session): return headers async def consumptions(start_date, end_date): + # Hay un error en la web de UFD. Al solicitar los consumos desplaza el rango un día hacia atrás + # y del primer día (que no se solicita) devuelve solo el consumo de las 24:00. + # Solución: Sumamos un día a end_date y descartamos los días que no tengan 24 consumos y + # no estén en el periodo solicitado, para que también funcione correctamente cuando se solucione la web. + _LOGGER.debug(f"START - UFD.consumptions(start_date={start_date.isoformat()}, end_date={end_date.isoformat()})") - + result = {} async with aiohttp.ClientSession() as session: headers = await UFD.getHeaders(session) - url = UFD.consumptions_url.format(nif=UFD.nif, cups=UFD.cups, start_date=start_date.strftime('%d/%m/%Y'), end_date=end_date.strftime('%d/%m/%Y')) + # Fix UFD: Sumamos un día a end_date + url = UFD.consumptions_url.format(nif=UFD.nif, cups=UFD.cups, start_date=start_date.strftime('%d/%m/%Y'), + end_date=(end_date + datetime.timedelta(days=1)).strftime('%d/%m/%Y')) response = None _LOGGER.info(f"UFD.get_consumptions(start_date={start_date.isoformat()}, end_date={end_date.isoformat()})") async with session.get(url, headers=headers, ssl=False) as resp: @@ -95,10 +102,12 @@ async def consumptions(start_date, end_date): _LOGGER.error(f"status_code: {resp.status}, response: {text}") if response is not None and 'items' in response: for dayConsumption in response['items']: - timestamp = int(time.mktime(time.strptime(dayConsumption['periodStartDate'], '%d/%m/%Y'))) - for hourConsumption in dayConsumption['consumptions']['items']: - result[timestamp] = float(hourConsumption['consumptionValue'].replace(',','.')) - timestamp += 3600 + if len(dayConsumption['consumptions']['items']) == 24: + if start_date <= datetime.datetime.strptime(dayConsumption['periodStartDate'], '%d/%m/%Y').date() <= end_date: + timestamp = int(time.mktime(time.strptime(dayConsumption['periodStartDate'], '%d/%m/%Y'))) + for hourConsumption in dayConsumption['consumptions']['items']: + result[timestamp] = float(hourConsumption['consumptionValue'].replace(',','.')) + timestamp += 3600 _LOGGER.debug(f"END - UFD.consumptions: len(result)={len(result)}") return result