Skip to content

Commit 3e876d4

Browse files
authored
Merge pull request #20 from fer112233/master
Añadido consulta de producción de solar.
2 parents ea5c320 + fca930a commit 3e876d4

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

oligo/asyncio/asynciber.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
GUARDAR_ESCENARIO_URL = "escenarioNew/confirmarMedicionOnLine/{}/1/{}"
1818
BORRAR_ESCENARIO_URL = "escenarioNew/borrarEscenario/"
1919
OBTENER_PERIODO_URL = "consumoNew/obtenerDatosConsumoPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/"
20+
OBTENER_PERIODO_GENERACION_URL = "consumoNew/obtenerDatosGeneracionPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/"
2021

2122

2223
class ResponseException(Exception):
@@ -175,6 +176,25 @@ async def consumption(self, start: datetime, end: datetime) -> list:
175176
data = await self._consumption_raw(start, end)
176177
return [float(x["valor"]) for x in data["y"]["data"][0] if x]
177178

179+
async def _production_raw(self, start: datetime, end: datetime) -> list:
180+
return await self.__request(
181+
OBTENER_PERIODO_GENERACION_URL.format(
182+
start.strftime("%d-%m-%Y"), end.strftime("%d-%m-%Y")
183+
)
184+
)
185+
186+
# Get production data from a time period
187+
#
188+
# start/end: datetime.date objects indicating the time period (both inclusive)
189+
# The supported time range seems to be (not documented) from Jan 1 in the previous year and a max
190+
# length of one year.
191+
#
192+
# Returns a list of production starting a midnight on the start day until 23:00 on the last day.
193+
# Each value is the hourly production in Wh.
194+
async def production(self, start: datetime, end: datetime) -> list:
195+
data = await self._production_raw(start, end)
196+
return [float(x["valor"]) for x in data["y"]["data"][0] if x]
197+
178198
# Get total consumption in Wh (Watt-hour) over a time period
179199
#
180200
# start/end: datetime.date objects indicating the time period (both inclusive)

oligo/iber.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Iber:
3636
__guardar_escenario_url = __domain + "/consumidores/rest/escenarioNew/confirmarMedicionOnLine/{}/1/{}"
3737
__borrar_escenario_url = __domain + "/consumidores/rest/escenarioNew/borrarEscenario/"
3838
__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
39+
__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
3940

4041
__headers = {
4142
'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):
212213
else:
213214
values.append(float(x['valor']))
214215
return values
216+
217+
def _production_raw(self, start, end):
218+
self.__check_session()
219+
start_str = start.strftime('%d-%m-%Y')
220+
end_str = end.strftime('%d-%m-%Y')
221+
222+
response = self.__session.request("GET", self.__obtener_periodo_generacion_url.format(start_str, end_str),
223+
headers=self.__headers)
224+
if response.status_code != 200:
225+
raise ResponseException
226+
if not response.text:
227+
raise NoResponseException
228+
return response.json()
229+
230+
# Get production data from a time period
231+
#
232+
# start/end: datetime.date objects indicating the time period (both inclusive)
233+
# The supported time range seems to be (not documented) from Jan 1 in the previous year and a max
234+
# length of one year.
235+
#
236+
# Returns a list of productions starting a midnight on the start day until 23:00 on the last day.
237+
# Each value is the hourly production in Wh.
238+
def production(self, start, end):
239+
json = self._production_raw(start, end)
240+
values = []
241+
for x in json['y']['data'][0]:
242+
if x is None:
243+
values.append(None)
244+
else:
245+
values.append(float(x['valor']))
246+
return values
215247

216248
# Get total consumption in Wh (Watt-hour) over a time period
217249
#

0 commit comments

Comments
 (0)