Skip to content

Commit 40e380d

Browse files
committed
Update BatteryPowerFormula generator to take battery_ids as param
... instead of fetching battery ids from the component graph. The BatteryPool will instead deal with which battery ids to use. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent fd37b57 commit 40e380d

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/frequenz/sdk/timeseries/_formula_engine/_formula_generators/_battery_power_formula.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import logging
77

88
from ....microgrid import connection_manager
9-
from ....microgrid.component import ComponentCategory, ComponentMetricId, InverterType
9+
from ....microgrid.component import ComponentMetricId
1010
from ..._formula_engine import FormulaEngine
11-
from ._formula_generator import NON_EXISTING_COMPONENT_ID, FormulaGenerator
11+
from ._formula_generator import (
12+
NON_EXISTING_COMPONENT_ID,
13+
ComponentNotFound,
14+
FormulaGenerator,
15+
)
1216

1317
logger = logging.getLogger(__name__)
1418

@@ -37,28 +41,32 @@ def generate(
3741
in the component graph.
3842
"""
3943
builder = self._get_builder("battery-power", ComponentMetricId.ACTIVE_POWER)
40-
component_graph = connection_manager.get().component_graph
41-
battery_inverters = list(
42-
comp
43-
for comp in component_graph.components()
44-
if comp.category == ComponentCategory.INVERTER
45-
and comp.type == InverterType.BATTERY
46-
)
47-
48-
if not battery_inverters:
44+
component_ids = self._config.component_ids
45+
if not component_ids:
4946
logger.warning(
50-
"Unable to find any battery inverters in the component graph. "
47+
"No Battery component IDs specified. "
5148
"Subscribing to the resampling actor with a non-existing "
5249
"component id, so that `0` values are sent from the formula."
5350
)
54-
# If there are no battery inverters, we have to send 0 values as the same
55-
# frequency as the other streams. So we subscribe with a non-existing
51+
# If there are no Batteries, we have to send 0 values as the same
52+
# frequency as the other streams. So we subscribe with a non-existing
5653
# component id, just to get a `None` message at the resampling interval.
5754
builder.push_component_metric(
5855
NON_EXISTING_COMPONENT_ID, nones_are_zeros=True
5956
)
6057
return builder.build()
6158

59+
component_graph = connection_manager.get().component_graph
60+
61+
battery_inverters = list(
62+
next(iter(component_graph.predecessors(bat_id))) for bat_id in component_ids
63+
)
64+
65+
if len(component_ids) != len(battery_inverters):
66+
raise ComponentNotFound(
67+
"Can't find inverters for all batteries from the component graph."
68+
)
69+
6270
for idx, comp in enumerate(battery_inverters):
6371
if idx > 0:
6472
builder.push_oper("+")

0 commit comments

Comments
 (0)