Skip to content

Commit cba93d3

Browse files
committed
Update BatteryPool.soc to stream Sample[Percentage] values
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 1205e74 commit cba93d3

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from ...microgrid import connection_manager
1414
from ...microgrid.component import ComponentCategory, ComponentMetricId, InverterType
15-
from ...timeseries import Energy, Quantity, Sample
15+
from ...timeseries import Energy, Percentage, Sample
1616
from ._component_metrics import ComponentMetricsData
1717
from ._result_types import Bound, PowerMetrics
1818

@@ -59,7 +59,7 @@ def battery_inverter_mapping(batteries: Iterable[int]) -> dict[int, int]:
5959

6060
# Formula output types class have no common interface
6161
# Print all possible types here.
62-
T = TypeVar("T", Sample[Quantity], Sample[Energy], PowerMetrics)
62+
T = TypeVar("T", Sample[Percentage], Sample[Energy], PowerMetrics)
6363

6464

6565
class MetricCalculator(ABC, Generic[T]):
@@ -234,7 +234,7 @@ def calculate(
234234
)
235235

236236

237-
class SoCCalculator(MetricCalculator[Sample[Quantity]]):
237+
class SoCCalculator(MetricCalculator[Sample[Percentage]]):
238238
"""Define how to calculate SoC metrics."""
239239

240240
def __init__(self, batteries: Set[int]) -> None:
@@ -283,7 +283,7 @@ def calculate(
283283
self,
284284
metrics_data: dict[int, ComponentMetricsData],
285285
working_batteries: set[int],
286-
) -> Sample[Quantity] | None:
286+
) -> Sample[Percentage] | None:
287287
"""Aggregate the metrics_data and calculate high level metric.
288288
289289
Missing components will be ignored. Formula will be calculated for all
@@ -349,11 +349,11 @@ def calculate(
349349
if total_capacity_x100 == 0:
350350
return Sample(
351351
timestamp=timestamp,
352-
value=Quantity(0.0),
352+
value=Percentage.from_percent(0.0),
353353
)
354354
return Sample(
355355
timestamp=timestamp,
356-
value=Quantity(used_capacity_x100 / total_capacity_x100),
356+
value=Percentage.from_percent(used_capacity_x100 / total_capacity_x100),
357357
)
358358

359359

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
from ...actor.power_distributing.result import Result
2121
from ...microgrid import connection_manager
2222
from ...microgrid.component import ComponentCategory
23-
from ...timeseries import Quantity, Sample
23+
from ...timeseries import Sample
2424
from .._formula_engine import FormulaEngine, FormulaEnginePool
2525
from .._formula_engine._formula_generators import (
2626
BatteryPowerFormula,
2727
FormulaGeneratorConfig,
2828
FormulaType,
2929
)
30-
from .._quantities import Energy, Power
30+
from .._quantities import Energy, Percentage, Power
3131
from ._methods import MetricAggregator, SendOnUpdate
3232
from ._metric_calculator import CapacityCalculator, PowerBoundsCalculator, SoCCalculator
3333
from ._result_types import PowerMetrics
@@ -330,7 +330,7 @@ def consumption_power(self) -> FormulaEngine[Power]:
330330
return engine
331331

332332
@property
333-
def soc(self) -> MetricAggregator[Sample[Quantity]]:
333+
def soc(self) -> MetricAggregator[Sample[Percentage]]:
334334
"""Fetch the normalized average weighted-by-capacity SoC values for the pool.
335335
336336
The values are normalized to the 0-100% range.

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from frequenz.sdk.actor import ResamplerConfig
2525
from frequenz.sdk.actor.power_distributing import BatteryStatus
2626
from frequenz.sdk.microgrid.component import ComponentCategory
27-
from frequenz.sdk.timeseries import Energy, Power, Quantity, Sample
27+
from frequenz.sdk.timeseries import Energy, Percentage, Power, Sample
2828
from frequenz.sdk.timeseries.battery_pool import BatteryPool, Bound, PowerMetrics
2929
from frequenz.sdk.timeseries.battery_pool._metric_calculator import (
3030
battery_inverter_mapping,
@@ -688,29 +688,29 @@ async def run_soc_test(setup_args: SetupArgs) -> None:
688688
now = datetime.now(tz=timezone.utc)
689689
expected = Sample(
690690
timestamp=now,
691-
value=Quantity(10.0),
691+
value=Percentage.from_percent(10.0),
692692
)
693693
compare_messages(msg, expected, WAIT_FOR_COMPONENT_DATA_SEC + 0.2)
694694

695695
batteries_in_pool = list(battery_pool.battery_ids)
696-
scenarios: list[Scenario[Sample[Quantity]]] = [
696+
scenarios: list[Scenario[Sample[Percentage]]] = [
697697
Scenario(
698698
batteries_in_pool[0],
699699
{"capacity": 150, "soc": 10},
700-
Sample(now, Quantity(2.5)),
700+
Sample(now, Percentage.from_percent(2.5)),
701701
),
702702
Scenario(
703703
batteries_in_pool[0],
704704
{
705705
"soc_lower_bound": 0.0,
706706
},
707-
Sample(now, Quantity(12.727272727272727)),
707+
Sample(now, Percentage.from_percent(12.727272727272727)),
708708
),
709709
# If NaN, then not include that battery in the metric.
710710
Scenario(
711711
batteries_in_pool[0],
712712
{"soc_upper_bound": float("NaN")},
713-
Sample(now, Quantity(10.0)),
713+
Sample(now, Percentage.from_percent(10.0)),
714714
),
715715
# All batteries are sending NaN, can't calculate SoC so we should send None
716716
Scenario(
@@ -721,7 +721,7 @@ async def run_soc_test(setup_args: SetupArgs) -> None:
721721
Scenario(
722722
batteries_in_pool[1],
723723
{"soc": 30},
724-
Sample(now, Quantity(10.0)),
724+
Sample(now, Percentage.from_percent(10.0)),
725725
),
726726
# Final metric didn't change, so nothing should be received.
727727
Scenario(
@@ -734,17 +734,17 @@ async def run_soc_test(setup_args: SetupArgs) -> None:
734734
Scenario(
735735
batteries_in_pool[1],
736736
{"capacity": 0},
737-
Sample(now, Quantity(0.0)),
737+
Sample(now, Percentage.from_percent(0.0)),
738738
),
739739
Scenario(
740740
batteries_in_pool[0],
741741
{"capacity": 50, "soc": 55.0},
742-
Sample(now, Quantity(50.0)),
742+
Sample(now, Percentage.from_percent(50.0)),
743743
),
744744
Scenario(
745745
batteries_in_pool[1],
746746
{"capacity": 150},
747-
Sample(now, Quantity(25.0)),
747+
Sample(now, Percentage.from_percent(25.0)),
748748
),
749749
]
750750

@@ -757,15 +757,15 @@ async def run_soc_test(setup_args: SetupArgs) -> None:
757757
all_batteries=all_batteries,
758758
batteries_in_pool=batteries_in_pool,
759759
waiting_time_sec=waiting_time_sec,
760-
all_pool_result=Sample(now, Quantity(25.0)),
761-
only_first_battery_result=Sample(now, Quantity(50.0)),
760+
all_pool_result=Sample(now, Percentage.from_percent(25.0)),
761+
only_first_battery_result=Sample(now, Percentage.from_percent(50.0)),
762762
)
763763

764764
# One battery stopped sending data.
765765
await streamer.stop_streaming(batteries_in_pool[1])
766766
await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2)
767767
msg = await asyncio.wait_for(receiver.receive(), timeout=waiting_time_sec)
768-
compare_messages(msg, Sample(now, Quantity(50.0)), 0.2)
768+
compare_messages(msg, Sample(now, Percentage.from_percent(50.0)), 0.2)
769769

770770
# All batteries stopped sending data.
771771
await streamer.stop_streaming(batteries_in_pool[0])
@@ -777,7 +777,7 @@ async def run_soc_test(setup_args: SetupArgs) -> None:
777777
latest_data = streamer.get_current_component_data(batteries_in_pool[0])
778778
streamer.start_streaming(latest_data, sampling_rate=0.1)
779779
msg = await asyncio.wait_for(receiver.receive(), timeout=waiting_time_sec)
780-
compare_messages(msg, Sample(now, Quantity(50.0)), 0.2)
780+
compare_messages(msg, Sample(now, Percentage.from_percent(50.0)), 0.2)
781781

782782

783783
async def run_power_bounds_test( # pylint: disable=too-many-locals

0 commit comments

Comments
 (0)