Skip to content

Commit 7a4fd1f

Browse files
committed
refactor: Adapt code to v1alpha8 API of frequenz-client-common
The `frequenz-client-common` dependency has been updated, which introduced breaking changes from the new `v1alpha8` API. This commit adapts the codebase to these changes, resolving the resulting `nox` failures across `mypy`, `pytest`, and formatters. Key changes include: - Replacing the deprecated `ComponentCategory` with `v1alpha8`'s `ElectricalComponentCategory`. - Updating enum members to match the new API, such as `ElectricalComponentCategory.GRID` to `GRID_CONNECTION_POINT` and `InverterType.PV` to `InverterType.SOLAR`. - Adjusting `Event` enum usage (e.g., `Event.CREATED` is now `Event.EVENT_CREATED`). Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 58c00f1 commit 7a4fd1f

File tree

11 files changed

+115
-88
lines changed

11 files changed

+115
-88
lines changed

RELEASE_NOTES.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
## Upgrading
88

9+
* The client has been updated to support breaking changes in the `v1alpha8` API of the `frequenz-client-common` dependency.
10+
* `ComponentCategory` has been replaced with `ElectricalComponentCategory`.
11+
* Enum members were updated to align with the new API (e.g., `ElectricalComponentCategory.GRID` is now `ElectricalComponentCategory.GRID_CONNECTION_POINT`).
12+
* `Event` enum usage was updated (e.g., `Event.CREATED` is now `Event.EVENT_CREATED`).
13+
* The `DispatchApiClient.list()` method signature was updated to use new filter argument names.
14+
915
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
1016

1117
## New Features
@@ -19,3 +25,11 @@
1925
## Bug Fixes
2026

2127
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
28+
29+
## Changed
30+
31+
* Adapted the client to breaking changes in the `v1alpha8` API of the `frequenz-client-common` dependency.
32+
* Replaced `ComponentCategory` with `ElectricalComponentCategory`.
33+
* Updated various enum members to match the new API (e.g., `ElectricalComponentCategory.GRID` is now `ElectricalComponentCategory.GRID_CONNECTION_POINT`).
34+
* Aligned `Event` enum usage with the new API (e.g., `Event.CREATED` is now `Event.EVENT_CREATED`).
35+
* Updated `DispatchApiClient.list()` method signature to use new filter argument names.

pyproject.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ classifiers = [
3737
requires-python = ">= 3.11, < 4"
3838
dependencies = [
3939
"typing-extensions >= 4.13.0, < 5",
40-
"frequenz-api-dispatch == 1.0.0-rc2",
40+
"frequenz-api-dispatch == 1.0.0-rc3",
4141
"frequenz-client-base >= 0.11.0, < 0.12.0",
42-
"frequenz-client-common >= 0.3.2, < 0.4.0",
42+
"frequenz-client-common >= 0.3.4, < 0.4.0",
4343
"frequenz-core >= 1.0.2, < 2.0.0",
44-
"grpcio >= 1.70.0, < 2",
44+
"grpcio >= 1.72.1, < 2",
4545
"python-dateutil >= 2.8.2, < 3.0",
4646
]
4747
dynamic = ["version"]
@@ -75,7 +75,7 @@ dev-mkdocs = [
7575
"mike == 2.1.3",
7676
"mkdocs-gen-files == 0.5.0",
7777
"mkdocs-literate-nav == 0.6.2",
78-
"frequenz-api-dispatch == 1.0.0-rc2",
78+
"frequenz-api-dispatch == 1.0.0-rc3",
7979
"mkdocs-macros-plugin == 1.3.7",
8080
"mkdocs-material == 9.6.16",
8181
"mkdocstrings[python] == 0.30.0",
@@ -95,7 +95,7 @@ dev-pylint = [
9595
"pylint == 3.3.7",
9696
# For checking the noxfile, docs/ script, and tests
9797
"frequenz-client-dispatch[cli,dev-mkdocs,dev-noxfile,dev-pytest]",
98-
"frequenz-api-dispatch == 1.0.0-rc2",
98+
"frequenz-api-dispatch == 1.0.0-rc3",
9999
]
100100
dev-pytest = [
101101
"pytest == 8.4.1",

src/frequenz/client/dispatch/_cli_types.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import parsedatetime # type: ignore
1313
from tzlocal import get_localzone
1414

15-
from frequenz.client.common.microgrid.components import ComponentCategory
15+
from frequenz.client.common.v1alpha8.microgrid.electrical_components import (
16+
ElectricalComponentCategory,
17+
)
1618
from frequenz.client.dispatch.types import (
1719
BatteryType,
1820
EvChargerType,
@@ -177,11 +179,11 @@ def convert(
177179

178180
def enum_from_str(
179181
name: str,
180-
) -> InverterType | BatteryType | EvChargerType | ComponentCategory:
182+
) -> InverterType | BatteryType | EvChargerType | ElectricalComponentCategory:
181183
"""Convert a string to an enum member."""
182184
name = name.strip().upper()
183-
if name in ComponentCategory.__members__:
184-
return ComponentCategory[name]
185+
if name in ElectricalComponentCategory.__members__:
186+
return ElectricalComponentCategory[name]
185187
if name in InverterType.__members__:
186188
return InverterType[name]
187189
if name in BatteryType.__members__:
@@ -208,7 +210,7 @@ def enum_from_str(
208210
"- METER,INVERTER # A list of component categories\n"
209211
"- NA_ION,SOLAR # A list of component category types (category is derived)\n"
210212
"Valid categories:\n"
211-
f"{', '.join([cat.name for cat in ComponentCategory])}\n"
213+
f"{', '.join([cat.name for cat in ElectricalComponentCategory])}\n"
212214
"Valid types:\n"
213215
f"{types_str}\n",
214216
param,

src/frequenz/client/dispatch/_client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
from datetime import datetime, timedelta
88
from typing import Any, AsyncIterator, Awaitable, Iterator, Literal, cast
99

10-
# pylint: disable=no-name-in-module
11-
from frequenz.api.common.v1.pagination.pagination_params_pb2 import PaginationParams
10+
# pylint: disable-next=no-name-in-module
11+
from frequenz.api.common.v1alpha8.pagination.pagination_params_pb2 import (
12+
PaginationParams,
13+
)
14+
from frequenz.api.common.v1alpha8.types.interval_pb2 import Interval as PBInterval
1215
from frequenz.api.dispatch.v1 import dispatch_pb2_grpc
1316
from frequenz.api.dispatch.v1.dispatch_pb2 import (
1417
CreateMicrogridDispatchResponse,
@@ -20,11 +23,6 @@
2023
ListMicrogridDispatchesResponse,
2124
StreamMicrogridDispatchesRequest,
2225
StreamMicrogridDispatchesResponse,
23-
)
24-
from frequenz.api.dispatch.v1.dispatch_pb2 import (
25-
TimeIntervalFilter as PBTimeIntervalFilter,
26-
)
27-
from frequenz.api.dispatch.v1.dispatch_pb2 import (
2826
UpdateMicrogridDispatchRequest,
2927
UpdateMicrogridDispatchResponse,
3028
)
@@ -170,11 +168,9 @@ async def list(
170168

171169
def to_interval(
172170
from_: datetime | None, to: datetime | None
173-
) -> PBTimeIntervalFilter | None:
171+
) -> PBInterval | None:
174172
return (
175-
PBTimeIntervalFilter(
176-
from_time=to_timestamp(from_), to_time=to_timestamp(to)
177-
)
173+
PBInterval(start_time=to_timestamp(from_), end_time=to_timestamp(to))
178174
if from_ or to
179175
else None
180176
)

src/frequenz/client/dispatch/test/_service.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import grpc.aio
1515

1616
# pylint: disable=no-name-in-module
17-
from frequenz.api.common.v1.pagination.pagination_info_pb2 import PaginationInfo
17+
from frequenz.api.common.v1alpha8.pagination.pagination_info_pb2 import PaginationInfo
18+
from frequenz.api.common.v1alpha8.streaming.event_pb2 import Event as PBEvent
1819
from frequenz.api.dispatch.v1.dispatch_pb2 import (
1920
CreateMicrogridDispatchRequest as PBDispatchCreateRequest,
2021
)
@@ -36,9 +37,10 @@
3637
# pylint: enable=no-name-in-module
3738
from frequenz.client.base.conversion import to_datetime as _to_dt
3839
from frequenz.client.common.microgrid import MicrogridId
40+
from frequenz.client.common.v1alpha8.streaming import Event
3941

4042
from .._internal_types import DispatchCreateRequest
41-
from ..types import Dispatch, DispatchEvent, DispatchId, Event
43+
from ..types import Dispatch, DispatchEvent, DispatchId
4244

4345
_logger = logging.getLogger(__name__)
4446

@@ -148,20 +150,20 @@ def _filter_dispatch(
148150
if target != dispatch.target:
149151
return False
150152
if _filter.HasField("start_time_interval"):
151-
if start_from := _filter.start_time_interval.from_time:
153+
if start_from := _filter.start_time_interval.start_time:
152154
if dispatch.start_time < _to_dt(start_from):
153155
return False
154-
if start_to := _filter.start_time_interval.to_time:
156+
if start_to := _filter.start_time_interval.end_time:
155157
if dispatch.start_time >= _to_dt(start_to):
156158
return False
157159
if _filter.HasField("end_time_interval"):
158-
if end_from := _filter.end_time_interval.from_time:
160+
if end_from := _filter.end_time_interval.start_time:
159161
if (
160162
dispatch.duration
161163
and dispatch.start_time + dispatch.duration < _to_dt(end_from)
162164
):
163165
return False
164-
if end_to := _filter.end_time_interval.to_time:
166+
if end_to := _filter.end_time_interval.end_time:
165167
if (
166168
dispatch.duration
167169
and dispatch.start_time + dispatch.duration >= _to_dt(end_to)
@@ -198,7 +200,9 @@ async def CreateMicrogridDispatch(
198200
await self._stream_sender.send(
199201
self.StreamEvent(
200202
microgrid_id,
201-
DispatchEvent(dispatch=new_dispatch, event=Event.CREATED),
203+
DispatchEvent(
204+
dispatch=new_dispatch, event=Event(PBEvent.EVENT_CREATED)
205+
),
202206
)
203207
)
204208

@@ -289,7 +293,7 @@ async def UpdateMicrogridDispatch(
289293
await self._stream_sender.send(
290294
self.StreamEvent(
291295
microgrid_id,
292-
DispatchEvent(dispatch=dispatch, event=Event.UPDATED),
296+
DispatchEvent(dispatch=dispatch, event=Event(PBEvent.EVENT_UPDATED)),
293297
)
294298
)
295299

@@ -347,7 +351,7 @@ async def DeleteMicrogridDispatch(
347351
microgrid_id,
348352
DispatchEvent(
349353
dispatch=dispatch_to_delete,
350-
event=Event.DELETED,
354+
event=Event(PBEvent.EVENT_DELETED),
351355
),
352356
)
353357
)

src/frequenz/client/dispatch/test/generator.py

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

9-
from frequenz.client.common.microgrid.components import ComponentCategory
9+
from frequenz.client.common.v1alpha8.microgrid.electrical_components import (
10+
ElectricalComponentCategory,
11+
)
1012

1113
from .._internal_types import rounded_start_time
1214
from ..recurrence import EndCriteria, Frequency, RecurrenceRule, Weekday
@@ -82,15 +84,15 @@ def generate_target_category_and_type(self) -> TargetCategory:
8284
Returns:
8385
a random category and type
8486
"""
85-
category = self._rng.choice(list(ComponentCategory)[1:])
87+
category = self._rng.choice(list(ElectricalComponentCategory)[1:])
8688
category_type: BatteryType | InverterType | EvChargerType | None = None
8789

8890
match category:
89-
case ComponentCategory.BATTERY:
91+
case ElectricalComponentCategory.BATTERY:
9092
category_type = self._rng.choice(list(BatteryType)[1:])
91-
case ComponentCategory.INVERTER:
93+
case ElectricalComponentCategory.INVERTER:
9294
category_type = self._rng.choice(list(InverterType)[1:])
93-
case ComponentCategory.EV_CHARGER:
95+
case ElectricalComponentCategory.EV_CHARGER:
9496
category_type = self._rng.choice(list(EvChargerType)[1:])
9597
case _:
9698
category_type = None
@@ -116,7 +118,7 @@ def generate_dispatch(self) -> Dispatch:
116118
*[
117119
# Not yet used
118120
# self.generate_target_category_and_type()
119-
self._rng.choice(list(ComponentCategory)[1:])
121+
self._rng.choice(list(ElectricalComponentCategory)[1:])
120122
for _ in range(self._rng.randint(1, 10))
121123
]
122124
),

src/frequenz/client/dispatch/types.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
from typing import Any, Self, SupportsInt, TypeAlias, cast, final
1111

1212
# pylint: enable=no-name-in-module
13-
from frequenz.api.common.v1.microgrid.components.battery_pb2 import (
13+
from frequenz.api.common.v1alpha8.microgrid.electrical_components.electrical_components_pb2 import (
1414
BatteryType as PBBatteryType,
1515
)
16-
from frequenz.api.common.v1.microgrid.components.ev_charger_pb2 import (
16+
from frequenz.api.common.v1alpha8.microgrid.electrical_components.electrical_components_pb2 import (
1717
EvChargerType as PBEvChargerType,
1818
)
19-
from frequenz.api.common.v1.microgrid.components.inverter_pb2 import (
19+
from frequenz.api.common.v1alpha8.microgrid.electrical_components.electrical_components_pb2 import (
2020
InverterType as PBInverterType,
2121
)
2222

@@ -33,7 +33,10 @@
3333
from google.protobuf.struct_pb2 import Struct
3434

3535
from frequenz.client.base.conversion import to_datetime, to_timestamp
36-
from frequenz.client.common.microgrid.components import ComponentCategory
36+
from frequenz.client.common.v1alpha8.microgrid.electrical_components import (
37+
ElectricalComponentCategory,
38+
)
39+
from frequenz.client.common.v1alpha8.streaming import Event
3740

3841
from .recurrence import Frequency, RecurrenceRule, Weekday
3942

@@ -81,7 +84,7 @@ class InverterType(Enum):
8184
BATTERY = PBInverterType.INVERTER_TYPE_BATTERY
8285
"""Battery inverter."""
8386

84-
SOLAR = PBInverterType.INVERTER_TYPE_SOLAR
87+
PV = PBInverterType.INVERTER_TYPE_PV
8588
"""Solar inverter."""
8689

8790
HYBRID = PBInverterType.INVERTER_TYPE_HYBRID
@@ -92,28 +95,28 @@ class InverterType(Enum):
9295
class TargetCategory:
9396
"""Represents a category and optionally a type."""
9497

95-
target: ComponentCategory | BatteryType | EvChargerType | InverterType
98+
target: ElectricalComponentCategory | BatteryType | EvChargerType | InverterType
9699
"""The target category of the dispatch.
97100
98101
Implicitly derived from the types.
99102
"""
100103

101104
@property
102-
def category(self) -> ComponentCategory:
105+
def category(self) -> ElectricalComponentCategory:
103106
"""Get the category of the target.
104107
105108
Returns:
106109
The category of the target.
107110
"""
108111
match self.target:
109-
case ComponentCategory():
112+
case ElectricalComponentCategory():
110113
return self.target
111114
case BatteryType():
112-
return ComponentCategory.BATTERY
115+
return ElectricalComponentCategory.BATTERY
113116
case EvChargerType():
114-
return ComponentCategory.EV_CHARGER
117+
return ElectricalComponentCategory.EV_CHARGER
115118
case InverterType():
116-
return ComponentCategory.INVERTER
119+
return ElectricalComponentCategory.INVERTER
117120

118121
@property
119122
def type(self) -> BatteryType | EvChargerType | InverterType | None:
@@ -151,7 +154,11 @@ def __new__(cls, *ids: SupportsInt) -> Self:
151154

152155
# Define the union of types that can be passed to TargetCategories constructor
153156
TargetCategoryInputType = (
154-
TargetCategory | ComponentCategory | BatteryType | InverterType | EvChargerType
157+
TargetCategory
158+
| ElectricalComponentCategory
159+
| BatteryType
160+
| InverterType
161+
| EvChargerType
155162
)
156163
"""Type for the input to TargetCategories constructor."""
157164

@@ -181,7 +188,8 @@ def __new__(cls, *categories_input: TargetCategoryInputType) -> Self:
181188
if isinstance(item, TargetCategory):
182189
processed_elements.append(item)
183190
elif isinstance(
184-
item, (ComponentCategory, BatteryType, InverterType, EvChargerType)
191+
item,
192+
(ElectricalComponentCategory, BatteryType, InverterType, EvChargerType),
185193
):
186194
# Wrap raw categories/types into TargetCategory instances
187195
processed_elements.append(TargetCategory(target=item))
@@ -236,15 +244,15 @@ def _target_components_from_protobuf(
236244
case "component_categories":
237245
return TargetCategories(
238246
*map(
239-
ComponentCategory.from_proto,
247+
ElectricalComponentCategory.from_proto,
240248
pb_target.component_categories.categories,
241249
)
242250
)
243251
case "component_categories_types":
244252
return TargetCategories(
245253
*map(
246254
lambda cat_and_type: _extract_category_type(cat_and_type)
247-
or ComponentCategory.from_proto(cat_and_type.category),
255+
or ElectricalComponentCategory.from_proto(cat_and_type.category),
248256
pb_target.component_categories_types.categories,
249257
)
250258
)
@@ -617,15 +625,6 @@ def to_protobuf(self) -> PBDispatch:
617625
)
618626

619627

620-
class Event(Enum):
621-
"""Enum representing the type of event that occurred during a dispatch operation."""
622-
623-
UNSPECIFIED = StreamMicrogridDispatchesResponse.Event.EVENT_UNSPECIFIED
624-
CREATED = StreamMicrogridDispatchesResponse.Event.EVENT_CREATED
625-
UPDATED = StreamMicrogridDispatchesResponse.Event.EVENT_UPDATED
626-
DELETED = StreamMicrogridDispatchesResponse.Event.EVENT_DELETED
627-
628-
629628
@dataclass(kw_only=True, frozen=True)
630629
class DispatchEvent:
631630
"""Represents an event that occurred during a dispatch operation."""

0 commit comments

Comments
 (0)