Skip to content

Commit 767e52c

Browse files
Redesign access to migrogrid.grid()
Signed-off-by: Christian Parpart <[email protected]>
1 parent c498555 commit 767e52c

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This version ships an experimental version of the **Power Manager**, adds prelim
88

99
- `microgrid.battery_pool()` method now accepts a priority value.
1010

11+
- `microgrid.grid()`
12+
13+
* Similar to `microgrid.battery_pool()`, the Grid is now similarily accessed.
14+
1115
- `BatteryPool`'s control methods
1216

1317
* Original methods `{set_power/charge/discharge}` are now replaced by `propose_{power/charge/discharge}`

src/frequenz/sdk/microgrid/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@
123123
""" # noqa: D205, D400
124124

125125
from ..actor import ResamplerConfig
126-
from . import _data_pipeline, client, component, connection_manager, fuse, grid
127-
from ._data_pipeline import battery_pool, ev_charger_pool, frequency, logical_meter
126+
from . import _data_pipeline, client, component, connection_manager, fuse
127+
from ._data_pipeline import (
128+
battery_pool,
129+
ev_charger_pool,
130+
frequency,
131+
grid,
132+
logical_meter,
133+
)
134+
from ._grid import initialize as initialize_grid
128135

129136

130137
async def initialize(host: str, port: int, resampler_config: ResamplerConfig) -> None:
@@ -139,7 +146,7 @@ async def initialize(host: str, port: int, resampler_config: ResamplerConfig) ->
139146

140147
api_client = connection_manager.get().api_client
141148
components = await api_client.components()
142-
grid.initialize(components)
149+
initialize_grid(components)
143150

144151
await _data_pipeline.initialize(resampler_config)
145152

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from ..microgrid.component import Component
2323
from ..timeseries._grid_frequency import GridFrequency
2424
from . import connection_manager
25+
from ._grid import Grid
26+
from ._grid import get as get_grid
2527
from .component import ComponentCategory
2628

2729
_logger = logging.getLogger(__name__)
@@ -186,6 +188,18 @@ def ev_charger_pool(
186188
)
187189
return self._ev_charger_pools[key]
188190

191+
def grid(
192+
self,
193+
) -> Grid | None:
194+
"""Return the grid instance.
195+
196+
If a Grid instance doesn't exist, a new one is created and returned.
197+
198+
Returns:
199+
A Grid instance.
200+
"""
201+
return get_grid()
202+
189203
def battery_pool(
190204
self,
191205
battery_ids: abc.Set[int] | None = None,
@@ -454,6 +468,15 @@ def battery_pool(
454468
return _get().battery_pool(battery_ids, name, priority)
455469

456470

471+
def grid() -> Grid | None:
472+
"""Return the grid instance.
473+
474+
Returns:
475+
The Grid instance.
476+
"""
477+
return _get().grid()
478+
479+
457480
def _get() -> _DataPipeline:
458481
if _DATA_PIPELINE is None:
459482
raise RuntimeError(

tests/microgrid/test_grid.py

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

44
"""Tests for the `Grid` module."""
55

6-
from frequenz.sdk import microgrid
6+
from frequenz.sdk.microgrid import _grid
77
from frequenz.sdk.microgrid.component import Component, ComponentCategory, GridMetadata
88
from frequenz.sdk.microgrid.fuse import Fuse
9-
from frequenz.sdk.microgrid.grid import Grid
109
from frequenz.sdk.timeseries import Current
1110

1211

13-
async def test_grid() -> None:
12+
async def test_grid_1() -> None:
1413
"""Test the grid connection module."""
1514
# The tests here need to be in this exact sequence, because the grid connection
1615
# is a singleton. Once it gets created, it stays in memory for the duration of
@@ -21,15 +20,29 @@ async def test_grid() -> None:
2120
Component(2, ComponentCategory.METER),
2221
]
2322

24-
microgrid.grid.initialize(components)
25-
26-
grid = microgrid.grid.get()
23+
_grid.initialize(components)
24+
grid = _grid.get()
2725
assert grid is None
2826

29-
# validate that the microgrid initialization fails when there are multiple
30-
# grid connection points.
27+
28+
def _create_fuse() -> Fuse:
29+
"""Create a fuse with a fixed current.
30+
31+
Returns:
32+
Fuse: The fuse.
33+
"""
3134
fuse_current = Current.from_amperes(123.0)
3235
fuse = Fuse(fuse_current)
36+
return fuse
37+
38+
39+
async def test_grid_2() -> None:
40+
"""Test the grid connection module, slightly more complex.
41+
42+
Validate that the microgrid initialization fails
43+
when there are multiple grid connection points.
44+
"""
45+
fuse = _create_fuse()
3346

3447
components = [
3548
Component(1, ComponentCategory.GRID, None, GridMetadata(fuse)),
@@ -38,29 +51,27 @@ async def test_grid() -> None:
3851
]
3952

4053
try:
41-
microgrid.grid.initialize(components)
42-
assert False, "Expected microgrid.grid.initialize to raise a RuntimeError."
54+
_grid.initialize(components)
55+
assert False, "Expected microgrid.grid() to raise a RuntimeError."
4356
except RuntimeError:
4457
pass
4558

46-
grid = microgrid.grid.get()
47-
assert grid is None
4859

49-
# validate that microgrids with one grid connection are accepted.
60+
async def test_grid_3() -> None:
61+
"""Validate that microgrids with one grid connection are accepted."""
5062
components = [
51-
Component(1, ComponentCategory.GRID, None, GridMetadata(fuse)),
63+
Component(1, ComponentCategory.GRID, None, GridMetadata(_create_fuse())),
5264
Component(2, ComponentCategory.METER),
5365
]
5466

55-
microgrid.grid.initialize(components)
56-
57-
grid = microgrid.grid.get()
67+
_grid.initialize(components)
68+
grid = _grid.get()
5869
assert grid is not None
5970

6071
expected_fuse_current = Current.from_amperes(123.0)
6172
expected_fuse = Fuse(expected_fuse_current)
6273

63-
assert grid == Grid(fuse=expected_fuse)
74+
assert grid == _grid.Grid(fuse=expected_fuse)
6475

6576
fuse_current = grid.fuse.max_current
6677
assert fuse_current == expected_fuse_current

0 commit comments

Comments
 (0)