Skip to content

Commit 1638430

Browse files
committed
Use ComponentCategory from frequenz-api-common package
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent be64e00 commit 1638430

File tree

7 files changed

+78
-70
lines changed

7 files changed

+78
-70
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import grpc
2323
from frequenz.api.microgrid import common_pb2 as common_pb
24+
from frequenz.api.common import components_pb2 as components_pb
2425
from frequenz.api.microgrid import microgrid_pb2 as microgrid_pb
2526
from frequenz.api.microgrid.microgrid_pb2_grpc import MicrogridStub
2627
from frequenz.channels import Broadcast, Receiver, Sender
@@ -244,7 +245,7 @@ async def components(self) -> Iterable[Component]:
244245
)
245246
components_only = filter(
246247
lambda c: c.category
247-
is not microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
248+
is not components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
248249
component_list.components,
249250
)
250251
result: Iterable[Component] = map(

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from enum import Enum
1010
from typing import Optional
1111

12+
import frequenz.api.common.components_pb2 as components_pb
1213
import frequenz.api.microgrid.inverter_pb2 as inverter_pb
13-
import frequenz.api.microgrid.microgrid_pb2 as microgrid_pb
1414

1515

1616
class ComponentType(Enum):
@@ -27,7 +27,7 @@ class InverterType(ComponentType):
2727

2828

2929
def _component_type_from_protobuf(
30-
component_category: microgrid_pb.ComponentCategory.ValueType,
30+
component_category: components_pb.ComponentCategory.ValueType,
3131
component_type: inverter_pb.Type.ValueType,
3232
) -> Optional[ComponentType]:
3333
"""Convert a protobuf InverterType message to Component enum.
@@ -44,7 +44,10 @@ def _component_type_from_protobuf(
4444
# ComponentType values in the protobuf definition are not unique across categories
4545
# as of v0.11.0, so we need to check the component category first, before doing any
4646
# component type checks.
47-
if component_category == microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER:
47+
if (
48+
component_category
49+
== components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
50+
):
4851
if not any(t.value == component_type for t in InverterType):
4952
return None
5053

@@ -56,21 +59,21 @@ def _component_type_from_protobuf(
5659
class ComponentCategory(Enum):
5760
"""Possible types of microgrid component."""
5861

59-
NONE = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
60-
GRID = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_GRID
61-
METER = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
62-
INVERTER = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
63-
BATTERY = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
64-
EV_CHARGER = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
65-
CHP = microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_CHP
62+
NONE = components_pb.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
63+
GRID = components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID
64+
METER = components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
65+
INVERTER = components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
66+
BATTERY = components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
67+
EV_CHARGER = components_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
68+
CHP = components_pb.ComponentCategory.COMPONENT_CATEGORY_CHP
6669

6770
# types not yet supported by the API but which can be inferred
6871
# from available graph info
6972
PV_ARRAY = 1000001
7073

7174

7275
def _component_category_from_protobuf(
73-
component_category: microgrid_pb.ComponentCategory.ValueType,
76+
component_category: components_pb.ComponentCategory.ValueType,
7477
) -> ComponentCategory:
7578
"""Convert a protobuf ComponentCategory message to ComponentCategory enum.
7679
@@ -87,7 +90,7 @@ def _component_category_from_protobuf(
8790
a valid component category as it does not form part of the
8891
microgrid itself)
8992
"""
90-
if component_category == microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR:
93+
if component_category == components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR:
9194
raise ValueError("Cannot create a component from a sensor!")
9295

9396
if not any(t.value == component_category for t in ComponentCategory):

tests/actor/test_data_sourcing.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Tests for the DataSourcingActor.
66
"""
77

8-
from frequenz.api.microgrid import microgrid_pb2
8+
from frequenz.api.common import components_pb2 as components_pb
99
from frequenz.channels import Broadcast
1010

1111
from frequenz.sdk.actor import (
@@ -28,19 +28,19 @@ async def test_data_sourcing_actor(self) -> None:
2828
await server.start()
2929

3030
servicer.add_component(
31-
1, microgrid_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID
31+
1, components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID
3232
)
3333
servicer.add_component(
34-
4, microgrid_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
34+
4, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
3535
)
3636
servicer.add_component(
37-
7, microgrid_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
37+
7, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
3838
)
3939
servicer.add_component(
40-
8, microgrid_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
40+
8, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
4141
)
4242
servicer.add_component(
43-
9, microgrid_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY
43+
9, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
4444
)
4545

4646
servicer.add_connection(1, 4)

tests/microgrid/test_client.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import grpc
1111
import pytest
1212
from frequenz.api.microgrid import common_pb2 as common_pb
13+
from frequenz.api.common import components_pb2 as components_pb
1314
from frequenz.api.microgrid import microgrid_pb2 as microgrid_pb
1415
from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module
1516

@@ -51,22 +52,22 @@ async def test_components(self) -> None:
5152
assert set(await microgrid.components()) == set()
5253

5354
servicer.add_component(
54-
0, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
55+
0, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
5556
)
5657
assert set(await microgrid.components()) == {
5758
Component(0, ComponentCategory.METER)
5859
}
5960

6061
servicer.add_component(
61-
0, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
62+
0, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
6263
)
6364
assert set(await microgrid.components()) == {
6465
Component(0, ComponentCategory.METER),
6566
Component(0, ComponentCategory.BATTERY),
6667
}
6768

6869
servicer.add_component(
69-
0, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
70+
0, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
7071
)
7172
assert set(await microgrid.components()) == {
7273
Component(0, ComponentCategory.METER),
@@ -76,7 +77,7 @@ async def test_components(self) -> None:
7677

7778
# sensors are not counted as components by the API client
7879
servicer.add_component(
79-
1, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR
80+
1, components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR
8081
)
8182
assert set(await microgrid.components()) == {
8283
Component(0, ComponentCategory.METER),
@@ -86,10 +87,10 @@ async def test_components(self) -> None:
8687

8788
servicer.set_components(
8889
[
89-
(9, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER),
90-
(99, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER),
91-
(666, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
92-
(999, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY),
90+
(9, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER),
91+
(99, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER),
92+
(666, components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
93+
(999, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY),
9394
]
9495
)
9596
assert set(await microgrid.components()) == {
@@ -100,17 +101,20 @@ async def test_components(self) -> None:
100101

101102
servicer.set_components(
102103
[
103-
(99, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
104+
(99, components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
104105
(
105106
100,
106-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED,
107+
components_pb.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED,
107108
),
108-
(101, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_GRID),
109-
(104, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER),
110-
(105, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER),
111-
(106, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY),
112-
(107, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER),
113-
(999, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
109+
(101, components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID),
110+
(104, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER),
111+
(105, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER),
112+
(106, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY),
113+
(
114+
107,
115+
components_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER,
116+
),
117+
(999, components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR),
114118
]
115119
)
116120
assert set(await microgrid.components()) == {
@@ -141,11 +145,11 @@ async def test_connections(self) -> None:
141145
servicer.add_connection(7, 9)
142146
servicer.add_component(
143147
7,
144-
component_category=microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
148+
component_category=components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
145149
)
146150
servicer.add_component(
147151
9,
148-
component_category=microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
152+
component_category=components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER,
149153
)
150154
assert set(await microgrid.connections()) == {
151155
Connection(0, 0),
@@ -163,7 +167,7 @@ async def test_connections(self) -> None:
163167
for component_id in [999, 99, 19, 909, 101, 91]:
164168
servicer.add_component(
165169
component_id,
166-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
170+
components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
167171
)
168172

169173
assert set(await microgrid.connections()) == {
@@ -176,7 +180,7 @@ async def test_connections(self) -> None:
176180
for component_id in [1, 2, 3, 4, 5, 6, 7, 8]:
177181
servicer.add_component(
178182
component_id,
179-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
183+
components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
180184
)
181185

182186
servicer.set_connections(
@@ -308,7 +312,7 @@ def ListAllComponents(
308312
for component_id in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
309313
servicer.add_component(
310314
component_id,
311-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
315+
components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY,
312316
)
313317
servicer.set_connections(
314318
[
@@ -359,10 +363,10 @@ async def test_meter_data(self) -> None:
359363
microgrid = self.create_client(57899)
360364

361365
servicer.add_component(
362-
83, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
366+
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
363367
)
364368
servicer.add_component(
365-
38, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
369+
38, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
366370
)
367371

368372
with pytest.raises(ValueError):
@@ -391,10 +395,10 @@ async def test_battery_data(self) -> None:
391395
microgrid = self.create_client(57899)
392396

393397
servicer.add_component(
394-
83, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
398+
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
395399
)
396400
servicer.add_component(
397-
38, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
401+
38, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
398402
)
399403

400404
with pytest.raises(ValueError):
@@ -423,10 +427,10 @@ async def test_inverter_data(self) -> None:
423427
microgrid = self.create_client(57899)
424428

425429
servicer.add_component(
426-
83, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
430+
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
427431
)
428432
servicer.add_component(
429-
38, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
433+
38, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
430434
)
431435

432436
with pytest.raises(ValueError):
@@ -455,10 +459,10 @@ async def test_ev_charger_data(self) -> None:
455459
microgrid = self.create_client(57899)
456460

457461
servicer.add_component(
458-
83, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
462+
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
459463
)
460464
servicer.add_component(
461-
38, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
465+
38, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
462466
)
463467

464468
with pytest.raises(ValueError):
@@ -489,7 +493,7 @@ async def test_charge(self) -> None:
489493
microgrid = self.create_client(57899)
490494

491495
servicer.add_component(
492-
83, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
496+
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
493497
)
494498

495499
await microgrid.set_power(component_id=83, power_w=12)
@@ -512,7 +516,7 @@ async def test_discharge(self) -> None:
512516
microgrid = self.create_client(57899)
513517

514518
servicer.add_component(
515-
73, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
519+
73, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
516520
)
517521

518522
await microgrid.set_power(component_id=73, power_w=-15)
@@ -532,7 +536,7 @@ async def test_set_bounds(self) -> None:
532536
microgrid = self.create_client(57899)
533537

534538
servicer.add_component(
535-
38, microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
539+
38, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
536540
)
537541

538542
num_calls = 4

tests/microgrid/test_component.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Tests for the microgrid component wrapper.
66
"""
77

8-
import frequenz.api.microgrid.microgrid_pb2 as microgrid_pb
8+
import frequenz.api.common.components_pb2 as components_pb
99
import pytest
1010

1111
import frequenz.sdk.microgrid.component._component as cp
@@ -16,42 +16,42 @@ def test_component_category_from_protobuf() -> None:
1616
"""Test the creating component category from protobuf."""
1717
assert (
1818
cp._component_category_from_protobuf(
19-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
19+
components_pb.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
2020
)
2121
== cp.ComponentCategory.NONE
2222
)
2323

2424
assert (
2525
cp._component_category_from_protobuf(
26-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_GRID
26+
components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID
2727
)
2828
== cp.ComponentCategory.GRID
2929
)
3030

3131
assert (
3232
cp._component_category_from_protobuf(
33-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_METER
33+
components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
3434
)
3535
== cp.ComponentCategory.METER
3636
)
3737

3838
assert (
3939
cp._component_category_from_protobuf(
40-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
40+
components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
4141
)
4242
== cp.ComponentCategory.INVERTER
4343
)
4444

4545
assert (
4646
cp._component_category_from_protobuf(
47-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
47+
components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
4848
)
4949
== cp.ComponentCategory.BATTERY
5050
)
5151

5252
assert (
5353
cp._component_category_from_protobuf(
54-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
54+
components_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
5555
)
5656
== cp.ComponentCategory.EV_CHARGER
5757
)
@@ -60,7 +60,7 @@ def test_component_category_from_protobuf() -> None:
6060

6161
with pytest.raises(ValueError):
6262
cp._component_category_from_protobuf(
63-
microgrid_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR
63+
components_pb.ComponentCategory.COMPONENT_CATEGORY_SENSOR
6464
)
6565

6666

0 commit comments

Comments
 (0)