Skip to content

Commit acc29db

Browse files
committed
Extract methods for calculating target soc when the next price minimum is reachable
1 parent 93b0e82 commit acc29db

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

source/inverter_charge_controller.py

+52-31
Original file line numberDiff line numberDiff line change
@@ -365,41 +365,18 @@ def _coordinate_charging_next_price_minimum_is_reachable(
365365
next price minimum.
366366
"""
367367
if current_energy_rate >= self.next_price_minimum:
368-
self.log.info(
369-
f"The price of the current minimum ({current_energy_rate.rate} ct/kWh) is higher than the one of "
370-
f"the upcoming minimum ({self.next_price_minimum.rate} ct/kWh) "
371-
"--> Will only charge the battery to reach the next price minimum"
372-
)
373-
if minimum_of_soc_until_next_price_minimum > self.target_min_soc:
374-
self.log.info(
375-
"The expected minimum state of charge until the next price minimum without additional charging "
376-
f"{minimum_of_soc_until_next_price_minimum} is higher than the target minimum state of charge "
377-
f"{self.target_min_soc} --> There is no need to charge"
378-
)
379-
return
380-
381-
self.log.debug(
382-
f"Formula for calculating the target state of charge: current {current_state_of_charge} + "
383-
f"target minimum state of charge ({self.target_min_soc}) - minimum state of charge until next price "
384-
f"minimum ({minimum_of_soc_until_next_price_minimum})"
385-
)
386368
charging_target_soc = (
387-
current_state_of_charge + self.target_min_soc - minimum_of_soc_until_next_price_minimum
369+
self._calculate_target_soc_next_price_minimum_is_reachable_and_current_minimum_is_higher_than_next_one(
370+
current_energy_rate, current_state_of_charge, minimum_of_soc_until_next_price_minimum
371+
)
388372
)
389-
373+
if charging_target_soc is None:
374+
return
390375
else:
391-
self.log.info(
392-
f"The price of the upcoming minimum ({self.next_price_minimum.rate} ct/kWh) is higher than the one of "
393-
f"the current minimum ({current_energy_rate.rate} ct/kWh) "
394-
"--> Will charge as much as possible without wasting any energy of the sun"
395-
)
396-
self.log.debug(
397-
f"Formula for calculating the target state of charge: current ({current_state_of_charge}) + "
398-
f"target maximum state of charge ({self.target_max_soc}) - "
399-
f"maximum state of charge until next price minimum ({maximum_of_soc_until_next_price_minimum})"
400-
)
401376
charging_target_soc = (
402-
current_state_of_charge + self.target_max_soc - maximum_of_soc_until_next_price_minimum
377+
self._calculate_target_soc_next_price_minimum_is_reachable_and_current_minimum_is_lower_than_next_one(
378+
current_energy_rate, current_state_of_charge, maximum_of_soc_until_next_price_minimum
379+
)
403380
)
404381

405382
self.log.info(f"The calculated target state of charge is {charging_target_soc}")
@@ -414,6 +391,50 @@ def _coordinate_charging_next_price_minimum_is_reachable(
414391
with self._protocol_amount_of_energy_bought():
415392
self._charge_inverter(charging_target_soc)
416393

394+
def _calculate_target_soc_next_price_minimum_is_reachable_and_current_minimum_is_higher_than_next_one(
395+
self,
396+
current_energy_rate: EnergyRate,
397+
current_state_of_charge: StateOfCharge,
398+
minimum_of_soc_until_next_price_minimum: StateOfCharge,
399+
) -> Optional[StateOfCharge]:
400+
self.log.info(
401+
f"The price of the current minimum ({current_energy_rate.rate} ct/kWh) is higher than the one of "
402+
f"the upcoming minimum ({self.next_price_minimum.rate} ct/kWh) "
403+
"--> Will only charge the battery to reach the next price minimum"
404+
)
405+
if minimum_of_soc_until_next_price_minimum > self.target_min_soc:
406+
self.log.info(
407+
"The expected minimum state of charge until the next price minimum without additional charging "
408+
f"{minimum_of_soc_until_next_price_minimum} is higher than the target minimum state of charge "
409+
f"{self.target_min_soc} --> There is no need to charge"
410+
)
411+
return None
412+
413+
self.log.debug(
414+
f"Formula for calculating the target state of charge: current {current_state_of_charge} + "
415+
f"target minimum state of charge ({self.target_min_soc}) - minimum state of charge until next price "
416+
f"minimum ({minimum_of_soc_until_next_price_minimum})"
417+
)
418+
return current_state_of_charge + self.target_min_soc - minimum_of_soc_until_next_price_minimum
419+
420+
def _calculate_target_soc_next_price_minimum_is_reachable_and_current_minimum_is_lower_than_next_one(
421+
self,
422+
current_energy_rate: EnergyRate,
423+
current_state_of_charge: StateOfCharge,
424+
maximum_of_soc_until_next_price_minimum: StateOfCharge,
425+
) -> StateOfCharge:
426+
self.log.info(
427+
f"The price of the upcoming minimum ({self.next_price_minimum.rate} ct/kWh) is higher than the one of "
428+
f"the current minimum ({current_energy_rate.rate} ct/kWh) "
429+
"--> Will charge as much as possible without wasting any energy of the sun"
430+
)
431+
self.log.debug(
432+
f"Formula for calculating the target state of charge: current ({current_state_of_charge}) + "
433+
f"target maximum state of charge ({self.target_max_soc}) - "
434+
f"maximum state of charge until next price minimum ({maximum_of_soc_until_next_price_minimum})"
435+
)
436+
return current_state_of_charge + self.target_max_soc - maximum_of_soc_until_next_price_minimum
437+
417438
def _charge_inverter(self, target_state_of_charge: StateOfCharge) -> None:
418439
"""
419440
Charges the inverter battery to the target state of charge within a specified maximum charging duration.

0 commit comments

Comments
 (0)