Skip to content

Commit

Permalink
Handle timezone switch
Browse files Browse the repository at this point in the history
  • Loading branch information
JRascagneres committed Oct 29, 2023
1 parent 936c9aa commit ece172e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions custom_components/national_grid/coordinators/national_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ def get_data(
current_data,
"wind_forecast",
get_hourly_wind_forecast,
today_full,
now_utc_full,
)

wind_forecast_earliest = obtain_data_with_fallback(
current_data,
"wind_forecast_earliest",
get_hourly_wind_forecast_earliest,
today_full,
now_utc_full,
)

Expand Down Expand Up @@ -147,9 +145,7 @@ def get_data_if_exists(data, key: str):
return None


def get_hourly_wind_forecast(
now: datetime, now_utc: datetime
) -> NationalGridWindForecast:
def get_hourly_wind_forecast(now_utc: datetime) -> NationalGridWindForecast:
# Need to calculate start. We want data from 8pm on current day to day+2 8pm... however, this is calculated every so often.
# This means that day + 2 isn't calculated until 03:30 GMT

Expand All @@ -170,7 +166,7 @@ def get_hourly_wind_forecast(
start_time_formatted = start_time.strftime("%Y-%m-%dT%H:%M:%S")
end_time_formatted = end_time.strftime("%Y-%m-%dT%H:%M:%S")

current_hour = now.replace(minute=0, second=0, microsecond=0)
current_hour = now_utc.replace(minute=0, second=0, microsecond=0)

url = (
"https://data.elexon.co.uk/bmrs/api/v1/forecast/generation/wind/latest?from="
Expand Down Expand Up @@ -200,14 +196,15 @@ def get_hourly_wind_forecast(
if forecast_item_start_time == current_hour:
current_generation = int(item["generation"])

if current_generation == 0:
raise UnexpectedDataError

return NationalGridWindForecast(
forecast=wind_forecast, current_value=current_generation
)


def get_hourly_wind_forecast_earliest(
now: datetime, now_utc: datetime
) -> NationalGridWindForecast:
def get_hourly_wind_forecast_earliest(now_utc: datetime) -> NationalGridWindForecast:
# Need to calculate start. We want data from 8pm on current day to day+2 8pm... however, this is calculated every so often.
# This means that day + 2 isn't calculated until 03:30 GMT

Expand All @@ -228,7 +225,7 @@ def get_hourly_wind_forecast_earliest(
start_time_formatted = start_time.strftime("%Y-%m-%dT%H:%M:%S")
end_time_formatted = end_time.strftime("%Y-%m-%dT%H:%M:%S")

current_hour = now.replace(minute=0, second=0, microsecond=0)
current_hour = now_utc.replace(minute=0, second=0, microsecond=0)

url = (
"https://data.elexon.co.uk/bmrs/api/v1/forecast/generation/wind/earliest?from="
Expand Down Expand Up @@ -258,6 +255,9 @@ def get_hourly_wind_forecast_earliest(
if forecast_item_start_time == current_hour:
current_generation = int(item["generation"])

if current_generation == 0:
raise UnexpectedDataError

return NationalGridWindForecast(
forecast=wind_forecast_earliest, current_value=current_generation
)
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.26",
"version": "0.0.27",
"config_flow": true
}

0 comments on commit ece172e

Please sign in to comment.