Skip to content

Commit 2417c89

Browse files
committed
Cache upcoming energy rates in an iteration
1 parent f18761a commit 2417c89

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

source/inverter_charge_controller.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def start(self) -> None:
5858
while True:
5959
try:
6060
if first_iteration:
61-
self.next_price_minimum = self.tibber_api_handler.get_next_price_minimum(first_iteration)
61+
self.next_price_minimum = self.tibber_api_handler.get_next_price_minimum(first_iteration=True)
6262
first_iteration = False
6363
else:
6464
self._do_iteration()
@@ -75,7 +75,7 @@ def start(self) -> None:
7575
pause.until(time_to_sleep_to)
7676
self.write_newlines_to_log_file()
7777
self.log.info("Waking up since the the price minimum has to re-checked")
78-
self.next_price_minimum = self.tibber_api_handler.get_next_price_minimum(True)
78+
self.next_price_minimum = self.tibber_api_handler.get_next_price_minimum(first_iteration=True)
7979

8080
self.sems_portal_api_handler.write_values_to_database()
8181

@@ -464,13 +464,25 @@ def _write_energy_buy_statistics_to_database(
464464
In this case, previous values from the same iteration are saved and used when the failed API call is retried.
465465
"""
466466

467+
def _get_upcoming_energy_rates(self) -> list[EnergyRate]:
468+
cache_key = "upcoming_energy_rates"
469+
upcoming_energy_rates = self._get_value_from_cache_if_exists(cache_key)
470+
if upcoming_energy_rates:
471+
return upcoming_energy_rates
472+
473+
upcoming_energy_rates = self.tibber_api_handler.get_upcoming_energy_rates()
474+
self._set_cache_key(cache_key, upcoming_energy_rates)
475+
return upcoming_energy_rates
476+
467477
def _get_next_price_minimum(self) -> EnergyRate:
468478
cache_key = "next_price_minimum"
469479
next_price_minimum = self._get_value_from_cache_if_exists(cache_key)
470480
if next_price_minimum:
471481
return next_price_minimum
472482

473-
next_price_minimum = self.tibber_api_handler.get_next_price_minimum()
483+
next_price_minimum = self.tibber_api_handler.get_next_price_minimum(
484+
upcoming_energy_rates=self._get_upcoming_energy_rates()
485+
)
474486
self._set_cache_key(cache_key, next_price_minimum)
475487
return next_price_minimum
476488

source/tibber_api_handler.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def __init__(self):
2323

2424
self.database_handler = DatabaseHandler("energy_prices")
2525

26-
self.upcoming_energy_rates_cache = []
27-
28-
def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
26+
def get_next_price_minimum(
27+
self, first_iteration: bool = False, upcoming_energy_rates: list[EnergyRate] = None
28+
) -> EnergyRate:
2929
"""
3030
This method performs a series of operations to determine the most cost-effective time to charge by analyzing
3131
upcoming energy rates retrieved from the Tibber API and returns its timestamp.
@@ -55,11 +55,8 @@ def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
5555
EnergyRate: The next price minimum energy rate.
5656
"""
5757
self.log.debug("Finding the price minimum...")
58-
api_result = self._fetch_upcoming_prices_from_api()
59-
all_energy_rates = self._extract_energy_rates_from_api_response(api_result)
60-
self.write_energy_rates_to_database(all_energy_rates)
61-
upcoming_energy_rates = self._remove_energy_rates_from_the_past(all_energy_rates)
62-
self.upcoming_energy_rates_cache = upcoming_energy_rates
58+
if upcoming_energy_rates is None:
59+
upcoming_energy_rates = self.get_upcoming_energy_rates()
6360
if first_iteration and not self._check_if_next_three_prices_are_greater_than_current_one(
6461
upcoming_energy_rates
6562
):
@@ -85,6 +82,13 @@ def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
8582

8683
return minimum_of_energy_rates
8784

85+
def get_upcoming_energy_rates(self) -> list[EnergyRate]:
86+
self.log.debug("Fetching the upcoming energy rates from the Tibber API...")
87+
api_result = self._fetch_upcoming_prices_from_api()
88+
all_energy_rates = self._extract_energy_rates_from_api_response(api_result)
89+
self.write_energy_rates_to_database(all_energy_rates)
90+
return self._remove_energy_rates_from_the_past(all_energy_rates)
91+
8892
@staticmethod
8993
def _check_if_next_three_prices_are_greater_than_current_one(all_upcoming_energy_rates: list[EnergyRate]) -> bool:
9094
"""

0 commit comments

Comments
 (0)