|
6 | 6 | import logging |
7 | 7 |
|
8 | 8 | from ....microgrid import connection_manager |
9 | | -from ....microgrid.component import ComponentCategory, ComponentMetricId, InverterType |
| 9 | +from ....microgrid.component import ComponentMetricId |
10 | 10 | 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 | +) |
12 | 16 |
|
13 | 17 | logger = logging.getLogger(__name__) |
14 | 18 |
|
@@ -37,28 +41,32 @@ def generate( |
37 | 41 | in the component graph. |
38 | 42 | """ |
39 | 43 | 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: |
49 | 46 | logger.warning( |
50 | | - "Unable to find any battery inverters in the component graph. " |
| 47 | + "No Battery component IDs specified. " |
51 | 48 | "Subscribing to the resampling actor with a non-existing " |
52 | 49 | "component id, so that `0` values are sent from the formula." |
53 | 50 | ) |
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 |
56 | 53 | # component id, just to get a `None` message at the resampling interval. |
57 | 54 | builder.push_component_metric( |
58 | 55 | NON_EXISTING_COMPONENT_ID, nones_are_zeros=True |
59 | 56 | ) |
60 | 57 | return builder.build() |
61 | 58 |
|
| 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 | + |
62 | 70 | for idx, comp in enumerate(battery_inverters): |
63 | 71 | if idx > 0: |
64 | 72 | builder.push_oper("+") |
|
0 commit comments