Skip to content

Commit 4708546

Browse files
committed
Fetch component_type from component metadata
The component type is now part of `Metadata`. This commit also adds a test for non-NONE inverter types. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent c103d1c commit 4708546

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ class InverterType(ComponentType):
3131

3232
def _component_type_from_protobuf(
3333
component_category: components_pb.ComponentCategory.ValueType,
34-
component_type: inverter_pb.Type.ValueType,
34+
component_metadata: inverter_pb.Metadata,
3535
) -> Optional[ComponentType]:
3636
"""Convert a protobuf InverterType message to Component enum.
3737
3838
For internal-only use by the `microgrid` package.
3939
4040
Args:
4141
component_category: category the type belongs to.
42-
component_type: protobuf enum to convert.
42+
component_metadata: protobuf metadata to fetch type from.
4343
4444
Returns:
4545
Enum value corresponding to the protobuf message.
@@ -51,10 +51,14 @@ def _component_type_from_protobuf(
5151
component_category
5252
== components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
5353
):
54-
if not any(t.value == component_type for t in InverterType):
54+
# mypy 1.4.1 crashes at this line, maybe it doesn't like the name of the "type"
55+
# attribute in this context. Hence the "# type: ignore".
56+
if not any(
57+
t.value == component_metadata.type for t in InverterType # type: ignore
58+
):
5559
return None
5660

57-
return InverterType(component_type)
61+
return InverterType(component_metadata.type)
5862

5963
return None
6064

tests/microgrid/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ async def test_components(self) -> None:
126126
Component(107, ComponentCategory.EV_CHARGER),
127127
}
128128

129+
servicer.set_components(
130+
[
131+
(9, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER),
132+
(666, components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
133+
(999, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY),
134+
]
135+
)
136+
servicer.add_component(
137+
99,
138+
components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
139+
components_pb.InverterType.INVERTER_TYPE_BATTERY,
140+
)
141+
142+
assert set(await microgrid.components()) == {
143+
Component(9, ComponentCategory.METER),
144+
Component(99, ComponentCategory.INVERTER, InverterType.BATTERY),
145+
Component(999, ComponentCategory.BATTERY),
146+
}
147+
129148
finally:
130149
assert await server.graceful_shutdown()
131150

0 commit comments

Comments
 (0)