@@ -23,9 +23,9 @@ def __init__(self):
23
23
24
24
self .database_handler = DatabaseHandler ("energy_prices" )
25
25
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 :
29
29
"""
30
30
This method performs a series of operations to determine the most cost-effective time to charge by analyzing
31
31
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:
55
55
EnergyRate: The next price minimum energy rate.
56
56
"""
57
57
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 ()
63
60
if first_iteration and not self ._check_if_next_three_prices_are_greater_than_current_one (
64
61
upcoming_energy_rates
65
62
):
@@ -85,6 +82,13 @@ def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
85
82
86
83
return minimum_of_energy_rates
87
84
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
+
88
92
@staticmethod
89
93
def _check_if_next_three_prices_are_greater_than_current_one (all_upcoming_energy_rates : list [EnergyRate ]) -> bool :
90
94
"""
0 commit comments