Skip to content

Commit 41ed6a0

Browse files
committed
Code optimizations to "buildforcastdata" in solcastapi.py.
1 parent 344b19e commit 41ed6a0

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

custom_components/solcast_solar/solcastapi.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -748,37 +748,36 @@ async def buildforcastdata(self):
748748
yesterday = dt.now(self._tz).date() + timedelta(days=-730)
749749
lastday = dt.now(self._tz).date() + timedelta(days=7)
750750

751-
_forecasts = []
752-
753-
for s in self._data['siteinfo']:
751+
_forecasts = {}
752+
753+
for s, siteinfo in self._data['siteinfo'].items():
754754
tally = 0
755-
for x in self._data['siteinfo'][s]['forecasts']:
755+
for x in siteinfo['forecasts']:
756756
#loop each rooftop site and its forecasts
757757
z = x["period_start"]
758758
zz = z.astimezone(self._tz) #- timedelta(minutes=30)
759759

760760
#v4.0.8 added code to dampen the forecast data.. (* self._damp[h])
761761

762-
if zz.date() < lastday and zz.date() > yesterday:
762+
if yesterday < zz.date() < lastday:
763763
h = f"{zz.hour}"
764764
if zz.date() == today:
765765
tally += min(x[self._use_data_field] * 0.5 * self._damp[h], self._hardlimit)
766766

767-
itm = next((item for item in _forecasts if item["period_start"] == z), None)
767+
itm = _forecasts.get(z)
768768
if itm:
769769
itm["pv_estimate"] = min(round(itm["pv_estimate"] + (x["pv_estimate"] * self._damp[h]),4), self._hardlimit)
770770
itm["pv_estimate10"] = min(round(itm["pv_estimate10"] + (x["pv_estimate10"] * self._damp[h]),4), self._hardlimit)
771771
itm["pv_estimate90"] = min(round(itm["pv_estimate90"] + (x["pv_estimate90"] * self._damp[h]),4), self._hardlimit)
772772
else:
773-
_forecasts.append({"period_start": z,"pv_estimate": min(round((x["pv_estimate"]* self._damp[h]),4), self._hardlimit),
774-
"pv_estimate10": min(round((x["pv_estimate10"]* self._damp[h]),4), self._hardlimit),
775-
"pv_estimate90": min(round((x["pv_estimate90"]* self._damp[h]),4), self._hardlimit)})
776-
777-
self._data['siteinfo'][s]['tally'] = round(tally, 4)
778-
779-
_forecasts = sorted(_forecasts, key=itemgetter("period_start"))
780-
781-
self._data_forecasts = _forecasts
773+
_forecasts[z] = {"period_start": z,"pv_estimate": min(round((x["pv_estimate"]* self._damp[h]),4), self._hardlimit),
774+
"pv_estimate10": min(round((x["pv_estimate10"]* self._damp[h]),4), self._hardlimit),
775+
"pv_estimate90": min(round((x["pv_estimate90"]* self._damp[h]),4), self._hardlimit)}
776+
777+
siteinfo['tally'] = round(tally, 4)
778+
779+
self._data_forecasts = list(_forecasts.values())
780+
self._data_forecasts.sort(key=itemgetter("period_start"))
782781

783782
await self.checkDataRecords()
784783

0 commit comments

Comments
 (0)