From fca930a7cd236526c3c51bd8f563c725032e4bdf Mon Sep 17 00:00:00 2001 From: Fernando Pascual Date: Fri, 30 Jul 2021 23:50:13 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20consulta=20de=20producci=C3=B3n?= =?UTF-8?q?=20de=20solar.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oligo/asyncio/asynciber.py | 20 ++++++++++++++++++++ oligo/iber.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/oligo/asyncio/asynciber.py b/oligo/asyncio/asynciber.py index 2daf1e9..dc6205a 100644 --- a/oligo/asyncio/asynciber.py +++ b/oligo/asyncio/asynciber.py @@ -17,6 +17,7 @@ GUARDAR_ESCENARIO_URL = "escenarioNew/confirmarMedicionOnLine/{}/1/{}" BORRAR_ESCENARIO_URL = "escenarioNew/borrarEscenario/" OBTENER_PERIODO_URL = "consumoNew/obtenerDatosConsumoPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" +OBTENER_PERIODO_GENERACION_URL = "consumoNew/obtenerDatosGeneracionPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" class ResponseException(Exception): @@ -175,6 +176,25 @@ async def consumption(self, start: datetime, end: datetime) -> list: data = await self._consumption_raw(start, end) return [float(x["valor"]) for x in data["y"]["data"][0] if x] + async def _production_raw(self, start: datetime, end: datetime) -> list: + return await self.__request( + OBTENER_PERIODO_GENERACION_URL.format( + start.strftime("%d-%m-%Y"), end.strftime("%d-%m-%Y") + ) + ) + + # Get production data from a time period + # + # start/end: datetime.date objects indicating the time period (both inclusive) + # The supported time range seems to be (not documented) from Jan 1 in the previous year and a max + # length of one year. + # + # Returns a list of production starting a midnight on the start day until 23:00 on the last day. + # Each value is the hourly production in Wh. + async def production(self, start: datetime, end: datetime) -> list: + data = await self._production_raw(start, end) + return [float(x["valor"]) for x in data["y"]["data"][0] if x] + # Get total consumption in Wh (Watt-hour) over a time period # # start/end: datetime.date objects indicating the time period (both inclusive) diff --git a/oligo/iber.py b/oligo/iber.py index 2cf5885..eeb2d42 100644 --- a/oligo/iber.py +++ b/oligo/iber.py @@ -36,6 +36,7 @@ class Iber: __guardar_escenario_url = __domain + "/consumidores/rest/escenarioNew/confirmarMedicionOnLine/{}/1/{}" __borrar_escenario_url = __domain + "/consumidores/rest/escenarioNew/borrarEscenario/" __obtener_periodo_url = __domain + "/consumidores/rest/consumoNew/obtenerDatosConsumoPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" # date format: 07-11-2020 - that's 7 Nov 2020 + __obtener_periodo_generacion_url = __domain + "/consumidores/rest/consumoNew/obtenerDatosGeneracionPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" # date format: 07-11-2020 - that's 7 Nov 2020 __headers = { 'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/77.0.3865.90 Chrome/77.0.3865.90 Safari/537.36", @@ -212,6 +213,37 @@ def consumption(self, start, end): else: values.append(float(x['valor'])) return values + + def _production_raw(self, start, end): + self.__check_session() + start_str = start.strftime('%d-%m-%Y') + end_str = end.strftime('%d-%m-%Y') + + response = self.__session.request("GET", self.__obtener_periodo_generacion_url.format(start_str, end_str), + headers=self.__headers) + if response.status_code != 200: + raise ResponseException + if not response.text: + raise NoResponseException + return response.json() + + # Get production data from a time period + # + # start/end: datetime.date objects indicating the time period (both inclusive) + # The supported time range seems to be (not documented) from Jan 1 in the previous year and a max + # length of one year. + # + # Returns a list of productions starting a midnight on the start day until 23:00 on the last day. + # Each value is the hourly production in Wh. + def production(self, start, end): + json = self._production_raw(start, end) + values = [] + for x in json['y']['data'][0]: + if x is None: + values.append(None) + else: + values.append(float(x['valor'])) + return values # Get total consumption in Wh (Watt-hour) over a time period #