Skip to content

Commit

Permalink
Fix loop error
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyang17 committed Feb 13, 2024
1 parent d6b6d19 commit 75f7f10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
11 changes: 5 additions & 6 deletions custom_components/pvpc_energy/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,16 @@ async def import_energy_data(hass):

async def get_data(getter, start_date, end_date, data, days):
_LOGGER.debug(f"START - get_data(getter={getter}, start_date={start_date.isoformat()}, end_date={end_date.isoformat()}, len(data)={len(data)}, days={days})")
timestamps = data.keys()
data_len = len(data)

request_start_date = end_date + datetime.timedelta(days=1)
while request_start_date > start_date:
request_end_date = request_start_date - datetime.timedelta(days=1)
while int(time.mktime(request_end_date.timetuple())) in timestamps and request_end_date >= start_date:
while int(time.mktime(request_end_date.timetuple())) in data.keys() and request_end_date >= start_date:
request_end_date -= datetime.timedelta(days=1)
if request_end_date < start_date: break
request_start_date = request_end_date - datetime.timedelta(days=1)
while int(time.mktime(request_start_date.timetuple())) not in timestamps and request_start_date >= start_date and (request_end_date - request_start_date).days < days:
while int(time.mktime(request_start_date.timetuple())) not in data.keys() and request_start_date >= start_date and (request_end_date - request_start_date).days < days:
request_start_date -= datetime.timedelta(days=1)
request_start_date += datetime.timedelta(days=1)
new_data = await getter(request_start_date, request_end_date)
Expand All @@ -154,9 +153,9 @@ async def get_data(getter, start_date, end_date, data, days):
elif len(new_data) > 0:
data |= new_data
else:
first_data_day = datetime.datetime.fromtimestamp(min(list(timestamps))).date()
first_data_day = datetime.datetime.fromtimestamp(min(list(data.keys()))).date()
if request_end_date < first_data_day:
data[int(time.mktime((request_end_date + datetime.timedelta(days=1)).timetuple())) - 3600] = '-'
data[min(list(data.keys())) - 3600] = '-'
elif request_start_date > first_data_day:
timestamp = int(time.mktime((request_start_date).timetuple()))
end_timestamp = int(time.mktime((request_end_date).timetuple()))
Expand Down Expand Up @@ -289,7 +288,7 @@ def load_energy_data(file_path):
timestamp, consumption, price = line[0:-1].split(',')[-3:]
timestamp = int(timestamp)
if consumption == '-':
if (datetime.datetime.today() - datetime.datetime.fromtimestamp(timestamp)).days >= 750:
if (datetime.datetime.today() - datetime.datetime.fromtimestamp(timestamp)).days >= 600:
consumptions[timestamp] = consumption
elif consumption != '':
consumptions[timestamp] = float(consumption)
Expand Down
12 changes: 6 additions & 6 deletions custom_components/pvpc_energy/ree.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ async def pvpc(start_date, end_date):
_LOGGER.debug(f"START - REE.pvpc(start_date={start_date.isoformat()}, end_date={end_date.isoformat()})")

result = None
url = REE.url_indicators.format(start_date=start_date.strftime('%Y-%m-%d'), end_date=end_date.strftime('%Y-%m-%dT23%%3A00%%3A00'))
response = None
url = REE.url_indicators.format(start_date=start_date.strftime('%Y-%m-%d'), end_date=end_date.strftime('%Y-%m-%dT23%%3A00%%3A00'))
_LOGGER.info(f"REE.get_prices(start_date={start_date.isoformat()}, end_date={end_date.isoformat()})")
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=REE.getHeaders(), ssl=False) as resp:
if resp.status == 200:
response = await resp.json()
if response is not None:
result = {}
if len(response['indicator']['values']) > 0:
for value in response['indicator']['values']:
result[int(datetime.datetime.fromisoformat(value['datetime']).timestamp())] = round(value['value'] / 1000, 5)
if response is not None:
result = {}
if len(response['indicator']['values']) > 0:
for value in response['indicator']['values']:
result[int(datetime.datetime.fromisoformat(value['datetime']).timestamp())] = round(value['value'] / 1000, 5)

_LOGGER.debug(f"END - REE.pvpc: len(result)={'None' if result is None else len(result)}")
return result

0 comments on commit 75f7f10

Please sign in to comment.