Skip to content

Commit ea42350

Browse files
committed
Adjust BatteryPool's SoC to 100.0 when it isclose to 100.0
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent f52f791 commit ea42350

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/frequenz/sdk/timeseries/battery_pool/_metric_calculator.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66

77
import logging
8+
import math
89
from abc import ABC, abstractmethod
910
from collections.abc import Mapping, Set
1011
from datetime import datetime, timezone
1112
from typing import Generic, TypeVar
1213

1314
from ... import timeseries
15+
from ..._internal import _math
1416
from ...actor.power_distributing._component_managers._battery_manager import (
1517
_get_battery_inverter_mappings,
1618
)
@@ -406,15 +408,18 @@ def calculate(
406408
if timestamp == _MIN_TIMESTAMP:
407409
return Sample(datetime.now(tz=timezone.utc), None)
408410

411+
# When the calculated is close to 0.0 or 100.0, they are set to exactly 0.0 or
412+
# 100.0, to make full/empty checks using the == operator less error prone.
413+
pct = 0.0
409414
# To avoid zero division error
410-
if total_capacity_x100 == 0:
411-
return Sample(
412-
timestamp=timestamp,
413-
value=Percentage.from_percent(0.0),
414-
)
415+
if not _math.is_close_to_zero(total_capacity_x100):
416+
pct = used_capacity_x100 / total_capacity_x100
417+
if math.isclose(pct, 100.0):
418+
pct = 100.0
419+
415420
return Sample(
416421
timestamp=timestamp,
417-
value=Percentage.from_percent(used_capacity_x100 / total_capacity_x100),
422+
value=Percentage.from_percent(pct),
418423
)
419424

420425

0 commit comments

Comments
 (0)