Skip to content

Commit cfb9991

Browse files
Add function to create mock component graph structure
... to pytest utils folder. The behaviour is copied from the MockMicrogrid. I moved it to another module to make for readable. Signed-off-by: ela-kotulska-frequenz <[email protected]>
1 parent ac472c8 commit cfb9991

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Utils for tests that uses component graph."""
5+
from __future__ import annotations
6+
7+
from dataclasses import dataclass
8+
9+
from frequenz.sdk.microgrid.client import Connection
10+
from frequenz.sdk.microgrid.component import Component, ComponentCategory, InverterType
11+
12+
13+
@dataclass
14+
class ComponentGraphConfig:
15+
"""Config with information how the component graph should be created."""
16+
17+
grid_side_meter: bool = True
18+
"""True if the main meter should be by grid side."""
19+
20+
batteries_num: int = 0
21+
"""Number of batteries in the component graph.
22+
23+
Each battery will have its own inverter and meter connected.
24+
"""
25+
26+
solar_inverters_num: int = 0
27+
"""Number of pv inverters in the component graph.
28+
29+
Each pv inverter will have its own pv meter connected.
30+
"""
31+
32+
ev_chargers: int = 0
33+
"""Number of ev chargers in the component graph."""
34+
35+
36+
def create_component_graph_structure(
37+
component_graph_config: ComponentGraphConfig,
38+
) -> tuple[set[Component], set[Connection]]:
39+
"""Create structure of components graph.
40+
41+
Args:
42+
component_graph_config: config that tells what graph should have.
43+
44+
Returns:
45+
Create set of components and set of connections between them.
46+
"""
47+
grid_id = 1
48+
main_meter_id = 2
49+
50+
components = {
51+
Component(grid_id, ComponentCategory.GRID),
52+
Component(main_meter_id, ComponentCategory.METER),
53+
}
54+
connections = {Connection(grid_id, main_meter_id)}
55+
56+
junction_id: int = grid_id
57+
if component_graph_config.grid_side_meter:
58+
junction_id = main_meter_id
59+
60+
start_idx = 3
61+
for _ in range(component_graph_config.batteries_num):
62+
meter_id = start_idx
63+
inv_id = start_idx + 1
64+
battery_id = start_idx + 2
65+
start_idx += 3
66+
67+
components.add(Component(meter_id, ComponentCategory.METER))
68+
components.add(Component(battery_id, ComponentCategory.BATTERY))
69+
components.add(
70+
Component(inv_id, ComponentCategory.INVERTER, InverterType.BATTERY)
71+
)
72+
73+
connections.add(Connection(junction_id, meter_id))
74+
connections.add(Connection(meter_id, inv_id))
75+
connections.add(Connection(inv_id, battery_id))
76+
77+
for _ in range(component_graph_config.solar_inverters_num):
78+
meter_id = start_idx
79+
inv_id = start_idx + 1
80+
start_idx += 2
81+
82+
components.add(Component(meter_id, ComponentCategory.METER))
83+
components.add(
84+
Component(inv_id, ComponentCategory.INVERTER, InverterType.SOLAR)
85+
)
86+
connections.add(Connection(junction_id, meter_id))
87+
connections.add(Connection(meter_id, inv_id))
88+
89+
for _ in range(component_graph_config.ev_chargers):
90+
ev_id = start_idx
91+
start_idx += 1
92+
93+
components.add(Component(ev_id, ComponentCategory.EV_CHARGER))
94+
connections.add(Connection(junction_id, ev_id))
95+
return components, connections

0 commit comments

Comments
 (0)