Skip to content

Commit e4c9fe9

Browse files
authored
Code quality improvements in the DataPipeline and *Pool classes (#948)
Some more cleanup in preparation for adding support for incremental power actors in the `PowerManager`.
2 parents a31c845 + 4ecdc79 commit e4c9fe9

File tree

5 files changed

+101
-93
lines changed

5 files changed

+101
-93
lines changed

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ def producer(self) -> Producer:
182182
)
183183
return self._producer
184184

185+
def grid(self) -> Grid:
186+
"""Return the grid measuring point."""
187+
if self._grid is None:
188+
initialize_grid(
189+
channel_registry=self._channel_registry,
190+
resampler_subscription_sender=self._resampling_request_sender(),
191+
)
192+
self._grid = get_grid()
193+
194+
return self._grid
195+
185196
def ev_charger_pool(
186197
self,
187198
*,
@@ -250,7 +261,9 @@ def ev_charger_pool(
250261
)
251262
)
252263
return EVChargerPool(
253-
self._ev_charger_pool_reference_stores[ref_store_key], name, priority
264+
pool_ref_store=self._ev_charger_pool_reference_stores[ref_store_key],
265+
name=name,
266+
priority=priority,
254267
)
255268

256269
def pv_pool(
@@ -317,18 +330,11 @@ def pv_pool(
317330
component_ids=component_ids,
318331
)
319332

320-
return PVPool(self._pv_pool_reference_stores[ref_store_key], name, priority)
321-
322-
def grid(self) -> Grid:
323-
"""Return the grid measuring point."""
324-
if self._grid is None:
325-
initialize_grid(
326-
channel_registry=self._channel_registry,
327-
resampler_subscription_sender=self._resampling_request_sender(),
328-
)
329-
self._grid = get_grid()
330-
331-
return self._grid
333+
return PVPool(
334+
pool_ref_store=self._pv_pool_reference_stores[ref_store_key],
335+
name=name,
336+
priority=priority,
337+
)
332338

333339
def battery_pool(
334340
self,
@@ -400,7 +406,9 @@ def battery_pool(
400406
)
401407

402408
return BatteryPool(
403-
self._battery_pool_reference_stores[ref_store_key], name, priority
409+
pool_ref_store=self._battery_pool_reference_stores[ref_store_key],
410+
name=name,
411+
priority=priority,
404412
)
405413

406414
def _data_sourcing_request_sender(self) -> Sender[ComponentMetricRequest]:
@@ -518,8 +526,7 @@ def ev_charger_pool(
518526
propose different power values for the same set of EV chargers.
519527
520528
!!! note
521-
When specifying priority, bigger values indicate higher priority. The default
522-
priority is the lowest possible value.
529+
When specifying priority, bigger values indicate higher priority.
523530
524531
It is recommended to reuse the same instance of the `EVChargerPool` within the
525532
same actor, unless they are managing different sets of EV chargers.
@@ -558,8 +565,7 @@ def battery_pool(
558565
propose different power values for the same set of batteries.
559566
560567
!!! note
561-
When specifying priority, bigger values indicate higher priority. The default
562-
priority is the lowest possible value.
568+
When specifying priority, bigger values indicate higher priority.
563569
564570
It is recommended to reuse the same instance of the `BatteryPool` within the
565571
same actor, unless they are managing different sets of batteries.
@@ -598,8 +604,7 @@ def pv_pool(
598604
propose different power values for the same set of PV inverters.
599605
600606
!!! note
601-
When specifying priority, bigger values indicate higher priority. The default
602-
priority is the lowest possible value.
607+
When specifying priority, bigger values indicate higher priority.
603608
604609
It is recommended to reuse the same instance of the `PVPool` within the same
605610
actor, unless they are managing different sets of PV inverters.

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

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class BatteryPool:
5555

5656
def __init__(
5757
self,
58-
battery_pool_ref: BatteryPoolReferenceStore,
58+
*,
59+
pool_ref_store: BatteryPoolReferenceStore,
5960
name: str | None,
6061
priority: int,
6162
):
@@ -67,12 +68,12 @@ def __init__(
6768
for creating `BatteryPool` instances.
6869
6970
Args:
70-
battery_pool_ref: The battery pool reference store instance.
71+
pool_ref_store: The battery pool reference store instance.
7172
name: An optional name used to identify this instance of the pool or a
7273
corresponding actor in the logs.
7374
priority: The priority of the actor using this wrapper.
7475
"""
75-
self._battery_pool = battery_pool_ref
76+
self._pool_ref_store = pool_ref_store
7677
unique_id = str(uuid.uuid4())
7778
self._source_id = unique_id if name is None else f"{name}-{unique_id}"
7879
self._priority = priority
@@ -120,12 +121,12 @@ async def propose_power(
120121
to the maximum power of the batteries in the pool. This is currently
121122
and experimental feature.
122123
"""
123-
await self._battery_pool._power_manager_requests_sender.send(
124+
await self._pool_ref_store._power_manager_requests_sender.send(
124125
_power_managing.Proposal(
125126
source_id=self._source_id,
126127
preferred_power=power,
127128
bounds=bounds,
128-
component_ids=self._battery_pool._batteries,
129+
component_ids=self._pool_ref_store._batteries,
129130
priority=self._priority,
130131
creation_time=asyncio.get_running_loop().time(),
131132
request_timeout=request_timeout,
@@ -166,12 +167,12 @@ async def propose_charge(
166167
"""
167168
if power and power < Power.zero():
168169
raise ValueError("Charge power must be positive.")
169-
await self._battery_pool._power_manager_requests_sender.send(
170+
await self._pool_ref_store._power_manager_requests_sender.send(
170171
_power_managing.Proposal(
171172
source_id=self._source_id,
172173
preferred_power=power,
173174
bounds=timeseries.Bounds(None, None),
174-
component_ids=self._battery_pool._batteries,
175+
component_ids=self._pool_ref_store._batteries,
175176
priority=self._priority,
176177
creation_time=asyncio.get_running_loop().time(),
177178
request_timeout=request_timeout,
@@ -214,12 +215,12 @@ async def propose_discharge(
214215
if power < Power.zero():
215216
raise ValueError("Discharge power must be positive.")
216217
power = -power
217-
await self._battery_pool._power_manager_requests_sender.send(
218+
await self._pool_ref_store._power_manager_requests_sender.send(
218219
_power_managing.Proposal(
219220
source_id=self._source_id,
220221
preferred_power=power,
221222
bounds=timeseries.Bounds(None, None),
222-
component_ids=self._battery_pool._batteries,
223+
component_ids=self._pool_ref_store._batteries,
223224
priority=self._priority,
224225
creation_time=asyncio.get_running_loop().time(),
225226
request_timeout=request_timeout,
@@ -233,7 +234,7 @@ def component_ids(self) -> abc.Set[int]:
233234
Returns:
234235
Ids of the batteries in the pool
235236
"""
236-
return self._battery_pool._batteries
237+
return self._pool_ref_store._batteries
237238

238239
@property
239240
def power(self) -> FormulaEngine[Power]:
@@ -251,11 +252,11 @@ def power(self) -> FormulaEngine[Power]:
251252
A FormulaEngine that will calculate and stream the total power of all
252253
batteries in the pool.
253254
"""
254-
engine = self._battery_pool._formula_pool.from_power_formula_generator(
255+
engine = self._pool_ref_store._formula_pool.from_power_formula_generator(
255256
"battery_pool_power",
256257
BatteryPowerFormula,
257258
FormulaGeneratorConfig(
258-
component_ids=self._battery_pool._batteries,
259+
component_ids=self._pool_ref_store._batteries,
259260
),
260261
)
261262
assert isinstance(engine, FormulaEngine)
@@ -298,15 +299,15 @@ def soc(self) -> ReceiverFetcher[Sample[Percentage]]:
298299
"""
299300
method_name = SendOnUpdate.name() + "_" + SoCCalculator.name()
300301

301-
if method_name not in self._battery_pool._active_methods:
302-
calculator = SoCCalculator(self._battery_pool._batteries)
303-
self._battery_pool._active_methods[method_name] = SendOnUpdate(
302+
if method_name not in self._pool_ref_store._active_methods:
303+
calculator = SoCCalculator(self._pool_ref_store._batteries)
304+
self._pool_ref_store._active_methods[method_name] = SendOnUpdate(
304305
metric_calculator=calculator,
305-
working_batteries=self._battery_pool._working_batteries,
306-
min_update_interval=self._battery_pool._min_update_interval,
306+
working_batteries=self._pool_ref_store._working_batteries,
307+
min_update_interval=self._pool_ref_store._min_update_interval,
307308
)
308309

309-
return self._battery_pool._active_methods[method_name]
310+
return self._pool_ref_store._active_methods[method_name]
310311

311312
@property
312313
def temperature(self) -> ReceiverFetcher[Sample[Temperature]]:
@@ -317,14 +318,14 @@ def temperature(self) -> ReceiverFetcher[Sample[Temperature]]:
317318
of all batteries in the pool.
318319
"""
319320
method_name = SendOnUpdate.name() + "_" + TemperatureCalculator.name()
320-
if method_name not in self._battery_pool._active_methods:
321-
calculator = TemperatureCalculator(self._battery_pool._batteries)
322-
self._battery_pool._active_methods[method_name] = SendOnUpdate(
321+
if method_name not in self._pool_ref_store._active_methods:
322+
calculator = TemperatureCalculator(self._pool_ref_store._batteries)
323+
self._pool_ref_store._active_methods[method_name] = SendOnUpdate(
323324
metric_calculator=calculator,
324-
working_batteries=self._battery_pool._working_batteries,
325-
min_update_interval=self._battery_pool._min_update_interval,
325+
working_batteries=self._pool_ref_store._working_batteries,
326+
min_update_interval=self._pool_ref_store._min_update_interval,
326327
)
327-
return self._battery_pool._active_methods[method_name]
328+
return self._pool_ref_store._active_methods[method_name]
328329

329330
@property
330331
def capacity(self) -> ReceiverFetcher[Sample[Energy]]:
@@ -355,15 +356,15 @@ def capacity(self) -> ReceiverFetcher[Sample[Energy]]:
355356
"""
356357
method_name = SendOnUpdate.name() + "_" + CapacityCalculator.name()
357358

358-
if method_name not in self._battery_pool._active_methods:
359-
calculator = CapacityCalculator(self._battery_pool._batteries)
360-
self._battery_pool._active_methods[method_name] = SendOnUpdate(
359+
if method_name not in self._pool_ref_store._active_methods:
360+
calculator = CapacityCalculator(self._pool_ref_store._batteries)
361+
self._pool_ref_store._active_methods[method_name] = SendOnUpdate(
361362
metric_calculator=calculator,
362-
working_batteries=self._battery_pool._working_batteries,
363-
min_update_interval=self._battery_pool._min_update_interval,
363+
working_batteries=self._pool_ref_store._working_batteries,
364+
min_update_interval=self._pool_ref_store._min_update_interval,
364365
)
365366

366-
return self._battery_pool._active_methods[method_name]
367+
return self._pool_ref_store._active_methods[method_name]
367368

368369
@property
369370
def power_status(self) -> ReceiverFetcher[BatteryPoolReport]:
@@ -380,14 +381,14 @@ def power_status(self) -> ReceiverFetcher[BatteryPoolReport]:
380381
sub = _power_managing.ReportRequest(
381382
source_id=self._source_id,
382383
priority=self._priority,
383-
component_ids=self._battery_pool._batteries,
384+
component_ids=self._pool_ref_store._batteries,
384385
)
385-
self._battery_pool._power_bounds_subs[sub.get_channel_name()] = (
386+
self._pool_ref_store._power_bounds_subs[sub.get_channel_name()] = (
386387
asyncio.create_task(
387-
self._battery_pool._power_manager_bounds_subscription_sender.send(sub)
388+
self._pool_ref_store._power_manager_bounds_subscription_sender.send(sub)
388389
)
389390
)
390-
channel = self._battery_pool._channel_registry.get_or_create(
391+
channel = self._pool_ref_store._channel_registry.get_or_create(
391392
_power_managing._Report, sub.get_channel_name()
392393
)
393394
channel.resend_latest = True
@@ -415,16 +416,16 @@ def _system_power_bounds(self) -> ReceiverFetcher[SystemBounds]:
415416
"""
416417
method_name = SendOnUpdate.name() + "_" + PowerBoundsCalculator.name()
417418

418-
if method_name not in self._battery_pool._active_methods:
419-
calculator = PowerBoundsCalculator(self._battery_pool._batteries)
420-
self._battery_pool._active_methods[method_name] = SendOnUpdate(
419+
if method_name not in self._pool_ref_store._active_methods:
420+
calculator = PowerBoundsCalculator(self._pool_ref_store._batteries)
421+
self._pool_ref_store._active_methods[method_name] = SendOnUpdate(
421422
metric_calculator=calculator,
422-
working_batteries=self._battery_pool._working_batteries,
423-
min_update_interval=self._battery_pool._min_update_interval,
423+
working_batteries=self._pool_ref_store._working_batteries,
424+
min_update_interval=self._pool_ref_store._min_update_interval,
424425
)
425426

426-
return self._battery_pool._active_methods[method_name]
427+
return self._pool_ref_store._active_methods[method_name]
427428

428429
async def stop(self) -> None:
429430
"""Stop all tasks and channels owned by the BatteryPool."""
430-
await self._battery_pool.stop()
431+
await self._pool_ref_store.stop()

0 commit comments

Comments
 (0)