Skip to content

Commit 321cc9c

Browse files
Add support for reactive formulas in formula_engine_pool
Signed-off-by: Elzbieta Kotulska <[email protected]>
1 parent cd8d39e commit 321cc9c

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/frequenz/sdk/timeseries/formula_engine/_formula_engine_pool.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from frequenz.channels import Sender
1111
from frequenz.client.microgrid import ComponentMetricId
12-
from frequenz.quantities import Current, Power, Quantity
12+
from frequenz.quantities import Current, Power, Quantity, ReactivePower
1313

1414
from ..._internal._channels import ChannelRegistry
1515
from ...microgrid._data_sourcing import ComponentMetricRequest
@@ -54,6 +54,7 @@ def __init__(
5454
self._power_engines: dict[str, FormulaEngine[Power]] = {}
5555
self._power_3_phase_engines: dict[str, FormulaEngine3Phase[Power]] = {}
5656
self._current_engines: dict[str, FormulaEngine3Phase[Current]] = {}
57+
self._reactive_power_engines: dict[str, FormulaEngine[ReactivePower]] = {}
5758

5859
def from_string(
5960
self,
@@ -91,6 +92,40 @@ def from_string(
9192

9293
return formula_engine
9394

95+
def from_reactive_power_formula_generator(
96+
self,
97+
channel_key: str,
98+
generator: type[FormulaGenerator[ReactivePower]],
99+
config: FormulaGeneratorConfig = FormulaGeneratorConfig(),
100+
) -> FormulaEngine[ReactivePower]:
101+
"""Get a receiver for a formula from a generator.
102+
103+
Args:
104+
channel_key: A string to uniquely identify the formula.
105+
generator: A formula generator.
106+
config: config to initialize the formula generator with.
107+
108+
Returns:
109+
A FormulaReceiver or a FormulaReceiver3Phase instance based on what the
110+
FormulaGenerator returns.
111+
"""
112+
from ._formula_engine import ( # pylint: disable=import-outside-toplevel
113+
FormulaEngine,
114+
)
115+
116+
if channel_key in self._reactive_power_engines:
117+
return self._reactive_power_engines[channel_key]
118+
119+
engine = generator(
120+
self._namespace,
121+
self._channel_registry,
122+
self._resampler_subscription_sender,
123+
config,
124+
).generate()
125+
assert isinstance(engine, FormulaEngine)
126+
self._reactive_power_engines[channel_key] = engine
127+
return engine
128+
94129
def from_power_formula_generator(
95130
self,
96131
channel_key: str,
@@ -203,3 +238,5 @@ async def stop(self) -> None:
203238
await power_3_phase_engine._stop() # pylint: disable=protected-access
204239
for current_engine in self._current_engines.values():
205240
await current_engine._stop() # pylint: disable=protected-access
241+
for reactive_power_engine in self._reactive_power_engines.values():
242+
await reactive_power_engine._stop() # pylint: disable=protected-access

src/frequenz/sdk/timeseries/grid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from frequenz.channels import Sender
1616
from frequenz.client.microgrid._component import ComponentCategory, ComponentMetricId
17-
from frequenz.quantities import Current, Power
17+
from frequenz.quantities import Current, Power, ReactivePower
1818

1919
from .._internal._channels import ChannelRegistry
2020
from ..microgrid import connection_manager
@@ -97,7 +97,7 @@ def power(self) -> FormulaEngine[Power]:
9797
return engine
9898

9999
@property
100-
def reactive_power(self) -> FormulaEngine[Power]:
100+
def reactive_power(self) -> FormulaEngine[ReactivePower]:
101101
"""Fetch the grid reactive power for the microgrid.
102102
103103
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -111,7 +111,7 @@ def reactive_power(self) -> FormulaEngine[Power]:
111111
Returns:
112112
A FormulaEngine that will calculate and stream grid reactive power.
113113
"""
114-
engine = self._formula_pool.from_power_formula_generator(
114+
engine = self._formula_pool.from_reactive_power_formula_generator(
115115
f"grid-{ComponentMetricId.REACTIVE_POWER.value}",
116116
GridReactivePowerFormula,
117117
)

0 commit comments

Comments
 (0)