Skip to content

Commit 8fbf265

Browse files
committed
Update BatteryPool to expose a power() formula
... for the batteries that are part of the BatteryPool instance. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 40e380d commit 8fbf265

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ def battery_pool(
176176

177177
if key not in self._battery_pools:
178178
self._battery_pools[key] = BatteryPool(
179+
channel_registry=self._channel_registry,
180+
resampler_subscription_sender=self._resampling_request_sender(),
179181
batteries_status_receiver=self._battery_status_channel.new_receiver(
180182
maxsize=1
181183
),

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@
66
from __future__ import annotations
77

88
import asyncio
9+
import uuid
910
from collections.abc import Set
1011
from datetime import timedelta
1112
from typing import Any
1213

13-
from frequenz.channels import Receiver
14+
from frequenz.channels import Receiver, Sender
1415

1516
from ..._internal._constants import RECEIVER_MAX_SIZE
1617
from ..._internal.asyncio import cancel_and_await
18+
from ...actor import ChannelRegistry, ComponentMetricRequest
1719
from ...actor.power_distributing._battery_pool_status import BatteryStatus
1820
from ...microgrid import connection_manager
1921
from ...microgrid.component import ComponentCategory
22+
from .._formula_engine import FormulaEngine, FormulaEnginePool
23+
from .._formula_engine._formula_generators import (
24+
BatteryPowerFormula,
25+
FormulaGeneratorConfig,
26+
)
2027
from ._methods import AggregateMethod, SendOnUpdate
2128
from ._metric_calculator import CapacityCalculator, PowerBoundsCalculator, SoCCalculator
2229
from ._result_types import CapacityMetrics, PowerMetrics, SoCMetrics
@@ -29,15 +36,21 @@ class BatteryPool:
2936
fetching high level metrics for this subset.
3037
"""
3138

32-
def __init__(
39+
def __init__( # pylint: disable=too-many-arguments
3340
self,
41+
channel_registry: ChannelRegistry,
42+
resampler_subscription_sender: Sender[ComponentMetricRequest],
3443
batteries_status_receiver: Receiver[BatteryStatus],
3544
min_update_interval: timedelta,
3645
batteries_id: Set[int] | None = None,
3746
) -> None:
3847
"""Create the class instance.
3948
4049
Args:
50+
channel_registry: A channel registry instance shared with the resampling
51+
actor.
52+
resampler_subscription_sender: A sender for sending metric requests to the
53+
resampling actor.
4154
batteries_status_receiver: Receiver to receive status of the batteries.
4255
Receivers should has maxsize = 1 to fetch only the latest status.
4356
Battery status channel should has resend_latest = True.
@@ -71,6 +84,13 @@ def __init__(
7184
self._min_update_interval = min_update_interval
7285
self._active_methods: dict[str, AggregateMethod[Any]] = {}
7386

87+
self._namespace: str = f"battery-pool-{self._batteries}-{uuid.uuid4()}"
88+
self._formula_pool: FormulaEnginePool = FormulaEnginePool(
89+
self._namespace,
90+
channel_registry,
91+
resampler_subscription_sender,
92+
)
93+
7494
@property
7595
def battery_ids(self) -> Set[int]:
7696
"""Return ids of the batteries in the pool.
@@ -80,6 +100,26 @@ def battery_ids(self) -> Set[int]:
80100
"""
81101
return self._batteries
82102

103+
@property
104+
def power(self) -> FormulaEngine:
105+
"""Fetch the total power of the batteries in the pool.
106+
107+
If a formula engine to calculate this metric is not already running, it will be
108+
started.
109+
110+
A receiver from the formula engine can be obtained by calling the `new_receiver`
111+
method.
112+
113+
Returns:
114+
A FormulaEngine that will calculate and stream the total power of all
115+
batteries in the pool.
116+
"""
117+
return self._formula_pool.from_generator(
118+
"battery_pool_power",
119+
BatteryPowerFormula,
120+
FormulaGeneratorConfig(component_ids=self._batteries),
121+
) # type: ignore[return-value]
122+
83123
async def soc(
84124
self, maxsize: int | None = RECEIVER_MAX_SIZE
85125
) -> Receiver[SoCMetrics | None]:

0 commit comments

Comments
 (0)