Skip to content

Commit c8973fc

Browse files
authored
Use ComponentId instead of int TargetComponents (#156)
2 parents ce80d90 + dfec693 commit c8973fc

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
* This release now supports the sdk up to rc2000.
1414
* Less logs are now on `INFO` level, and more on `DEBUG` level, making the output less verbose.
15+
* Changed the type of `DispatchInfo.components` from `list[int] | list[ComponentCategory]` to `list[ComponentId] | list[ComponentCategory]`, where `ComponentId` is imported from `frequenz.client.microgrid`.
1516

1617
## Bug Fixes
1718

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies = [
3838
# Make sure to update the version for cross-referencing also in the
3939
# mkdocs.yml file when changing the version here (look for the config key
4040
# plugins.mkdocstrings.handlers.python.import)
41-
"frequenz-sdk >= 1.0.0-rc1302, < 1.0.0-rc2000",
41+
"frequenz-sdk >= 1.0.0-rc2000, < 1.0.0-rc2100",
4242
"frequenz-channels >= 1.6.1, < 2.0.0",
4343
"frequenz-client-dispatch >= 0.10.1, < 0.11.0",
4444
]

src/frequenz/dispatch/_actor_dispatcher.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@
88
from collections.abc import Callable
99
from dataclasses import dataclass
1010
from datetime import timedelta
11-
from typing import Any, Awaitable
11+
from typing import Any, Awaitable, cast
1212

1313
from frequenz.channels import Broadcast, Receiver, Sender, select
14-
from frequenz.client.dispatch.types import TargetComponents
14+
from frequenz.client.common.microgrid.components import ComponentCategory
15+
from frequenz.client.microgrid import ComponentId
1516
from frequenz.sdk.actor import Actor, BackgroundService
1617

1718
from ._dispatch import Dispatch
1819

1920
_logger = logging.getLogger(__name__)
2021

22+
TargetComponents = list[ComponentId] | list[ComponentCategory]
23+
"""One or more target components specifying which components a dispatch targets.
24+
25+
It can be a list of component IDs or a list of categories.
26+
"""
27+
2128

2229
@dataclass(frozen=True, kw_only=True)
2330
class DispatchInfo:
@@ -46,7 +53,6 @@ class ActorDispatcher(BackgroundService):
4653
import asyncio
4754
from typing import override
4855
from frequenz.dispatch import Dispatcher, ActorDispatcher, DispatchInfo
49-
from frequenz.client.dispatch.types import TargetComponents
5056
from frequenz.client.common.microgrid.components import ComponentCategory
5157
from frequenz.channels import Receiver, Broadcast, select, selected_from
5258
from frequenz.sdk.actor import Actor, run
@@ -236,10 +242,21 @@ def start(self) -> None:
236242
"""Start the background service."""
237243
self._tasks.add(asyncio.create_task(self._run()))
238244

245+
def _get_target_components_from_dispatch(
246+
self, dispatch: Dispatch
247+
) -> TargetComponents:
248+
if all(isinstance(comp, int) for comp in dispatch.target):
249+
# We've confirmed all elements are integers, so we can cast.
250+
int_components = cast(list[int], dispatch.target)
251+
return [ComponentId(cid) for cid in int_components]
252+
# If not all are ints, then it must be a list of ComponentCategory
253+
# based on the definition of ClientTargetComponents.
254+
return cast(list[ComponentCategory], dispatch.target)
255+
239256
async def _start_actor(self, dispatch: Dispatch) -> None:
240257
"""Start the actor the given dispatch refers to."""
241258
dispatch_update = DispatchInfo(
242-
components=dispatch.target,
259+
components=self._get_target_components_from_dispatch(dispatch),
243260
dry_run=dispatch.dry_run,
244261
options=dispatch.payload,
245262
_src=dispatch,

tests/test_managing_actor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from frequenz.client.dispatch.recurrence import Frequency, RecurrenceRule
1919
from frequenz.client.dispatch.test.client import FakeClient
2020
from frequenz.client.dispatch.test.generator import DispatchGenerator
21+
from frequenz.client.microgrid import ComponentId
2122
from frequenz.sdk.actor import Actor
2223
from pytest import fixture
2324

@@ -152,6 +153,7 @@ async def test_simple_start_stop(
152153
active=True,
153154
dry_run=False,
154155
duration=duration,
156+
target=[1, 10, 15],
155157
start_time=now,
156158
payload={"test": True},
157159
type="UNIT_TEST",
@@ -169,7 +171,7 @@ async def test_simple_start_stop(
169171

170172
event = test_env.actor(1).initial_dispatch
171173
assert event.options == {"test": True}
172-
assert event.components == dispatch.target
174+
assert event.components == [ComponentId(1), ComponentId(10), ComponentId(15)]
173175
assert event.dry_run is False
174176

175177
assert test_env.actor(1).is_running is True

0 commit comments

Comments
 (0)