Skip to content

Commit deb1389

Browse files
committed
Cap StateOfCharge at battery capacity
1 parent d6fd9d4 commit deb1389

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

source/energy_classes.py

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime, timedelta
55

66
from environment_variable_getter import EnvironmentVariableGetter
7+
from logger import LoggerMixin
78

89

910
@dataclass
@@ -82,6 +83,7 @@ def format_maximum_charging_duration(self) -> str:
8283
return f"{charging_duration_in_hours} hours"
8384

8485

86+
soc_logger = LoggerMixin("StateOfCharge")
8587
battery_capacity = EnergyAmount(int(EnvironmentVariableGetter.get("INVERTER_BATTERY_CAPACITY")))
8688

8789

@@ -92,6 +94,13 @@ class StateOfCharge:
9294
def __repr__(self):
9395
return f"{self.in_percentage} % ({self.absolute})"
9496

97+
def __post_init__(self):
98+
if not self.absolute.watt_hours > battery_capacity.watt_hours:
99+
return
100+
101+
soc_logger.log.debug(f"Capping the state of charge at the battery capacity (would be {self.absolute})")
102+
self.absolute = EnergyAmount(battery_capacity.watt_hours)
103+
95104
def __add__(self, other: StateOfCharge) -> StateOfCharge:
96105
return StateOfCharge(self.absolute + other.absolute)
97106

0 commit comments

Comments
 (0)