From 51fa23d444da49c2c2bdac22660d3f1a343836a9 Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Thu, 12 Jun 2025 20:01:01 +0200 Subject: [PATCH] Refactor: Make helper function free & more modular Signed-off-by: Mathias L. Baumann --- src/frequenz/dispatch/_actor_dispatcher.py | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/frequenz/dispatch/_actor_dispatcher.py b/src/frequenz/dispatch/_actor_dispatcher.py index cc0deb9..b4bb31f 100644 --- a/src/frequenz/dispatch/_actor_dispatcher.py +++ b/src/frequenz/dispatch/_actor_dispatcher.py @@ -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 @@ -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, @@ -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)