Skip to content

Commit 5b19025

Browse files
authored
refactor: Adapt code to v1alpha8 API of frequenz-client-common (#197)
2 parents 58c00f1 + 9b008f7 commit 5b19025

File tree

10 files changed

+182
-114
lines changed

10 files changed

+182
-114
lines changed

RELEASE_NOTES.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
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.6`
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.category2` will return an `ElectricalComponentCategory`.
19+
* The existing `TargetCategory.category` property is preserved for backward compatibility and returns the corresponding `ComponentCategory`.
1320
* Support secrets for signing and verifying messages.
1421
* Use the new env variable `DISPATCH_API_SIGN_SECRET` to set the secret key.
1522
* Use the new `sign_secret` parameter in the `DispatchClient` constructor to set the secret key.
16-
* Added `auth_key` parameter to the `dispatch-cli` and thew env variable `DISPATCH_API_AUTH_KEY` to set the authentication key for the Dispatch API.
23+
* Added `auth_key` parameter to the `dispatch-cli` and the new variable `DISPATCH_API_AUTH_KEY` to set the authentication key for the Dispatch API.
1724

1825

1926
## Bug Fixes

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.6, < 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: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
"""Dispatch API client for Python."""
55
from __future__ import annotations
66

7+
import warnings
78
from datetime import datetime, timedelta
89
from typing import Any, AsyncIterator, Awaitable, Iterator, Literal, cast
910

10-
# pylint: disable=no-name-in-module
11-
from frequenz.api.common.v1.pagination.pagination_params_pb2 import PaginationParams
11+
# pylint: disable-next=no-name-in-module
12+
from frequenz.api.common.v1alpha8.pagination.pagination_params_pb2 import (
13+
PaginationParams,
14+
)
15+
from frequenz.api.common.v1alpha8.types.interval_pb2 import Interval as PBInterval
1216
from frequenz.api.dispatch.v1 import dispatch_pb2_grpc
1317
from frequenz.api.dispatch.v1.dispatch_pb2 import (
1418
CreateMicrogridDispatchResponse,
@@ -20,11 +24,6 @@
2024
ListMicrogridDispatchesResponse,
2125
StreamMicrogridDispatchesRequest,
2226
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 (
2827
UpdateMicrogridDispatchRequest,
2928
UpdateMicrogridDispatchResponse,
3029
)
@@ -60,7 +59,8 @@ def __init__(
6059
self,
6160
*,
6261
server_url: str,
63-
auth_key: str,
62+
auth_key: str | None = None,
63+
key: str | None = None,
6464
sign_secret: str | None = None,
6565
connect: bool = True,
6666
call_timeout: timedelta = timedelta(seconds=60),
@@ -71,11 +71,27 @@ def __init__(
7171
Args:
7272
server_url: The URL of the server to connect to.
7373
auth_key: API key to use for authentication.
74+
key: Deprecated, use `auth_key` instead.
7475
sign_secret: Optional secret for signing requests.
7576
connect: Whether to connect to the service immediately.
7677
call_timeout: Timeout for gRPC calls, default is 60 seconds.
7778
stream_timeout: Timeout for gRPC streams, default is 5 minutes.
79+
80+
Raises:
81+
TypeError: If neither `auth_key` nor `key` is provided.
7882
"""
83+
if key is not None:
84+
warnings.warn(
85+
"The `key` parameter is deprecated, use `auth_key` instead.",
86+
DeprecationWarning,
87+
stacklevel=2,
88+
)
89+
auth_key = auth_key or key
90+
if auth_key is None:
91+
raise TypeError(
92+
"__init__() missing 1 required keyword-only argument: 'auth_key'"
93+
)
94+
7995
super().__init__(
8096
server_url,
8197
dispatch_pb2_grpc.MicrogridDispatchServiceStub,
@@ -170,11 +186,9 @@ async def list(
170186

171187
def to_interval(
172188
from_: datetime | None, to: datetime | None
173-
) -> PBTimeIntervalFilter | None:
189+
) -> PBInterval | None:
174190
return (
175-
PBTimeIntervalFilter(
176-
from_time=to_timestamp(from_), to_time=to_timestamp(to)
177-
)
191+
PBInterval(start_time=to_timestamp(from_), end_time=to_timestamp(to))
178192
if from_ or to
179193
else None
180194
)

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

Lines changed: 7 additions & 6 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
)
@@ -36,9 +36,10 @@
3636
# pylint: enable=no-name-in-module
3737
from frequenz.client.base.conversion import to_datetime as _to_dt
3838
from frequenz.client.common.microgrid import MicrogridId
39+
from frequenz.client.common.streaming import Event
3940

4041
from .._internal_types import DispatchCreateRequest
41-
from ..types import Dispatch, DispatchEvent, DispatchId, Event
42+
from ..types import Dispatch, DispatchEvent, DispatchId
4243

4344
_logger = logging.getLogger(__name__)
4445

@@ -148,20 +149,20 @@ def _filter_dispatch(
148149
if target != dispatch.target:
149150
return False
150151
if _filter.HasField("start_time_interval"):
151-
if start_from := _filter.start_time_interval.from_time:
152+
if start_from := _filter.start_time_interval.start_time:
152153
if dispatch.start_time < _to_dt(start_from):
153154
return False
154-
if start_to := _filter.start_time_interval.to_time:
155+
if start_to := _filter.start_time_interval.end_time:
155156
if dispatch.start_time >= _to_dt(start_to):
156157
return False
157158
if _filter.HasField("end_time_interval"):
158-
if end_from := _filter.end_time_interval.from_time:
159+
if end_from := _filter.end_time_interval.start_time:
159160
if (
160161
dispatch.duration
161162
and dispatch.start_time + dispatch.duration < _to_dt(end_from)
162163
):
163164
return False
164-
if end_to := _filter.end_time_interval.to_time:
165+
if end_to := _filter.end_time_interval.end_time:
165166
if (
166167
dispatch.duration
167168
and dispatch.start_time + dispatch.duration >= _to_dt(end_to)

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)