Skip to content

Use ComponentId instead of int TargetComponents #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe clarify that this is temporary until ComponentId is moved to api-common.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the release notes accordingly before release


## Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies = [
# Make sure to update the version for cross-referencing also in the
# mkdocs.yml file when changing the version here (look for the config key
# plugins.mkdocstrings.handlers.python.import)
"frequenz-sdk >= 1.0.0-rc1302, < 1.0.0-rc2000",
"frequenz-sdk >= 1.0.0-rc2000, < 1.0.0-rc2100",
"frequenz-channels >= 1.6.1, < 2.0.0",
"frequenz-client-dispatch >= 0.10.1, < 0.11.0",
]
Expand Down
25 changes: 21 additions & 4 deletions src/frequenz/dispatch/_actor_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
from collections.abc import Callable
from dataclasses import dataclass
from datetime import timedelta
from typing import Any, Awaitable
from typing import Any, Awaitable, cast

from frequenz.channels import Broadcast, Receiver, Sender, select
from frequenz.client.dispatch.types import TargetComponents
from frequenz.client.common.microgrid.components import ComponentCategory
from frequenz.client.microgrid import ComponentId
from frequenz.sdk.actor import Actor, BackgroundService

from ._dispatch import Dispatch

_logger = logging.getLogger(__name__)

TargetComponents = list[ComponentId] | list[ComponentCategory]
"""One or more target components specifying which components a dispatch targets.

It can be a list of component IDs or a list of categories.
"""


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

def _get_target_components_from_dispatch(
self, dispatch: Dispatch
) -> TargetComponents:
if all(isinstance(comp, int) for comp in dispatch.target):
# We've confirmed all elements are integers, so we can cast.
int_components = cast(list[int], dispatch.target)
return [ComponentId(cid) for cid in int_components]
# If not all are ints, then it must be a list of ComponentCategory
# based on the definition of ClientTargetComponents.
return cast(list[ComponentCategory], dispatch.target)

async def _start_actor(self, dispatch: Dispatch) -> None:
"""Start the actor the given dispatch refers to."""
dispatch_update = DispatchInfo(
components=dispatch.target,
components=self._get_target_components_from_dispatch(dispatch),
dry_run=dispatch.dry_run,
options=dispatch.payload,
_src=dispatch,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_managing_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from frequenz.client.dispatch.recurrence import Frequency, RecurrenceRule
from frequenz.client.dispatch.test.client import FakeClient
from frequenz.client.dispatch.test.generator import DispatchGenerator
from frequenz.client.microgrid import ComponentId
from frequenz.sdk.actor import Actor
from pytest import fixture

Expand Down Expand Up @@ -152,6 +153,7 @@ async def test_simple_start_stop(
active=True,
dry_run=False,
duration=duration,
target=[1, 10, 15],
start_time=now,
payload={"test": True},
type="UNIT_TEST",
Expand All @@ -169,7 +171,7 @@ async def test_simple_start_stop(

event = test_env.actor(1).initial_dispatch
assert event.options == {"test": True}
assert event.components == dispatch.target
assert event.components == [ComponentId(1), ComponentId(10), ComponentId(15)]
assert event.dry_run is False

assert test_env.actor(1).is_running is True
Expand Down
Loading