@@ -129,7 +129,7 @@ def calculate_min_and_max_of_soc_in_timeframe(
129
129
)
130
130
return minimum_soc , maximum_soc
131
131
132
- def retrieve_solar_data (self , timeframe_start : datetime , timeframe_end : datetime ) -> dict [str , Power ]:
132
+ def retrieve_solar_data (self , retrieve_future_data : bool ) -> dict [str , Power ]:
133
133
"""
134
134
Retrieves solar data for a specified timeframe either from the solar forecast API or a debug solar output
135
135
depending on the configuration and API response.
@@ -138,8 +138,7 @@ def retrieve_solar_data(self, timeframe_start: datetime, timeframe_end: datetime
138
138
(too many requests) from the solar forecast API, the method falls back to using a debug solar data output.
139
139
140
140
Args:
141
- timeframe_start: Start date and time of the timeframe for which solar data is requested.
142
- timeframe_end: End date and time of the timeframe for which solar data is requested.
141
+ retrieve_future_data: Whether to retrieve data for the future or the past.
143
142
144
143
Returns:
145
144
A dictionary where keys represent specific times and values represent the forecasted power at those times.
@@ -152,42 +151,39 @@ def retrieve_solar_data(self, timeframe_start: datetime, timeframe_end: datetime
152
151
return self ._get_debug_solar_data ()
153
152
154
153
try :
155
- return self .retrieve_solar_data_from_api (timeframe_start , timeframe_end )
154
+ return self .retrieve_solar_data_from_api (retrieve_future_data )
156
155
except requests .exceptions .HTTPError as e :
157
156
if e .response .status_code != 429 :
158
157
raise e
159
158
self .log .warning ("Too many requests to the solar forecast API, using the debug solar output instead" )
160
159
return self ._get_debug_solar_data ()
161
160
162
- def retrieve_solar_data_from_api (self , timeframe_start : datetime , timeframe_end : datetime ) -> dict [str , Power ]:
161
+ def retrieve_solar_data_from_api (self , retrieve_future_data : bool ) -> dict [str , Power ]:
163
162
"""
164
163
Retrieves solar data from an API over a specified timeframe. The function collects photovoltaic forecasts and/or
165
164
historic data for multiple rooftops, processes the data into a dictionary mapping timestamps to cumulative power
166
165
values, and writes relevant data to a database.
167
166
168
167
Args:
169
- timeframe_start (datetime): The start of the timeframe for which to retrieve solar data.
170
- timeframe_end (datetime): The end of the timeframe for which to retrieve solar data.
168
+ retrieve_future_data: Whether to retrieve data for the future or the past.
171
169
172
170
Returns:
173
171
dict[str, Power]: A dictionary where keys represent timestamps (as ISO format strings) and values are Power
174
172
objects corresponding to the cumulative power generation.
175
173
"""
176
174
rooftop_ids = self ._get_rooftop_ids ()
177
175
178
- need_to_retrieve_forecast_data , need_to_retrieve_historic_data = self ._need_to_retrieve_data (
179
- timeframe_start , timeframe_end
180
- )
181
-
182
176
solar_data = {}
183
177
184
178
now = TimeHandler .get_time ().isoformat ()
185
179
for rooftop_id in rooftop_ids :
186
180
data_for_rooftop = []
187
- if need_to_retrieve_historic_data :
188
- data_for_rooftop += self .retrieve_historic_data_from_api (rooftop_id )
189
- if need_to_retrieve_forecast_data :
181
+ if retrieve_future_data :
182
+ self .log .debug ("Need to retrieve forecast data" )
190
183
data_for_rooftop += self .retrieve_forecast_data_from_api (rooftop_id )
184
+ else :
185
+ self .log .debug ("Need to retrieve historic data" )
186
+ data_for_rooftop += self .retrieve_historic_data_from_api (rooftop_id )
191
187
self .timeframe_duration = parse_duration (data_for_rooftop [0 ]["period" ])
192
188
for timeslot in data_for_rooftop :
193
189
period_start = (
@@ -248,31 +244,6 @@ def _get_rooftop_ids() -> list[str]:
248
244
rooftop_ids .append (rooftop_id_2 )
249
245
return rooftop_ids
250
246
251
- def _need_to_retrieve_data (self , timeframe_start : datetime , timeframe_end : datetime ) -> tuple [bool , bool ]:
252
- """
253
- This method evaluates whether either historic data or forecast data needs to be retrieved based on the given
254
- start and end timeframes in comparison to the current time.
255
-
256
- Args:
257
- timeframe_start (datetime): The start of the timeframe for evaluation.
258
- timeframe_end (datetime): The end of the timeframe for evaluation.
259
-
260
- Returns:
261
- tuple[bool, bool]: A tuple containing two boolean values:
262
- - The first boolean indicates whether retrieval of forecast data is required.
263
- - The second boolean indicates whether retrieval of historic data is required.
264
- """
265
- need_to_retrieve_historic_data = False
266
- need_to_retrieve_forecast_data = False
267
- now_minus_offset = TimeHandler .get_time (sanitize_seconds = True ) - timedelta (seconds = 1 )
268
- if timeframe_start >= now_minus_offset or timeframe_end >= now_minus_offset :
269
- self .log .debug ("Need to retrieve forecast data" )
270
- need_to_retrieve_forecast_data = True
271
- if timeframe_start <= now_minus_offset :
272
- self .log .debug ("Need to retrieve historic data" )
273
- need_to_retrieve_historic_data = True
274
- return need_to_retrieve_forecast_data , need_to_retrieve_historic_data
275
-
276
247
@staticmethod
277
248
def _calculate_energy_usage_in_timeframe (
278
249
timeframe_start : datetime ,
0 commit comments