Skip to content

Commit 1fa8efe

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 1fa8efe

File tree

11 files changed

+194
-121
lines changed

11 files changed

+194
-121
lines changed

RELEASE_NOTES.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
## Summary
44

5-
<!-- Here goes a general summary of what this release is about -->
5+
This release updates the `frequenz-api-dispatch` dependency to `v1.0.0-rc3` and adapts the client to the latest API changes. It introduces more specific component categories and new event names while maintaining backward compatibility.
66

77
## Upgrading
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
* `InverterType.SOLAR` is now deprecated in favor of `InverterType.PV`. The old name can still be used but will be removed in a future release.
10+
* The following dependencies were updated:
11+
* `frequenz-api-dispatch` to `v1.0.0-rc3`
12+
* `frequenz-client-common` to `v0.3.5`
13+
* `grpcio` to `v1.72.1`
1014

1115
## New Features
1216

17+
* **`ElectricalComponentCategory`**: The client now uses the more specific `frequenz.client.common.microgrid.electrical_components.ElectricalComponentCategory` for targeting components.
18+
* The new property `TargetCategory.ecategory` will return an `ElectricalComponentCategory`.
19+
* The existing `TargetCategory.category` property is preserved for backward compatibility and returns the corresponding `ComponentCategory`.
20+
* **New Event Names**: The `dispatch.Event` enum now includes new members prefixed with `EVENT_` (e.g., `EVENT_CREATED`). The old names (`CREATED`, `UPDATED`, `DELETED`) are kept as aliases for backward compatibility.
1321
* Support secrets for signing and verifying messages.
1422
* Use the new env variable `DISPATCH_API_SIGN_SECRET` to set the secret key.
1523
* Use the new `sign_secret` parameter in the `DispatchClient` constructor to set the secret key.

pyproject.toml

Lines changed: 6 additions & 6 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, < 2",
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.5, < 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"]
@@ -64,7 +64,7 @@ cli = [
6464
dev-flake8 = [
6565
"flake8 == 7.3.0",
6666
"flake8-docstrings == 1.7.0",
67-
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
67+
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
6868
"pydoclint == 0.6.6",
6969
"pydocstyle == 6.3.0",
7070
]
@@ -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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from tzlocal import get_localzone
1414

1515
from frequenz.client.common.microgrid.components import ComponentCategory
16+
from frequenz.client.common.microgrid.electrical_components import (
17+
ElectricalComponentCategory,
18+
)
1619
from frequenz.client.dispatch.types import (
1720
BatteryType,
1821
EvChargerType,
@@ -177,9 +180,17 @@ def convert(
177180

178181
def enum_from_str(
179182
name: str,
180-
) -> InverterType | BatteryType | EvChargerType | ComponentCategory:
183+
) -> (
184+
InverterType
185+
| BatteryType
186+
| EvChargerType
187+
| ElectricalComponentCategory
188+
| ComponentCategory
189+
):
181190
"""Convert a string to an enum member."""
182191
name = name.strip().upper()
192+
if name in ElectricalComponentCategory.__members__:
193+
return ElectricalComponentCategory[name]
183194
if name in ComponentCategory.__members__:
184195
return ComponentCategory[name]
185196
if name in InverterType.__members__:
@@ -208,7 +219,7 @@ def enum_from_str(
208219
"- METER,INVERTER # A list of component categories\n"
209220
"- NA_ION,SOLAR # A list of component category types (category is derived)\n"
210221
"Valid categories:\n"
211-
f"{', '.join([cat.name for cat in ComponentCategory])}\n"
222+
f"{', '.join([cat.name for cat in ElectricalComponentCategory])}\n"
212223
"Valid types:\n"
213224
f"{types_str}\n",
214225
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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
1818
from frequenz.api.dispatch.v1.dispatch_pb2 import (
1919
CreateMicrogridDispatchRequest as PBDispatchCreateRequest,
2020
)
@@ -131,7 +131,7 @@ async def StreamMicrogridDispatches(
131131
_logger.debug("Received message: %s", message)
132132
if message.microgrid_id == MicrogridId(request.microgrid_id):
133133
response = StreamMicrogridDispatchesResponse(
134-
event=message.event.event.value,
134+
event=message.event.event.value.value,
135135
dispatch=message.event.dispatch.to_protobuf(),
136136
)
137137
yield response
@@ -148,20 +148,20 @@ def _filter_dispatch(
148148
if target != dispatch.target:
149149
return False
150150
if _filter.HasField("start_time_interval"):
151-
if start_from := _filter.start_time_interval.from_time:
151+
if start_from := _filter.start_time_interval.start_time:
152152
if dispatch.start_time < _to_dt(start_from):
153153
return False
154-
if start_to := _filter.start_time_interval.to_time:
154+
if start_to := _filter.start_time_interval.end_time:
155155
if dispatch.start_time >= _to_dt(start_to):
156156
return False
157157
if _filter.HasField("end_time_interval"):
158-
if end_from := _filter.end_time_interval.from_time:
158+
if end_from := _filter.end_time_interval.start_time:
159159
if (
160160
dispatch.duration
161161
and dispatch.start_time + dispatch.duration < _to_dt(end_from)
162162
):
163163
return False
164-
if end_to := _filter.end_time_interval.to_time:
164+
if end_to := _filter.end_time_interval.end_time:
165165
if (
166166
dispatch.duration
167167
and dispatch.start_time + dispatch.duration >= _to_dt(end_to)
@@ -198,7 +198,7 @@ async def CreateMicrogridDispatch(
198198
await self._stream_sender.send(
199199
self.StreamEvent(
200200
microgrid_id,
201-
DispatchEvent(dispatch=new_dispatch, event=Event.CREATED),
201+
DispatchEvent(dispatch=new_dispatch, event=Event.EVENT_CREATED),
202202
)
203203
)
204204

@@ -289,7 +289,7 @@ async def UpdateMicrogridDispatch(
289289
await self._stream_sender.send(
290290
self.StreamEvent(
291291
microgrid_id,
292-
DispatchEvent(dispatch=dispatch, event=Event.UPDATED),
292+
DispatchEvent(dispatch=dispatch, event=Event.EVENT_UPDATED),
293293
)
294294
)
295295

@@ -347,7 +347,7 @@ async def DeleteMicrogridDispatch(
347347
microgrid_id,
348348
DispatchEvent(
349349
dispatch=dispatch_to_delete,
350-
event=Event.DELETED,
350+
event=Event.EVENT_DELETED,
351351
),
352352
)
353353
)

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.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
),

0 commit comments

Comments
 (0)