Skip to content

Commit 9429efa

Browse files
committed
Refactor: Make helper function free & more modular
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 7510149 commit 9429efa

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/frequenz/dispatch/_actor_dispatcher.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from frequenz.channels import Broadcast, Receiver, Sender, select
1414
from frequenz.client.common.microgrid.components import ComponentCategory
1515
from frequenz.client.microgrid import ComponentId
16+
from frequenz.client.dispatch.types import TargetComponents as ClientTargetComponents
1617
from frequenz.sdk.actor import Actor, BackgroundService
1718

1819
from ._dispatch import Dispatch
@@ -242,21 +243,10 @@ def start(self) -> None:
242243
"""Start the background service."""
243244
self._tasks.add(asyncio.create_task(self._run()))
244245

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-
256246
async def _start_actor(self, dispatch: Dispatch) -> None:
257247
"""Start the actor the given dispatch refers to."""
258248
dispatch_update = DispatchInfo(
259-
components=self._get_target_components_from_dispatch(dispatch),
249+
components=_convert_target_components(dispatch.target),
260250
dry_run=dispatch.dry_run,
261251
options=dispatch.payload,
262252
_src=dispatch,
@@ -337,3 +327,13 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None:
337327
await self._start_actor(dispatch)
338328
else:
339329
await self._stop_actor(dispatch, "Dispatch stopped")
330+
331+
332+
def _convert_target_components(target: ClientTargetComponents) -> TargetComponents:
333+
if all(isinstance(comp, int) for comp in target):
334+
# We've confirmed all elements are integers, so we can cast.
335+
int_components = cast(list[int], target)
336+
return [ComponentId(cid) for cid in int_components]
337+
# If not all are ints, then it must be a list of ComponentCategory
338+
# based on the definition of ClientTargetComponents.
339+
return cast(list[ComponentCategory], target)

0 commit comments

Comments
 (0)