Skip to content

Commit 2e71987

Browse files
Add metadata to Components
This commit introduces metadata to Components by incorporating GridMetadata, which now includes the maximum grid current, specified in Amperes. Signed-off-by: Tiyash Basu <[email protected]>
1 parent a17939d commit 2e71987

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

src/frequenz/sdk/microgrid/client/_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
)
3737
from ..component._component import (
3838
_component_category_from_protobuf,
39+
_component_metadata_from_protobuf,
3940
_component_type_from_protobuf,
4041
)
4142
from ._connection import Connection
@@ -254,6 +255,7 @@ async def components(self) -> Iterable[Component]:
254255
c.id,
255256
_component_category_from_protobuf(c.category),
256257
_component_type_from_protobuf(c.category, c.inverter),
258+
_component_metadata_from_protobuf(c.category, c.grid),
257259
),
258260
components_only,
259261
)

src/frequenz/sdk/microgrid/component/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
This package provides classes to operate con microgrid components.
77
"""
88

9-
from ._component import Component, ComponentCategory, ComponentMetricId, InverterType
9+
from ._component import (
10+
Component,
11+
ComponentCategory,
12+
ComponentMetadata,
13+
ComponentMetricId,
14+
GridMetadata,
15+
InverterType,
16+
)
1017
from ._component_data import (
1118
BatteryData,
1219
ComponentData,
@@ -21,10 +28,12 @@
2128
"Component",
2229
"ComponentData",
2330
"ComponentCategory",
31+
"ComponentMetadata",
2432
"ComponentMetricId",
2533
"EVChargerCableState",
2634
"EVChargerComponentState",
2735
"EVChargerData",
36+
"GridMetadata",
2837
"InverterData",
2938
"InverterType",
3039
"MeterData",

src/frequenz/sdk/microgrid/component/_component.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from enum import Enum
1010

1111
import frequenz.api.common.components_pb2 as components_pb
12+
import frequenz.api.microgrid.grid_pb2 as grid_pb
1213
import frequenz.api.microgrid.inverter_pb2 as inverter_pb
1314

1415

@@ -105,13 +106,37 @@ def _component_category_from_protobuf(
105106
return ComponentCategory(component_category)
106107

107108

109+
@dataclass(frozen=True)
110+
class ComponentMetadata:
111+
"""Base class for component metadata classes."""
112+
113+
114+
@dataclass(frozen=True)
115+
class GridMetadata(ComponentMetadata):
116+
"""Metadata for a grid connection point."""
117+
118+
max_current: float
119+
"""maximum current rating of the grid connection point in amps."""
120+
121+
122+
def _component_metadata_from_protobuf(
123+
component_category: components_pb.ComponentCategory.ValueType,
124+
component_metadata: grid_pb.Metadata,
125+
) -> GridMetadata | None:
126+
if component_category == components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID:
127+
return GridMetadata(float(component_metadata.rated_fuse_current))
128+
129+
return None
130+
131+
108132
@dataclass(frozen=True)
109133
class Component:
110134
"""Metadata for a single microgrid component."""
111135

112136
component_id: int
113137
category: ComponentCategory
114138
type: ComponentType | None = None
139+
metadata: ComponentMetadata | None = None
115140

116141
def is_valid(self) -> bool:
117142
"""Check if this instance contains valid data.

tests/microgrid/test_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Component,
2222
ComponentCategory,
2323
EVChargerData,
24+
GridMetadata,
2425
InverterData,
2526
InverterType,
2627
MeterData,
@@ -117,9 +118,15 @@ async def test_components(self) -> None:
117118
(999, components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
118119
]
119120
)
121+
120122
assert set(await microgrid.components()) == {
121123
Component(100, ComponentCategory.NONE),
122-
Component(101, ComponentCategory.GRID),
124+
Component(
125+
101,
126+
ComponentCategory.GRID,
127+
None,
128+
GridMetadata(max_current=0.0),
129+
),
123130
Component(104, ComponentCategory.METER),
124131
Component(105, ComponentCategory.INVERTER, InverterType.NONE),
125132
Component(106, ComponentCategory.BATTERY),

tests/microgrid/test_graph.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818

1919
import frequenz.sdk.microgrid._graph as gr
2020
from frequenz.sdk.microgrid.client import Connection, MicrogridGrpcClient
21-
from frequenz.sdk.microgrid.component import Component, ComponentCategory, InverterType
21+
from frequenz.sdk.microgrid.component import (
22+
Component,
23+
ComponentCategory,
24+
GridMetadata,
25+
InverterType,
26+
)
2227

2328
from .mock_api import MockGrpcServer, MockMicrogridServicer
2429

@@ -816,8 +821,16 @@ async def test_refresh_from_api(self) -> None:
816821
servicer.set_connections([(101, 111), (111, 131)])
817822
await graph.refresh_from_api(client)
818823

824+
# Note: we need to add GriMetadata as a dict here, because that's what
825+
# the ComponentGraph does too, and we need to be able to compare the
826+
# two graphs.
819827
expected = {
820-
Component(101, ComponentCategory.GRID),
828+
Component(
829+
101,
830+
ComponentCategory.GRID,
831+
None,
832+
asdict(GridMetadata(max_current=0.0)), # type: ignore
833+
),
821834
Component(111, ComponentCategory.METER),
822835
Component(131, ComponentCategory.EV_CHARGER),
823836
}
@@ -843,7 +856,12 @@ async def test_refresh_from_api(self) -> None:
843856
servicer.set_connections([(707, 717), (717, 727), (727, 737), (717, 747)])
844857
await graph.refresh_from_api(client)
845858
expected = {
846-
Component(707, ComponentCategory.GRID),
859+
Component(
860+
707,
861+
ComponentCategory.GRID,
862+
None,
863+
asdict(GridMetadata(max_current=0.0)), # type: ignore
864+
),
847865
Component(717, ComponentCategory.METER),
848866
Component(727, ComponentCategory.INVERTER, InverterType.NONE),
849867
Component(737, ComponentCategory.BATTERY),
@@ -1146,7 +1164,7 @@ def test__validate_grid_endpoint(self) -> None:
11461164
gr.InvalidGraphError,
11471165
match=r"Grid endpoint 1 has graph predecessors: \[Component"
11481166
r"\(component_id=99, category=<ComponentCategory.METER: 2>, "
1149-
r"type=None\)\]",
1167+
r"type=None, metadata=None\)\]",
11501168
) as _err_predecessors:
11511169
graph._validate_grid_endpoint()
11521170

0 commit comments

Comments
 (0)