Skip to content

Commit 2b652ac

Browse files
committed
Implement error handling for TooManyRequests to solar forecast
1 parent 77ce048 commit 2b652ac

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ It saves the following data:
215215

216216
- Create bucket: `influx bucket create -org default -token <token> --name default`
217217
- Delete bucket: `influx bucket delete -org default -token <token> --name default`
218-
- Retrieve all solar forecast values: `influx query -org default -token <token> 'from(bucket: "default")
219-
|> range(start: -1d)
218+
- Retrieve all solar forecast values: `influx query -org default -token <token> 'import "experimental"
219+
from(bucket: "default")
220+
|> range(start: 0, stop: experimental.addDuration(d: 2d, to: now()))
220221
|> filter(fn: (r) => r._measurement == "sun_forecast")
221222
|> pivot(rowKey:["_time"], columnKey:["_field"], valueColumn:"_value")'`
222223
- Retrieve all energy prices: `influx query -org default -token <token> 'import "experimental"

Diff for: source/inverter_charge_controller.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime, timedelta
33

44
import pause
5+
import requests.exceptions
56
from abscence_handler import AbsenceHandler
67
from aiohttp import ClientError
78
from energy_amount import EnergyAmount, EnergyRate
@@ -58,7 +59,10 @@ def start(self) -> None:
5859
time_to_sleep_to = now.replace(hour=14, minute=0, second=0, microsecond=0)
5960
if now > time_to_sleep_to:
6061
time_to_sleep_to += timedelta(days=1)
61-
self.log.info(f"The price minimum has to re-checked --> Waiting until {time_to_sleep_to}...")
62+
self.log.info(
63+
f"The price minimum {next_price_minimum} has to re-checked "
64+
+ f"--> Waiting until {time_to_sleep_to}..."
65+
)
6266
pause.until(time_to_sleep_to)
6367
self.log.info("Waking up since the the price minimum has to re-checked")
6468
next_price_minimum = self.tibber_api_handler.get_next_price_minimum(True)
@@ -110,12 +114,19 @@ def _do_iteration(self, current_energy_rate: float) -> EnergyRate:
110114
"The price minimum has to be re-checked since it is at the end of a day and the price rates for tomorrow are unavailable"
111115
)
112116

113-
expected_power_harvested_till_next_minimum = self.sun_forecast_handler.get_solar_output_in_timeframe(
114-
timestamp_now, next_price_minimum.timestamp
115-
)
116-
self.log.info(
117-
f"The expected energy harvested by the sun till the next price minimum is {expected_power_harvested_till_next_minimum}"
118-
)
117+
try:
118+
expected_power_harvested_till_next_minimum = self.sun_forecast_handler.get_solar_output_in_timeframe(
119+
timestamp_now, next_price_minimum.timestamp
120+
)
121+
self.log.info(
122+
f"The expected energy harvested by the sun till the next price minimum is {expected_power_harvested_till_next_minimum}"
123+
)
124+
except requests.exceptions.HTTPError as e:
125+
if e.response.status_code != 429:
126+
raise e
127+
128+
self.log.warning("Too many requests to the solar forecast API, using the debug solar output instead")
129+
expected_power_harvested_till_next_minimum = self.sun_forecast_handler.get_debug_solar_output()
119130

120131
if self.absence_handler.check_for_current_absence():
121132
self.log.info(

Diff for: source/sun_forecast_handler.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def retrieve_solar_forecast_data(self, rooftop_id: str) -> list[dict]:
4242
def retrieve_historic_data(self, rooftop_id: str) -> list[dict]:
4343
return self._retrieve_data_from_api(rooftop_id, "estimated_actuals")
4444

45-
def _get_debug_solar_output(self) -> EnergyAmount:
45+
def get_debug_solar_output(self) -> EnergyAmount:
4646
"""
47-
Returns a sample debug value for solar energy output.
47+
Returns a sample debug value for solar energy output. This value is also used where the API returns a 429
48+
(Too Many Requests) error.
4849
4950
Returns:
5051
EnergyAmount: A sample energy amount of 10,000 watt-hours.
5152
"""
52-
# We use a sample value for debugging the code since the API offers very limited call per day
5353
self.log.debug("Getting debug estimated solar output of today")
5454
return EnergyAmount(watt_hours=10000)
5555

0 commit comments

Comments
 (0)