Skip to content

Commit 8dfd2ed

Browse files
committed
Make inverter type available through the component graph
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 6ab8228 commit 8dfd2ed

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
InverterData,
2424
MeterData,
2525
)
26-
from ..component._component import _component_category_from_protobuf
26+
from ..component._component import (
27+
_component_category_from_protobuf,
28+
_component_type_from_protobuf,
29+
)
2730
from ._connection import Connection
2831
from ._retry import LinearBackoff, RetryStrategy
2932

@@ -229,7 +232,11 @@ async def components(self) -> Iterable[Component]:
229232
component_list.components,
230233
)
231234
result: Iterable[Component] = map(
232-
lambda c: Component(c.id, _component_category_from_protobuf(c.category)),
235+
lambda c: Component(
236+
c.id,
237+
_component_category_from_protobuf(c.category),
238+
_component_type_from_protobuf(c.category, c.inverter),
239+
),
233240
components_only,
234241
)
235242

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

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

9-
from ._component import Component, ComponentCategory, ComponentMetricId
9+
from ._component import Component, ComponentCategory, ComponentMetricId, InverterType
1010
from ._component_data import (
1111
BatteryData,
1212
ComponentData,
@@ -25,5 +25,6 @@
2525
"EVChargerCableState",
2626
"EVChargerData",
2727
"InverterData",
28+
"InverterType",
2829
"MeterData",
2930
]

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,52 @@
77

88
from dataclasses import dataclass
99
from enum import Enum
10+
from typing import Optional
1011

12+
import frequenz.api.microgrid.inverter_pb2 as inverter_pb
1113
import frequenz.api.microgrid.microgrid_pb2 as microgrid_pb
1214

1315

16+
class ComponentType(Enum):
17+
"""A base class from which individual component types are derived."""
18+
19+
20+
class InverterType(ComponentType):
21+
"""Enum representing inverter types."""
22+
23+
NONE = inverter_pb.Type.TYPE_UNSPECIFIED
24+
BATTERY = inverter_pb.Type.TYPE_BATTERY
25+
SOLAR = inverter_pb.Type.TYPE_SOLAR
26+
HYBRID = inverter_pb.Type.TYPE_HYBRID
27+
28+
29+
def _component_type_from_protobuf(
30+
component_category: microgrid_pb.ComponentCategory.ValueType,
31+
component_type: inverter_pb.Type.ValueType,
32+
) -> Optional[ComponentType]:
33+
"""Convert a protobuf InverterType message to Component enum.
34+
35+
For internal-only use by the `microgrid` package.
36+
37+
Args:
38+
component_category: category the type belongs to.
39+
component_type: protobuf enum to convert.
40+
41+
Returns:
42+
Enum value corresponding to the protobuf message.
43+
"""
44+
# ComponentType values in the protobuf definition are not unique across categories
45+
# as of v0.11.0, so we need to check the component category first, before doing any
46+
# component type checks.
47+
if component_category == microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER:
48+
if not any(t.value == component_type for t in InverterType):
49+
return None
50+
51+
return InverterType(component_type)
52+
53+
return None
54+
55+
1456
class ComponentCategory(Enum):
1557
"""Possible types of microgrid component."""
1658

@@ -62,6 +104,7 @@ class Component:
62104

63105
component_id: int
64106
category: ComponentCategory
107+
type: Optional[ComponentType] = None
65108

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

tests/microgrid/test_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
ComponentCategory,
2222
EVChargerData,
2323
InverterData,
24+
InverterType,
2425
MeterData,
2526
)
2627

@@ -97,7 +98,7 @@ async def test_components(self) -> None:
9798
)
9899
assert set(await microgrid.components()) == {
99100
Component(9, ComponentCategory.METER),
100-
Component(99, ComponentCategory.INVERTER),
101+
Component(99, ComponentCategory.INVERTER, InverterType.NONE),
101102
Component(999, ComponentCategory.BATTERY),
102103
}
103104

@@ -123,7 +124,7 @@ async def test_components(self) -> None:
123124
Component(101, ComponentCategory.GRID),
124125
Component(103, ComponentCategory.JUNCTION),
125126
Component(104, ComponentCategory.METER),
126-
Component(105, ComponentCategory.INVERTER),
127+
Component(105, ComponentCategory.INVERTER, InverterType.NONE),
127128
Component(106, ComponentCategory.BATTERY),
128129
Component(107, ComponentCategory.EV_CHARGER),
129130
}

tests/microgrid/test_graph.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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
21+
from frequenz.sdk.microgrid.component import Component, ComponentCategory, InverterType
2222

2323
from .mock_api import MockGrpcServer, MockMicrogridServicer
2424

@@ -751,7 +751,7 @@ async def test_refresh_from_api(self) -> None:
751751
expected = {
752752
Component(707, ComponentCategory.GRID),
753753
Component(717, ComponentCategory.METER),
754-
Component(727, ComponentCategory.INVERTER),
754+
Component(727, ComponentCategory.INVERTER, InverterType.NONE),
755755
Component(737, ComponentCategory.BATTERY),
756756
Component(747, ComponentCategory.METER),
757757
}
@@ -1068,7 +1068,8 @@ def test__validate_grid_endpoint(self) -> None:
10681068
with pytest.raises(
10691069
gr.InvalidGraphError,
10701070
match=r"Grid endpoint 1 has graph predecessors: \[Component"
1071-
r"\(component_id=99, category=<ComponentCategory.METER: 2>\)\]",
1071+
r"\(component_id=99, category=<ComponentCategory.METER: 2>, "
1072+
r"type=None\)\]",
10721073
) as _err_predecessors:
10731074
graph._validate_grid_endpoint()
10741075

0 commit comments

Comments
 (0)