Skip to content

Commit f031cd9

Browse files
committed
Update EV formula generators to use component_ids from the ev pool
... instead of from the component graph. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 2dbd795 commit f031cd9

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
"""Formula generator from component graph for 3-phase Grid Current."""
55

6+
from __future__ import annotations
7+
68
import logging
7-
from typing import List
89

9-
from .....sdk import microgrid
10-
from ....microgrid.component import Component, ComponentCategory, ComponentMetricId
10+
from ....microgrid.component import ComponentMetricId
1111
from .._formula_engine import FormulaEngine, FormulaEngine3Phase
1212
from ._formula_generator import NON_EXISTING_COMPONENT_ID, FormulaGenerator
1313

@@ -18,21 +18,16 @@ class EVChargerCurrentFormula(FormulaGenerator):
1818
"""Create a formula engine from the component graph for calculating grid current."""
1919

2020
async def generate(self) -> FormulaEngine3Phase:
21-
"""Generate a formula for calculating total ev current from the component graph.
21+
"""Generate a formula for calculating total ev current for given component ids.
2222
2323
Returns:
2424
A formula engine that calculates total 3-phase ev charger current values.
2525
"""
26-
component_graph = microgrid.get().component_graph
27-
ev_chargers = [
28-
comp
29-
for comp in component_graph.components()
30-
if comp.category == ComponentCategory.EV_CHARGER
31-
]
26+
component_ids = self._config.component_ids
3227

33-
if not ev_chargers:
28+
if not component_ids:
3429
logger.warning(
35-
"Unable to find any EV Chargers in the component graph. "
30+
"No ev charger component IDs specified. "
3631
"Subscribing to the resampling actor with a non-existing "
3732
"component id, so that `0` values are sent from the formula."
3833
)
@@ -54,34 +49,34 @@ async def generate(self) -> FormulaEngine3Phase:
5449
(
5550
(
5651
await self._gen_phase_formula(
57-
ev_chargers, ComponentMetricId.CURRENT_PHASE_1
52+
component_ids, ComponentMetricId.CURRENT_PHASE_1
5853
)
5954
).new_receiver(),
6055
(
6156
await self._gen_phase_formula(
62-
ev_chargers, ComponentMetricId.CURRENT_PHASE_2
57+
component_ids, ComponentMetricId.CURRENT_PHASE_2
6358
)
6459
).new_receiver(),
6560
(
6661
await self._gen_phase_formula(
67-
ev_chargers, ComponentMetricId.CURRENT_PHASE_3
62+
component_ids, ComponentMetricId.CURRENT_PHASE_3
6863
)
6964
).new_receiver(),
7065
),
7166
)
7267

7368
async def _gen_phase_formula(
7469
self,
75-
ev_chargers: List[Component],
70+
component_ids: set[int],
7671
metric_id: ComponentMetricId,
7772
) -> FormulaEngine:
7873
builder = self._get_builder("ev-current", metric_id)
7974

8075
# generate a formula that just adds values from all ev-chargers.
81-
for idx, comp in enumerate(ev_chargers):
76+
for idx, component_id in enumerate(component_ids):
8277
if idx > 0:
8378
builder.push_oper("+")
8479

85-
await builder.push_component_metric(comp.component_id, nones_are_zeros=True)
80+
await builder.push_component_metric(component_id, nones_are_zeros=True)
8681

8782
return builder.build()

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import logging
77

8-
from .....sdk import microgrid
9-
from ....microgrid.component import ComponentCategory, ComponentMetricId
8+
from ....microgrid.component import ComponentMetricId
109
from .._formula_engine import FormulaEngine
1110
from ._formula_generator import NON_EXISTING_COMPONENT_ID, FormulaGenerator
1211

@@ -17,22 +16,17 @@ class EVChargerPowerFormula(FormulaGenerator):
1716
"""Create a formula engine from the component graph for calculating grid power."""
1817

1918
async def generate(self) -> FormulaEngine:
20-
"""Generate a formula for calculating total EV power from the component graph.
19+
"""Generate a formula for calculating total ev power for given component ids.
2120
2221
Returns:
2322
A formula engine that calculates total EV charger power values.
2423
"""
2524
builder = self._get_builder("ev-power", ComponentMetricId.ACTIVE_POWER)
26-
component_graph = microgrid.get().component_graph
27-
ev_chargers = [
28-
comp
29-
for comp in component_graph.components()
30-
if comp.category == ComponentCategory.EV_CHARGER
31-
]
32-
33-
if not ev_chargers:
25+
26+
component_ids = self._config.component_ids
27+
if not component_ids:
3428
logger.warning(
35-
"Unable to find any EV Chargers in the component graph. "
29+
"No ev charger component IDs specified. "
3630
"Subscribing to the resampling actor with a non-existing "
3731
"component id, so that `0` values are sent from the formula."
3832
)
@@ -44,10 +38,10 @@ async def generate(self) -> FormulaEngine:
4438
)
4539
return builder.build()
4640

47-
for idx, comp in enumerate(ev_chargers):
41+
for idx, component_id in enumerate(component_ids):
4842
if idx > 0:
4943
builder.push_oper("+")
5044

51-
await builder.push_component_metric(comp.component_id, nones_are_zeros=True)
45+
await builder.push_component_metric(component_id, nones_are_zeros=True)
5246

5347
return builder.build()

src/frequenz/sdk/timeseries/ev_charger_pool/_ev_charger_pool.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .._formula_engine._formula_generators import (
1818
EVChargerCurrentFormula,
1919
EVChargerPowerFormula,
20+
FormulaGeneratorConfig,
2021
)
2122
from ._state_tracker import EVChargerPoolStates, StateTracker
2223

@@ -90,7 +91,9 @@ async def total_current(self) -> FormulaReceiver3Phase:
9091
A *new* receiver that will stream ev_charger current values.
9192
"""
9293
return await self._formula_pool.from_generator(
93-
"ev_charger_total_current", EVChargerCurrentFormula
94+
"ev_charger_total_current",
95+
EVChargerCurrentFormula,
96+
FormulaGeneratorConfig(component_ids=self._component_ids),
9497
)
9598

9699
async def total_power(self) -> FormulaReceiver:
@@ -104,5 +107,7 @@ async def total_power(self) -> FormulaReceiver:
104107
A *new* receiver that will stream ev_charger power values.
105108
"""
106109
return await self._formula_pool.from_generator(
107-
"ev_charger_total_power", EVChargerPowerFormula
110+
"ev_charger_total_power",
111+
EVChargerPowerFormula,
112+
FormulaGeneratorConfig(component_ids=self._component_ids),
108113
)

0 commit comments

Comments
 (0)