Skip to content

Commit

Permalink
Refactor and remove sort from apply dampening
Browse files Browse the repository at this point in the history
  • Loading branch information
autoSteve committed Dec 1, 2024
1 parent 25cddcb commit cd1c9e4
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions custom_components/solcast_solar/solcastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2859,28 +2859,31 @@ async def __http_data_call(
# Apply dampening to the new data
start_time = time.time()
valid_granular_dampening = self.__valid_granular_dampening()
for forecast in sorted(new_data, key=itemgetter("period_start")):
for forecast in new_data:
period_start = forecast["period_start"]
pv = round(forecast["pv_estimate"], 4)
pv10 = round(forecast["pv_estimate10"], 4)
pv90 = round(forecast["pv_estimate90"], 4)

# Retrieve the dampening factor for the period, and dampen the estimates.
dampening_factor = self.__get_dampening_factor(site, period_start.astimezone(self._tz), valid_granular_dampening)
pv_dampened = round(pv * dampening_factor, 4)
pv10_dampened = round(pv10 * dampening_factor, 4)
pv90_dampened = round(pv90 * dampening_factor, 4)

# Add or update the new entries.
self.__forecast_entry_update(forecasts, period_start, pv_dampened, pv10_dampened, pv90_dampened)
self.__forecast_entry_update(forecasts_undampened, period_start, pv, pv10, pv90)
self.__forecast_entry_update(
forecasts,
period_start,
round(forecast["pv_estimate"] * dampening_factor, 4),
round(forecast["pv_estimate10"] * dampening_factor, 4),
round(forecast["pv_estimate90"] * dampening_factor, 4),
)
self.__forecast_entry_update(
forecasts_undampened,
period_start,
round(forecast["pv_estimate"], 4),
round(forecast["pv_estimate10"], 4),
round(forecast["pv_estimate90"], 4),
)
_LOGGER.debug(
"Task apply_dampening took %.3f seconds",
time.time() - start_time,
)

def sort_and_prune(data, past_days, forecasts):
# start_time = time.time()
past_days = self.get_day_start_utc(future=past_days * -1)
forecasts = sorted(
filter(
Expand All @@ -2890,18 +2893,13 @@ def sort_and_prune(data, past_days, forecasts):
key=itemgetter("period_start"),
)
data["siteinfo"].update({site: {"forecasts": copy.deepcopy(forecasts)}})
# _LOGGER.debug("Task sort_and_prune forecast took %.3f seconds", time.time() - start_time)

start_time = time.time()
with ThreadPoolExecutor() as ex:
ex.submit(sort_and_prune, self._data, 730, forecasts)
ex.submit(sort_and_prune, self._data_undampened, 14, forecasts_undampened)
_LOGGER.debug("Task sort_and_prune took %.3f seconds", time.time() - start_time)

# _LOGGER.debug(
# "HTTP data call processing took %.3f seconds",
# time.time() - overall_start_time,
# )
_LOGGER.debug("Forecasts dictionary length %s (%s un-dampened)", len(forecasts), len(forecasts_undampened))
except InvalidStateError:
return DataCallStatus.FAIL
Expand Down

0 comments on commit cd1c9e4

Please sign in to comment.