1
- from datetime import date , datetime , timedelta
1
+ from datetime import datetime , timedelta
2
2
3
3
import pause
4
4
from abscence_handler import AbsenceHandler
@@ -164,9 +164,23 @@ def _do_iteration(self) -> datetime: # FIXME: Find better name
164
164
f"Need to charge to { required_state_of_charge } % in order to reach the next minimum with { target_min_state_of_charge } % left"
165
165
)
166
166
167
+ energy_bought_before_charging = self .sems_portal_api_handler .get_energy_buy ()
168
+ timestamp_starting_to_charge = datetime .now (tz = self .timezone )
169
+ self .log .debug (f"The amount of energy bought before charging is { energy_bought_before_charging } " )
170
+
167
171
# TODO: Implement error handling
168
172
self ._charge_inverter (required_state_of_charge )
169
173
174
+ timestamp_ending_to_charge = datetime .now (tz = self .timezone )
175
+ energy_bought = self ._calculate_amount_of_energy_bought (
176
+ energy_bought_before_charging , timestamp_starting_to_charge , timestamp_ending_to_charge
177
+ )
178
+ self .log .info (f"Bought { energy_bought } to charge the battery" )
179
+
180
+ self ._write_energy_buy_statistics_to_file (
181
+ timestamp_starting_to_charge , timestamp_ending_to_charge , energy_bought
182
+ )
183
+
170
184
return next_price_minimum
171
185
172
186
def _charge_inverter (self , target_state_of_charge : int ) -> None :
@@ -180,10 +194,6 @@ def _charge_inverter(self, target_state_of_charge: int) -> None:
180
194
charging_progress_check_interval = timedelta (minutes = 5 )
181
195
dry_run = EnvironmentVariableGetter .get (name_of_variable = "DRY_RUN" , default_value = True )
182
196
183
- energy_bought_before_charging = self .sems_portal_api_handler .get_energy_buy ()
184
- date_starting_to_charge = date .today ()
185
- self .log .debug (f"The amount of energy bought before charging is { energy_bought_before_charging } " )
186
-
187
197
self .log .info ("Starting to charge" )
188
198
self .inverter .set_operation_mode (OperationMode .ECO_CHARGE )
189
199
@@ -219,15 +229,13 @@ def _charge_inverter(self, target_state_of_charge: int) -> None:
219
229
f"Charging is still ongoing (current: { current_state_of_charge } %, target: >= { target_state_of_charge } %) --> Waiting for another { charging_progress_check_interval } ..."
220
230
)
221
231
222
- energy_bought = self ._calculate_amount_of_energy_bought (energy_bought_before_charging , date_starting_to_charge )
223
- self .log .info (f"Bought { energy_bought } to charge the battery" )
224
-
225
232
def _calculate_amount_of_energy_bought (
226
- self , energy_bought_before_charging : EnergyAmount , date_starting_to_charge : date
233
+ self ,
234
+ energy_bought_before_charging : EnergyAmount ,
235
+ timestamp_starting_to_charge : datetime ,
236
+ timestamp_ending_to_charge : datetime ,
227
237
) -> EnergyAmount :
228
- date_ending_to_charge = date .today ()
229
-
230
- if date_starting_to_charge == date_ending_to_charge :
238
+ if timestamp_starting_to_charge .date () == timestamp_ending_to_charge .date ():
231
239
energy_bought_today_after_charging = self .sems_portal_api_handler .get_energy_buy ()
232
240
self .log .debug (
233
241
f"It is till the same day since starting to charge, the amount of energy bought after charging is { energy_bought_today_after_charging } "
@@ -240,3 +248,12 @@ def _calculate_amount_of_energy_bought(
240
248
f"It is the next day since starting to charge, the amount of energy bought after charging (today) is { energy_bought_today_after_charging } , the amount of energy bought after charging (yesterday) is { energy_bought_yesterday } "
241
249
)
242
250
return energy_bought_today_after_charging + energy_bought_yesterday
251
+
252
+ def _write_energy_buy_statistics_to_file (
253
+ self , timestamp_starting_to_charge : datetime , timestamp_ending_to_charge : datetime , energy_bought : EnergyAmount
254
+ ) -> None :
255
+ filename = super ().directory_of_logs + "/energy_buy_prod.log"
256
+ with open (filename , "a" ) as f :
257
+ f .write (
258
+ f"{ timestamp_starting_to_charge .isoformat ()} \t { timestamp_ending_to_charge .isoformat ()} \t { energy_bought .watt_hours } \n "
259
+ )
0 commit comments