Skip to content

Commit c25e44c

Browse files
committed
Minor consmetics and formatting
1 parent 3f19d04 commit c25e44c

5 files changed

+40
-27
lines changed

source/energy_classes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __gt__(self, other: EnergyRate) -> bool:
8282
return self.rate > other.rate
8383

8484
def format_maximum_charging_duration(self) -> str:
85-
charging_duration_in_hours = self.maximum_charging_duration.total_seconds() // 3600
85+
charging_duration_in_hours = int(self.maximum_charging_duration.total_seconds() // 3600)
8686
if charging_duration_in_hours == 1:
8787
return "1 hour"
8888
return f"{charging_duration_in_hours} hours"

source/inverter_charge_controller.py

+29-20
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def _start(self) -> None:
5858
- Unexpected exceptions: Exit with status code 1
5959
6060
This method indefinitely repeats a sequence of operations until an unrecoverable error occurs.
61-
On the first iteration, it performs a special initialization operation to determine the next scheduled execution time.
61+
On the first iteration, it performs a special initialization operation to determine the next scheduled execution
62+
time.
6263
On subsequent iterations, it performs a standard operation to determine the next scheduled execution time.
6364
6465
Raises:
@@ -82,7 +83,7 @@ def _start(self) -> None:
8283
time_to_sleep_to += timedelta(days=1)
8384
self.log.info(
8485
f"The price minimum {next_price_minimum} has to re-checked "
85-
+ f"--> Waiting until {time_to_sleep_to}..."
86+
f"--> Waiting until {time_to_sleep_to}..."
8687
)
8788
pause.until(time_to_sleep_to)
8889
self.log.info("Waking up since the the price minimum has to re-checked")
@@ -127,10 +128,7 @@ def _do_iteration(self, current_energy_rate: EnergyRate) -> EnergyRate:
127128
timestamp_now = TimeHandler.get_time()
128129

129130
next_price_minimum = self.tibber_api_handler.get_next_price_minimum()
130-
self.log.info(
131-
f"The next price minimum is at {next_price_minimum} and it is feasible to charge for a "
132-
+ f"maximum of {current_energy_rate.format_maximum_charging_duration()}"
133-
)
131+
self.log.info(f"The next price minimum is at {next_price_minimum}")
134132

135133
if next_price_minimum.rate > current_energy_rate.rate:
136134
# Information is unused at the moment
@@ -140,12 +138,14 @@ def _do_iteration(self, current_energy_rate: EnergyRate) -> EnergyRate:
140138
timestamp_now, next_price_minimum.timestamp
141139
)
142140
self.log.info(
143-
f"The expected energy harvested by the sun till the next price minimum is {expected_power_harvested_till_next_minimum}"
141+
f"The expected energy harvested by the sun till the next price minimum is "
142+
f"{expected_power_harvested_till_next_minimum}"
144143
)
145144

146145
if self.absence_handler.check_for_current_absence():
147146
self.log.info(
148-
"Currently there is an absence, using the power consumption configured in the environment as the basis for calculation"
147+
"Currently there is an absence, using the power consumption configured in the environment as the "
148+
"basis for calculation"
149149
)
150150
expected_energy_usage_till_next_minimum = self.absence_handler.calculate_power_usage_for_absence(
151151
timestamp_now, next_price_minimum.timestamp
@@ -162,8 +162,8 @@ def _do_iteration(self, current_energy_rate: EnergyRate) -> EnergyRate:
162162
power_usage_increase_factor = 20
163163
self.log.info(
164164
"The price minimum has to be re-checked since it is at the end of a day and the price rates for "
165-
+ "tomorrow are unavailable --> The expected power usage "
166-
+ f"({expected_energy_usage_till_next_minimum}) is increased by {power_usage_increase_factor} %"
165+
"tomorrow are unavailable --> The expected power usage "
166+
f"({expected_energy_usage_till_next_minimum}) is increased by {power_usage_increase_factor} %"
167167
)
168168
expected_energy_usage_till_next_minimum += expected_energy_usage_till_next_minimum * (
169169
power_usage_increase_factor / 100
@@ -184,7 +184,8 @@ def _do_iteration(self, current_energy_rate: EnergyRate) -> EnergyRate:
184184
self.inverter.calculate_energy_saved_in_battery_from_state_of_charge(target_min_state_of_charge)
185185
)
186186
self.log.info(
187-
f"The battery shall contain {energy_to_be_in_battery_when_reaching_next_minimum} ({target_min_state_of_charge} %) when reaching the next minimum"
187+
f"The battery shall contain {energy_to_be_in_battery_when_reaching_next_minimum} "
188+
f"({target_min_state_of_charge} %) when reaching the next minimum"
188189
)
189190

190191
summary_of_energy_vales = {
@@ -230,7 +231,8 @@ def _do_iteration(self, current_energy_rate: EnergyRate) -> EnergyRate:
230231

231232
duration_to_wait_for_semsportal_update = timedelta(minutes=10)
232233
self.log.info(
233-
f"Sleeping for {duration_to_wait_for_semsportal_update} to let the SEMS Portal update its power consumption data..."
234+
f"Sleeping for {duration_to_wait_for_semsportal_update} to let the SEMS Portal update its power "
235+
"consumption data..."
234236
)
235237
pause.seconds(duration_to_wait_for_semsportal_update.total_seconds())
236238

@@ -269,20 +271,23 @@ def _charge_inverter(self, target_state_of_charge: int, maximum_charging_duratio
269271

270272
self.log.info(
271273
f"Set the inverter to charge, the target state of charge is {target_state_of_charge} %. "
272-
+ f"End of charging is {maximum_end_charging_time.strftime('%H:%M:%S')} at the latest. "
273-
+ f"Checking the charging progress every {charging_progress_check_interval}..."
274+
f"End of charging is {maximum_end_charging_time.strftime('%H:%M:%S')} at the latest. "
275+
f"Checking the charging progress every {charging_progress_check_interval}..."
274276
)
275277

276278
error_counter = 0
277279
while True:
278280
if error_counter == 3:
279281
self.log.error(
280-
f"An error occurred {error_counter} times while trying to get the current state of charge --> Stopping the charging process"
282+
f"An error occurred {error_counter} times while trying to get the current state of charge"
283+
f"--> Stopping the charging process"
281284
)
282285
# Can't set the mode of the inverter as it is unresponsive
283286
break
284287

285-
pause.seconds(charging_progress_check_interval.total_seconds())
288+
# Account for program execution times, this way the check happens at 05:00 and does not add up delays
289+
# (minor cosmetics)
290+
pause.seconds(charging_progress_check_interval.total_seconds() - TimeHandler.get_time().second)
286291

287292
try:
288293
if self.inverter.get_operation_mode() != OperationMode.ECO_CHARGE:
@@ -310,13 +315,14 @@ def _charge_inverter(self, target_state_of_charge: int, maximum_charging_duratio
310315
if TimeHandler.get_time() > maximum_end_charging_time:
311316
self.log.info(
312317
f"The maximum end charging time of {maximum_end_charging_time} has been reached "
313-
+ f"--> Stopping the charging process. The battery is at {current_state_of_charge} %"
318+
f"--> Stopping the charging process. The battery is at {current_state_of_charge} %"
314319
)
315320
self.inverter.set_operation_mode(OperationMode.GENERAL)
316321
break
317322

318323
self.log.debug(
319-
f"Charging is still ongoing (current: {current_state_of_charge} %, target: >= {target_state_of_charge} %) --> Waiting for another {charging_progress_check_interval}..."
324+
f"Charging is still ongoing (current: {current_state_of_charge} %, target: >= {target_state_of_charge} "
325+
f"%) --> Waiting for another {charging_progress_check_interval}..."
320326
)
321327

322328
def _calculate_amount_of_energy_bought(
@@ -343,14 +349,17 @@ def _calculate_amount_of_energy_bought(
343349
if timestamp_starting_to_charge.date() == timestamp_ending_to_charge.date():
344350
energy_bought_today_after_charging = self.sems_portal_api_handler.get_energy_buy()
345351
self.log.debug(
346-
f"It is till the same day since starting to charge, the amount of energy bought after charging is {energy_bought_today_after_charging}"
352+
f"It is till the same day since starting to charge, the amount of energy bought after charging is "
353+
f"{energy_bought_today_after_charging}"
347354
)
348355
return energy_bought_today_after_charging - energy_bought_before_charging
349356

350357
energy_bought_today_after_charging = self.sems_portal_api_handler.get_energy_buy()
351358
energy_bought_yesterday = self.sems_portal_api_handler.get_energy_buy(1) - energy_bought_before_charging
352359
self.log.debug(
353-
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}"
360+
f"It is the next day since starting to charge, the amount of energy bought after charging (today) is "
361+
f"{energy_bought_today_after_charging}, the amount of energy bought after charging (yesterday) is "
362+
f"{energy_bought_yesterday}"
354363
)
355364
return energy_bought_today_after_charging + energy_bought_yesterday
356365

source/sems_portal_api_handler.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ def get_energy_buy(self, days_in_past: int = 0) -> EnergyAmount:
186186
Returns:
187187
An instance of EnergyAmount representing the energy bought.
188188
"""
189-
self.log.debug(f"Determining the amount of energy bought {days_in_past} days ago")
190-
191-
self.login()
189+
if days_in_past == 0:
190+
timeframe_as_string = "today (until now)"
191+
elif days_in_past == 1:
192+
timeframe_as_string = "yesterday"
193+
else:
194+
timeframe_as_string = f"{days_in_past} days ago"
195+
self.log.debug(f"Determining the amount of energy bought {timeframe_as_string}")
192196

193197
api_response = self._retrieve_energy_consumption_data()
194198
lines = api_response["data"]["lines"]

source/sun_forecast_handler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def _calculate_energy_produced_in_timeframe(
106106
)
107107
self.log.trace(
108108
f"There is an estimated power generation of {power_in_timeslot} "
109-
+ f"from {max(timeslot_start, timestamp_start)} to {min(timeslot_end, timestamp_end)}, thus producing "
110-
+ f"{energy_produced_in_timeslot} in that timeframe"
109+
f"from {max(timeslot_start, timestamp_start)} to {min(timeslot_end, timestamp_end)}, thus producing "
110+
f"{energy_produced_in_timeslot} in that timeframe"
111111
)
112112
expected_solar_output += energy_produced_in_timeslot
113113

source/tibber_api_handler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def get_next_price_minimum(self, first_iteration: bool = False) -> EnergyRate:
6262
):
6363
self.log.info(
6464
"This is the first time finding the minimum prices and the prices are currently on a decline. "
65-
+ "Thus the next price minimum is considered (instead of the one after the first maximum)."
65+
"Thus the next price minimum is considered (instead of the one after the first maximum)."
6666
)
6767
energy_rates_between_first_and_second_maximum = self._find_energy_rates_till_first_maximum(
6868
upcoming_energy_rates

0 commit comments

Comments
 (0)