Skip to content

Commit ad830ef

Browse files
Change type of TargetComponents to return ComponentId
* `frequenz.client.dispatch.TargetComponents` is now public * its type has changed from `list[int] | list[ComponentCategory]` to `list[ComponentId] | list[ComponentCategory]`. * This change introduces a new dependency on `frequenz-client-microgrid` (>= v0.7.0, < 0.8.0) for the `frequenz.client.microgrid.ComponentId` type. Signed-off-by: Elzbieta Kotulska <[email protected]>
1 parent d3fbfe1 commit ad830ef

File tree

6 files changed

+30
-29
lines changed

6 files changed

+30
-29
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22

33
## Summary
44

5-
<!-- Here goes a general summary of what this release is about -->
6-
7-
## Upgrading
8-
9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
10-
115
## New Features
126

137
* `dispatch-cli` supports now the parameter `--type` and `--running` to filter the list of running services by type and status, respectively.
148
* Every call now has a default timeout of 60 seconds, streams terminate after five minutes. This can be influenced by the two new parameters for`DispatchApiClient.__init__()`:
159
* `default_timeout: timedelta` (default: 60 seconds)
1610
* `stream_timeout: timedelta` (default: 5 minutes)
1711

18-
## Bug Fixes
19-
20-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
12+
* `frequenz.client.dispatch.TargetComponents` is now public, and its type has changed from `list[int] | list[ComponentCategory]` to `list[ComponentId] | list[ComponentCategory]`. This change introduces a new dependency on `frequenz-client-microgrid` (>= v0.7.0, < 0.8.0) for the `frequenz.client.microgrid.ComponentId` type.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies = [
4040
"frequenz-api-dispatch == 1.0.0-rc1",
4141
"frequenz-client-base >= 0.8.0, < 0.10.0",
4242
"frequenz-client-common >= 0.1.0, < 0.4.0",
43+
"frequenz-client-microgrid >= 0.7.0, < 0.8.0",
4344
"grpcio >= 1.66.1, < 2",
4445
"python-dateutil >= 2.8.2, < 3.0",
4546
]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime, timedelta, timezone
88

99
from frequenz.client.common.microgrid.components import ComponentCategory
10+
from frequenz.client.microgrid import ComponentId
1011

1112
from .._internal_types import rounded_start_time
1213
from ..recurrence import EndCriteria, Frequency, RecurrenceRule, Weekday
@@ -99,7 +100,7 @@ def generate_dispatch(self) -> Dispatch:
99100
for _ in range(self._rng.randint(1, 10))
100101
],
101102
[
102-
self._rng.randint(1, 100)
103+
ComponentId(self._rng.randint(1, 100))
103104
for _ in range(self._rng.randint(1, 10))
104105
],
105106
]

src/frequenz/client/dispatch/types.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424

2525
# pylint: enable=no-name-in-module
2626
from frequenz.client.common.microgrid.components import ComponentCategory
27+
from frequenz.client.microgrid import ComponentId
2728

2829
from .recurrence import Frequency, RecurrenceRule, Weekday
2930

30-
TargetComponents = list[int] | list[ComponentCategory]
31+
TargetComponents = list[ComponentId] | list[ComponentCategory]
3132
"""One or more target components specifying which components a dispatch targets.
3233
3334
It can be a list of component IDs or a list of categories.
@@ -50,7 +51,9 @@ def _target_components_from_protobuf(
5051
"""
5152
match pb_target.WhichOneof("components"):
5253
case "component_ids":
53-
id_list: list[int] = list(pb_target.component_ids.ids)
54+
id_list: list[ComponentId] = list(
55+
map(ComponentId, pb_target.component_ids.ids)
56+
)
5457
return id_list
5558
case "component_categories":
5659
category_list: list[ComponentCategory] = list(
@@ -80,8 +83,10 @@ def _target_components_to_protobuf(
8083
"""
8184
pb_target = PBTargetComponents()
8285
match target:
83-
case list(component_ids) if all(isinstance(id, int) for id in component_ids):
84-
pb_target.component_ids.ids.extend(cast(list[int], component_ids))
86+
case list(component_ids) if all(
87+
isinstance(id, ComponentId) for id in component_ids
88+
):
89+
pb_target.component_ids.ids.extend(map(lambda x: int(x), component_ids))
8590
case list(categories) if all(
8691
isinstance(cat, ComponentCategory) for cat in categories
8792
):

tests/test_cli.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from tzlocal import get_localzone
1414

1515
from frequenz.client.common.microgrid.components import ComponentCategory
16+
from frequenz.client.microgrid import ComponentId
1617
from frequenz.client.dispatch.__main__ import cli
1718
from frequenz.client.dispatch.recurrence import (
1819
EndCriteria,
@@ -68,7 +69,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
6869
type="test",
6970
start_time=datetime(2023, 1, 1, 0, 0, 0),
7071
duration=timedelta(seconds=3600),
71-
target=[1, 2, 3],
72+
target=list(map(ComponentId, [1, 2, 3])),
7273
active=True,
7374
dry_run=False,
7475
payload={},
@@ -91,7 +92,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
9192
type="test",
9293
start_time=datetime(2023, 1, 1, 0, 0, 0),
9394
duration=timedelta(seconds=3600),
94-
target=[1, 2, 3],
95+
target=list(map(ComponentId, [1, 2, 3])),
9596
active=True,
9697
dry_run=False,
9798
payload={},
@@ -113,7 +114,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
113114
type="test",
114115
start_time=datetime(2023, 1, 1, 0, 0, 0),
115116
duration=timedelta(seconds=3600),
116-
target=[1, 2, 3],
117+
target=list(map(ComponentId, [1, 2, 3])),
117118
active=True,
118119
dry_run=False,
119120
payload={},
@@ -128,7 +129,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
128129
type="test",
129130
start_time=datetime(2023, 1, 1, 0, 0, 0),
130131
duration=timedelta(seconds=3600),
131-
target=[1, 2, 3],
132+
target=list(map(ComponentId, [1, 2, 3])),
132133
active=True,
133134
dry_run=False,
134135
payload={},
@@ -156,7 +157,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
156157
type="test",
157158
start_time=datetime(2023, 1, 1, 0, 0, 0),
158159
duration=timedelta(seconds=3600),
159-
target=[1, 2, 3],
160+
target=list(map(ComponentId, [1, 2, 3])),
160161
active=True,
161162
dry_run=False,
162163
payload={},
@@ -169,7 +170,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
169170
type="filtered",
170171
start_time=datetime(2023, 1, 1, 0, 0, 0),
171172
duration=timedelta(seconds=1800),
172-
target=[3],
173+
target=list(map(ComponentId, [3])),
173174
active=True,
174175
dry_run=False,
175176
payload={},
@@ -376,7 +377,7 @@ async def test_create_command(
376377
expected_type: str,
377378
expected_start_time_delta: timedelta | Literal["NOW"],
378379
expected_duration: timedelta,
379-
expected_target: list[int] | list[ComponentCategory],
380+
expected_target: list[ComponentId] | list[ComponentCategory],
380381
expected_options: dict[str, Any],
381382
expected_reccurence: RecurrenceRule | None,
382383
expected_return_code: int,
@@ -528,7 +529,7 @@ async def test_create_command(
528529
type="test",
529530
start_time=datetime(2023, 1, 1, 0, 0, 0),
530531
duration=timedelta(seconds=3600),
531-
target=[500, 501],
532+
target=list(map(ComponentId, [500, 501])),
532533
active=True,
533534
dry_run=False,
534535
payload={},
@@ -633,7 +634,7 @@ async def test_update_command(
633634
type="test",
634635
start_time=datetime(2023, 1, 1, 0, 0, 0),
635636
duration=timedelta(seconds=3600),
636-
target=[1, 2, 3],
637+
target=list(map(ComponentId, [1, 2, 3])),
637638
active=True,
638639
dry_run=False,
639640
payload={},
@@ -680,7 +681,7 @@ async def test_get_command(
680681
type="test",
681682
start_time=datetime(2023, 1, 1, 0, 0, 0),
682683
duration=timedelta(seconds=3600),
683-
target=[1, 2, 3],
684+
target=list(map(ComponentId, [1, 2, 3])),
684685
active=True,
685686
dry_run=False,
686687
payload={},

tests/test_proto.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
_target_components_from_protobuf,
1919
_target_components_to_protobuf,
2020
)
21+
from frequenz.client.microgrid import ComponentId
2122

2223

2324
def test_target_components() -> None:
2425
"""Test the target components."""
2526
for components in (
26-
[1, 2, 3],
27-
[10, 20, 30],
27+
list(map(ComponentId, [1, 2, 3])),
28+
list(map(ComponentId, [10, 20, 30])),
2829
[ComponentCategory.BATTERY],
2930
[ComponentCategory.GRID],
3031
[ComponentCategory.METER],
@@ -99,7 +100,7 @@ def test_dispatch() -> None:
99100
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
100101
end_time=datetime(2024, 10, 20, tzinfo=timezone.utc),
101102
duration=timedelta(days=10),
102-
target=[1, 2, 3],
103+
target=list(map(ComponentId, [1, 2, 3])),
103104
active=True,
104105
dry_run=False,
105106
payload={"key": "value"},
@@ -163,7 +164,7 @@ def test_dispatch_create_request_with_no_recurrence() -> None:
163164
type="test",
164165
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
165166
duration=timedelta(days=10),
166-
target=[1, 2, 3],
167+
target=list(map(ComponentId, [1, 2, 3])),
167168
active=True,
168169
dry_run=False,
169170
payload={"key": "value"},
@@ -180,7 +181,7 @@ def test_dispatch_create_start_immediately() -> None:
180181
type="test",
181182
start_time="NOW",
182183
duration=timedelta(days=10),
183-
target=[1, 2, 3],
184+
target=list(map(ComponentId, [1, 2, 3])),
184185
active=True,
185186
dry_run=False,
186187
payload={"key": "value"},

0 commit comments

Comments
 (0)