Skip to content

Commit

Permalink
Order the generation and ensure unique solar
Browse files Browse the repository at this point in the history
  • Loading branch information
JRascagneres committed Sep 1, 2023
1 parent 94eef0d commit 5c2f6d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions custom_components/national_grid/coordinators/national_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ def get_half_hourly_solar_forecast(
minutes=30
)

yesterday = now - timedelta(days=1)
tomorrow = now + timedelta(days=1)
day_after_tomorrow = now + timedelta(days=2)

times = [now, tomorrow, day_after_tomorrow]
times = [yesterday, now, tomorrow, day_after_tomorrow]
results = []
for date in times:
date_format = date.strftime("%Y-%m-%d")
Expand All @@ -235,18 +236,26 @@ def get_half_hourly_solar_forecast(
response = get_bmrs_data_items(url)
results = results + response

unique_date_list = []
forecast = []
current_value = 0
for item in results:
if item["businessType"] != "Solar generation":
continue

period_from_midnight = timedelta(
minutes=30 * (int(item["settlementPeriod"]) - 1)
)
settlementDate = item["settlementDate"]
settlementPeriod = item["settlementPeriod"]

unique_key = settlementDate + settlementPeriod
if unique_key in unique_date_list:
continue

unique_date_list.append(unique_key)

period_from_midnight = timedelta(minutes=30 * (int(settlementPeriod) - 1))

start_time = (
datetime.strptime(item["settlementDate"], "%Y-%m-%d") + period_from_midnight
datetime.strptime(settlementDate, "%Y-%m-%d") + period_from_midnight
).replace(tzinfo=now.tzinfo)

gen_value = int(float(item["quantity"]))
Expand All @@ -258,6 +267,8 @@ def get_half_hourly_solar_forecast(
if start_time == nearest_30_minutes:
current_value = gen_value

forecast = sorted(forecast, key=lambda x: x["start_time"])

return NationalGridSolarForecast(
current_value=current_value,
forecast=forecast,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/national_grid/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"dependencies": [],
"iot_class": "cloud_polling",
"requirements": [],
"version": "0.0.22",
"version": "0.0.23",
"config_flow": true
}

0 comments on commit 5c2f6d1

Please sign in to comment.