Skip to content

Commit 70c7a9e

Browse files
committed
Return BatteryPoolWrapper from the data pipeline instead of BatteryPool
This allows users to get a custom object that will keep track of the user's source_id and priorities, without having to duplicate the formula engine and metric calculators. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent d4f9704 commit 70c7a9e

File tree

6 files changed

+38
-24
lines changed

6 files changed

+38
-24
lines changed

benchmarks/power_distribution/power_distributor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def send_requests(batteries: set[int], request_num: int) -> list[Result]:
5151
List of the results from the PowerDistributingActor.
5252
"""
5353
battery_pool = microgrid.battery_pool(batteries)
54-
results_rx = battery_pool.power_bounds().new_receiver()
54+
results_rx = battery_pool.power_bounds.new_receiver()
5555
result: list[Any] = []
5656
for _ in range(request_num):
5757
await battery_pool.set_power(Power(float(random.randrange(100000, 1000000))))

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
Request,
3838
Result,
3939
)
40-
from ..timeseries.battery_pool import BatteryPool
40+
from ..timeseries.battery_pool import BatteryPool, BatteryPoolWrapper
4141
from ..timeseries.ev_charger_pool import EVChargerPool
4242
from ..timeseries.logical_meter import LogicalMeter
4343

@@ -185,20 +185,26 @@ def ev_charger_pool(
185185
def battery_pool(
186186
self,
187187
battery_ids: abc.Set[int] | None = None,
188-
) -> BatteryPool:
188+
source_id: str | None = None,
189+
priority: int = 0,
190+
) -> BatteryPoolWrapper:
189191
"""Return the corresponding BatteryPool instance for the given ids.
190192
191193
If a BatteryPool instance for the given ids doesn't exist, a new one is created
192194
and returned.
193195
196+
The BatteryPool is wrapped in a new `BatteryPoolWrapper` instance each time.
197+
194198
Args:
195199
battery_ids: Optional set of IDs of batteries to be managed by the
196200
BatteryPool.
201+
source_id: The source ID to use for the requests made with this instance.
202+
priority: The priority of the actor making the call.
197203
198204
Returns:
199-
A BatteryPool instance.
205+
A BatteryPoolWrapper instance.
200206
"""
201-
from ..timeseries.battery_pool import BatteryPool
207+
from ..timeseries.battery_pool import BatteryPool, BatteryPoolWrapper
202208

203209
if not self._power_managing_actor:
204210
self._start_power_managing_actor()
@@ -225,7 +231,7 @@ def battery_pool(
225231
batteries_id=battery_ids,
226232
)
227233

228-
return self._battery_pools[key]
234+
return BatteryPoolWrapper(self._battery_pools[key], source_id, priority)
229235

230236
def _start_power_managing_actor(self) -> None:
231237
"""Start the power managing actor if it is not already running."""
@@ -415,21 +421,29 @@ def ev_charger_pool(ev_charger_ids: set[int] | None = None) -> EVChargerPool:
415421
return _get().ev_charger_pool(ev_charger_ids)
416422

417423

418-
def battery_pool(battery_ids: abc.Set[int] | None = None) -> BatteryPool:
424+
def battery_pool(
425+
battery_ids: abc.Set[int] | None = None,
426+
source_id: str | None = None,
427+
priority: int = 0,
428+
) -> BatteryPoolWrapper:
419429
"""Return the corresponding BatteryPool instance for the given ids.
420430
421431
If a BatteryPool instance for the given ids doesn't exist, a new one is
422432
created and returned.
423433
434+
The BatteryPool is wrapped in a new `BatteryPoolWrapper` instance each time.
435+
424436
Args:
425437
battery_ids: Optional set of IDs of batteries to be managed by the
426438
BatteryPool. If not specified, all batteries available in the
427439
component graph are used.
440+
source_id: The source ID to use for the requests made with this instance.
441+
priority: The priority of the actor making the call.
428442
429443
Returns:
430444
A BatteryPool instance.
431445
"""
432-
return _get().battery_pool(battery_ids)
446+
return _get().battery_pool(battery_ids, source_id, priority)
433447

434448

435449
def _get() -> _DataPipeline:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
"""Manage a pool of batteries."""
55

6+
from ._battery_pool_wrapper import BatteryPoolWrapper
67
from ._result_types import PowerMetrics
78
from .battery_pool import BatteryPool
89

910
__all__ = [
1011
"BatteryPool",
12+
"BatteryPoolWrapper",
1113
"PowerMetrics",
1214
]

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
Sample,
3737
Temperature,
3838
)
39-
from frequenz.sdk.timeseries.battery_pool import BatteryPool, PowerMetrics
39+
from frequenz.sdk.timeseries.battery_pool import BatteryPoolWrapper, PowerMetrics
4040
from frequenz.sdk.timeseries.battery_pool._metric_calculator import (
4141
battery_inverter_mapping,
4242
)
@@ -90,7 +90,7 @@ def get_components(
9090
class SetupArgs:
9191
"""Setup arguments needed to run tests."""
9292

93-
battery_pool: BatteryPool
93+
battery_pool: BatteryPoolWrapper
9494
"""Battery pool that should be tested."""
9595

9696
min_update_interval: float
@@ -168,7 +168,7 @@ async def setup_all_batteries(mocker: MockerFixture) -> AsyncIterator[SetupArgs]
168168
await asyncio.gather(
169169
*[
170170
microgrid._data_pipeline._DATA_PIPELINE._stop(),
171-
battery_pool.stop(),
171+
battery_pool._battery_pool.stop(),
172172
streamer.stop(),
173173
]
174174
)
@@ -222,7 +222,7 @@ async def setup_batteries_pool(mocker: MockerFixture) -> AsyncIterator[SetupArgs
222222
await asyncio.gather(
223223
*[
224224
microgrid._data_pipeline._DATA_PIPELINE._stop(),
225-
battery_pool.stop(),
225+
battery_pool._battery_pool.stop(),
226226
streamer.stop(),
227227
]
228228
)

tests/timeseries/_battery_pool/test_battery_pool_control_methods.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ async def test_case_1(
185185
#
186186
# It will be replaced by a reporting streaming from the PowerManager in a
187187
# subsequent commit.
188-
bounds_rx = battery_pool.power_bounds().new_receiver()
188+
bounds_rx = battery_pool.power_bounds.new_receiver()
189189

190190
self._assert_report(
191191
await bounds_rx.receive(), power=None, lower=-4000.0, upper=4000.0
@@ -279,9 +279,9 @@ async def test_case_2(self, mocks: Mocks, mocker: MockerFixture) -> None:
279279
await self._init_data_for_inverters(mocks)
280280

281281
battery_pool_1 = microgrid.battery_pool(set(mocks.microgrid.battery_ids[:2]))
282-
bounds_1_rx = battery_pool_1.power_bounds().new_receiver()
282+
bounds_1_rx = battery_pool_1.power_bounds.new_receiver()
283283
battery_pool_2 = microgrid.battery_pool(set(mocks.microgrid.battery_ids[2:]))
284-
bounds_2_rx = battery_pool_2.power_bounds().new_receiver()
284+
bounds_2_rx = battery_pool_2.power_bounds.new_receiver()
285285

286286
self._assert_report(
287287
await bounds_1_rx.receive(), power=None, lower=-2000.0, upper=2000.0
@@ -324,10 +324,10 @@ async def test_case_3(self, mocks: Mocks, mocker: MockerFixture) -> None:
324324
await self._init_data_for_batteries(mocks)
325325
await self._init_data_for_inverters(mocks)
326326

327-
battery_pool_1 = microgrid.battery_pool()
328-
bounds_1_rx = battery_pool_1.power_bounds(2).new_receiver()
329-
battery_pool_2 = microgrid.battery_pool()
330-
bounds_2_rx = battery_pool_2.power_bounds(1).new_receiver()
327+
battery_pool_1 = microgrid.battery_pool(priority=2)
328+
bounds_1_rx = battery_pool_1.power_bounds.new_receiver()
329+
battery_pool_2 = microgrid.battery_pool(priority=1)
330+
bounds_2_rx = battery_pool_2.power_bounds.new_receiver()
331331

332332
self._assert_report(
333333
await bounds_1_rx.receive(), power=None, lower=-4000.0, upper=4000.0
@@ -337,7 +337,6 @@ async def test_case_3(self, mocks: Mocks, mocker: MockerFixture) -> None:
337337
)
338338
await battery_pool_1.set_power(
339339
Power.from_watts(-1000.0),
340-
_priority=2,
341340
_bounds=(Power.from_watts(-1000.0), Power.from_watts(0.0)),
342341
)
343342
self._assert_report(
@@ -356,7 +355,6 @@ async def test_case_3(self, mocks: Mocks, mocker: MockerFixture) -> None:
356355

357356
await battery_pool_2.set_power(
358357
Power.from_watts(0.0),
359-
_priority=1,
360358
_bounds=(Power.from_watts(0.0), Power.from_watts(1000.0)),
361359
)
362360
self._assert_report(

tests/timeseries/_formula_engine/test_formula_composition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_formula_composition( # pylint: disable=too-many-locals
9494

9595
await mockgrid.cleanup()
9696
await engine._stop() # pylint: disable=protected-access
97-
await battery_pool.stop()
97+
await battery_pool._battery_pool.stop() # pylint: disable=protected-access
9898
await logical_meter.stop()
9999

100100
async def test_formula_composition_missing_pv(self, mocker: MockerFixture) -> None:
@@ -130,7 +130,7 @@ async def test_formula_composition_missing_pv(self, mocker: MockerFixture) -> No
130130

131131
await mockgrid.cleanup()
132132
await engine._stop() # pylint: disable=protected-access
133-
await battery_pool.stop()
133+
await battery_pool._battery_pool.stop() # pylint: disable=protected-access
134134
await logical_meter.stop()
135135

136136
assert count == 10
@@ -165,7 +165,7 @@ async def test_formula_composition_missing_bat(self, mocker: MockerFixture) -> N
165165

166166
await mockgrid.cleanup()
167167
await engine._stop() # pylint: disable=protected-access
168-
await battery_pool.stop()
168+
await battery_pool._battery_pool.stop() # pylint: disable=protected-access
169169
await logical_meter.stop()
170170

171171
assert count == 10

0 commit comments

Comments
 (0)