Skip to content
Closed
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
24 changes: 12 additions & 12 deletions src/frequenz/dispatch/_actor_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

Expand Down Expand Up @@ -242,21 +243,10 @@ 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=self._get_target_components_from_dispatch(dispatch),
components=_convert_target_components(dispatch.target),
dry_run=dispatch.dry_run,
options=dispatch.payload,
_src=dispatch,
Expand Down Expand Up @@ -337,3 +327,13 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None:
await self._start_actor(dispatch)
else:
await self._stop_actor(dispatch, "Dispatch stopped")


def _convert_target_components(target: ClientTargetComponents) -> TargetComponents:
if all(isinstance(comp, int) for comp in target):
# We've confirmed all elements are integers, so we can cast.
int_components = cast(list[int], 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], target)
Loading