Skip to content

Commit cd345b3

Browse files
authored
Merge pull request #55 from shsms/fixes
Improve identifier typing
2 parents 228e3c4 + bbf0910 commit cd345b3

File tree

8 files changed

+50
-34
lines changed

8 files changed

+50
-34
lines changed

RELEASE_NOTES.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ This release adds support for retrieving microgrid electrical component connecti
66

77
## Upgrading
88

9-
No migration required. This is a backward-compatible feature addition.
9+
- The `get_microgrid` and `list_microgrid_electrical_components` methods now expect an argument of type `MicrogridId`, instead of an `int`.
10+
- The `PvInverter` type has been renamed to `SolarInverter`, to be compatible with the microgrid api client.
1011

1112
## New Features
1213

14+
- This exposes the abstract `Battery`, `EvCharger` and `Inverter` types.
15+
1316
### Component Connections API
1417

1518
* **New `ComponentConnection` class**: Introduced to represent connections between electrical components in a microgrid
@@ -18,4 +21,4 @@ No migration required. This is a backward-compatible feature addition.
1821

1922
## Bug Fixes
2023

21-
<!-- No bug fixes in this release -->
24+
<!-- No bug fixes in this release -->

src/frequenz/client/assets/_client.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from frequenz.api.assets.v1 import assets_pb2, assets_pb2_grpc
1515
from frequenz.client.base import channel
1616
from frequenz.client.base.client import BaseApiClient, call_stub_method
17+
from frequenz.client.common.microgrid import MicrogridId
18+
from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId
1719

1820
from ._microgrid import Microgrid
1921
from ._microgrid_proto import microgrid_from_proto
@@ -87,7 +89,7 @@ def stub(self) -> assets_pb2_grpc.PlatformAssetsAsyncStub:
8789
return self._stub # type: ignore
8890

8991
async def get_microgrid( # noqa: DOC502 (raises ApiClientError indirectly)
90-
self, microgrid_id: int
92+
self, microgrid_id: MicrogridId
9193
) -> Microgrid:
9294
"""
9395
Get the details of a microgrid.
@@ -105,7 +107,7 @@ async def get_microgrid( # noqa: DOC502 (raises ApiClientError indirectly)
105107
response = await call_stub_method(
106108
self,
107109
lambda: self.stub.GetMicrogrid(
108-
assets_pb2.GetMicrogridRequest(microgrid_id=microgrid_id),
110+
assets_pb2.GetMicrogridRequest(microgrid_id=int(microgrid_id)),
109111
timeout=DEFAULT_GRPC_CALL_TIMEOUT,
110112
),
111113
method_name="GetMicrogrid",
@@ -114,7 +116,7 @@ async def get_microgrid( # noqa: DOC502 (raises ApiClientError indirectly)
114116
return microgrid_from_proto(response.microgrid)
115117

116118
async def list_microgrid_electrical_components(
117-
self, microgrid_id: int
119+
self, microgrid_id: MicrogridId
118120
) -> list[ElectricalComponent]:
119121
"""
120122
Get the electrical components of a microgrid.
@@ -129,7 +131,7 @@ async def list_microgrid_electrical_components(
129131
self,
130132
lambda: self.stub.ListMicrogridElectricalComponents(
131133
assets_pb2.ListMicrogridElectricalComponentsRequest(
132-
microgrid_id=microgrid_id,
134+
microgrid_id=int(microgrid_id),
133135
),
134136
timeout=DEFAULT_GRPC_CALL_TIMEOUT,
135137
),
@@ -142,9 +144,9 @@ async def list_microgrid_electrical_components(
142144

143145
async def list_microgrid_electrical_component_connections(
144146
self,
145-
microgrid_id: int,
146-
source_component_ids: Iterable[int] = (),
147-
destination_component_ids: Iterable[int] = (),
147+
microgrid_id: MicrogridId,
148+
source_component_ids: Iterable[ElectricalComponentId] = (),
149+
destination_component_ids: Iterable[ElectricalComponentId] = (),
148150
) -> list[ComponentConnection | None]:
149151
"""
150152
Get the electrical component connections of a microgrid.
@@ -161,9 +163,9 @@ async def list_microgrid_electrical_component_connections(
161163
The electrical component connections of the microgrid.
162164
"""
163165
request = assets_pb2.ListMicrogridElectricalComponentConnectionsRequest(
164-
microgrid_id=microgrid_id,
165-
source_component_ids=source_component_ids,
166-
destination_component_ids=destination_component_ids,
166+
microgrid_id=int(microgrid_id),
167+
source_component_ids=(int(c) for c in source_component_ids),
168+
destination_component_ids=(int(c) for c in destination_component_ids),
167169
)
168170

169171
response = await call_stub_method(

src/frequenz/client/assets/electrical_component/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Electrical component types."""
55

66
from ._battery import (
7+
Battery,
78
BatteryType,
89
LiIonBattery,
910
NaIonBattery,
@@ -22,6 +23,7 @@
2223
from ._ev_charger import (
2324
AcEvCharger,
2425
DcEvCharger,
26+
EvCharger,
2527
EvChargerType,
2628
HybridEvCharger,
2729
UnrecognizedEvCharger,
@@ -32,8 +34,9 @@
3234
from ._inverter import (
3335
BatteryInverter,
3436
HybridInverter,
37+
Inverter,
3538
InverterType,
36-
PvInverter,
39+
SolarInverter,
3740
UnrecognizedInverter,
3841
UnspecifiedInverter,
3942
)
@@ -53,6 +56,7 @@
5356
__all__ = [
5457
"Chp",
5558
"CryptoMiner",
59+
"Battery",
5660
"BatteryType",
5761
"LiIonBattery",
5862
"NaIonBattery",
@@ -66,6 +70,7 @@
6670
"Electrolyzer",
6771
"AcEvCharger",
6872
"DcEvCharger",
73+
"EvCharger",
6974
"EvChargerType",
7075
"HybridEvCharger",
7176
"UnrecognizedEvCharger",
@@ -74,8 +79,9 @@
7479
"Hvac",
7580
"BatteryInverter",
7681
"HybridInverter",
82+
"Inverter",
7783
"InverterType",
78-
"PvInverter",
84+
"SolarInverter",
7985
"UnrecognizedInverter",
8086
"UnspecifiedInverter",
8187
"Meter",

src/frequenz/client/assets/electrical_component/_connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import dataclasses
77
from datetime import datetime, timezone
88

9-
from frequenz.client.common.microgrid.components import ComponentId
9+
from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId
1010

1111
from .._lifetime import Lifetime
1212

@@ -38,14 +38,14 @@ class ComponentConnection:
3838
when and how the microgrid infrastructure has been modified.
3939
"""
4040

41-
source: ComponentId
41+
source: ElectricalComponentId
4242
"""The unique identifier of the component where the connection originates.
4343
4444
This is aligned with the direction of current flow away from the grid connection
4545
point, or in case of islands, away from the islanding point.
4646
"""
4747

48-
destination: ComponentId
48+
destination: ElectricalComponentId
4949
"""The unique ID of the component where the connection terminates.
5050
5151
This is the component towards which the current flows.

src/frequenz/client/assets/electrical_component/_connection_proto.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
99
electrical_components_pb2,
1010
)
11-
from frequenz.client.common.microgrid.components import ComponentId
11+
from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId
1212

1313
from .._lifetime import Lifetime
1414
from .._lifetime_proto import lifetime_from_proto
@@ -63,8 +63,10 @@ def component_connection_from_proto_with_issues(
6363
`None` if the protobuf message is completely invalid and a
6464
`ComponentConnection` cannot be created.
6565
"""
66-
source_component_id = ComponentId(message.source_electrical_component_id)
67-
destination_component_id = ComponentId(message.destination_electrical_component_id)
66+
source_component_id = ElectricalComponentId(message.source_electrical_component_id)
67+
destination_component_id = ElectricalComponentId(
68+
message.destination_electrical_component_id
69+
)
6870
if source_component_id == destination_component_id:
6971
major_issues.append(
7072
f"connection ignored: source and destination are the same ({source_component_id})",

src/frequenz/client/assets/electrical_component/_electrical_component_proto.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
BatteryInverter,
4848
HybridInverter,
4949
InverterType,
50-
PvInverter,
50+
SolarInverter,
5151
UnrecognizedInverter,
5252
UnspecifiedInverter,
5353
)
@@ -320,12 +320,15 @@ def electrical_component_from_proto_with_issues(
320320
inverter_enum_to_class: dict[
321321
InverterType,
322322
type[
323-
UnspecifiedInverter | BatteryInverter | PvInverter | HybridInverter
323+
UnspecifiedInverter
324+
| BatteryInverter
325+
| SolarInverter
326+
| HybridInverter
324327
],
325328
] = {
326329
InverterType.UNSPECIFIED: UnspecifiedInverter,
327330
InverterType.BATTERY: BatteryInverter,
328-
InverterType.PV: PvInverter,
331+
InverterType.SOLAR: SolarInverter,
329332
InverterType.HYBRID: HybridInverter,
330333
}
331334
inverter_type = enum_proto.enum_from_proto(
@@ -335,7 +338,7 @@ def electrical_component_from_proto_with_issues(
335338
case (
336339
InverterType.UNSPECIFIED
337340
| InverterType.BATTERY
338-
| InverterType.PV
341+
| InverterType.SOLAR
339342
| InverterType.HYBRID
340343
):
341344
if inverter_type is InverterType.UNSPECIFIED:

src/frequenz/client/assets/electrical_component/_inverter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class InverterType(enum.Enum):
2525
BATTERY = electrical_components_pb2.INVERTER_TYPE_BATTERY
2626
"""The inverter is a battery inverter."""
2727

28-
PV = electrical_components_pb2.INVERTER_TYPE_PV
28+
SOLAR = electrical_components_pb2.INVERTER_TYPE_PV
2929
"""The inverter is a solar inverter."""
3030

3131
HYBRID = electrical_components_pb2.INVERTER_TYPE_HYBRID
@@ -106,10 +106,10 @@ class BatteryInverter(Inverter):
106106

107107

108108
@dataclasses.dataclass(frozen=True, kw_only=True)
109-
class PvInverter(Inverter):
110-
"""A PV inverter."""
109+
class SolarInverter(Inverter):
110+
"""A Solar inverter."""
111111

112-
type: Literal[InverterType.PV] = InverterType.PV
112+
type: Literal[InverterType.SOLAR] = InverterType.SOLAR
113113
"""The type of this inverter.
114114
115115
Note:
@@ -150,7 +150,7 @@ class UnrecognizedInverter(Inverter):
150150
InverterTypes: TypeAlias = (
151151
UnspecifiedInverter
152152
| BatteryInverter
153-
| PvInverter
153+
| SolarInverter
154154
| HybridInverter
155155
| UnrecognizedInverter
156156
)

tests/client_test_cases/list_microgrid_electrical_component_connections/success_case.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
electrical_components_pb2,
1313
)
1414
from frequenz.client.base.conversion import to_timestamp
15-
from frequenz.client.common.microgrid.components import ComponentId
15+
from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId
1616

1717
from frequenz.client.assets import Lifetime
1818
from frequenz.client.assets.electrical_component import ComponentConnection
@@ -50,12 +50,12 @@ def assert_client_result(actual_result: Any) -> None:
5050
"""Assert that the client result matches the expected connections list."""
5151
assert list(actual_result) == [
5252
ComponentConnection(
53-
source=ComponentId(1),
54-
destination=ComponentId(2),
53+
source=ElectricalComponentId(1),
54+
destination=ElectricalComponentId(2),
5555
),
5656
ComponentConnection(
57-
source=ComponentId(2),
58-
destination=ComponentId(3),
57+
source=ElectricalComponentId(2),
58+
destination=ElectricalComponentId(3),
5959
operational_lifetime=Lifetime(start=lifetime_start),
6060
),
6161
]

0 commit comments

Comments
 (0)