Skip to content

Commit 2c58f8b

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

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.channels.timer import SkipMissedAndDrift, Timer
1515
from frequenz.client.common.microgrid.components import ComponentCategory, 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
@@ -190,21 +191,10 @@ def start(self) -> None:
190191
"""Start the background service."""
191192
self._tasks.add(asyncio.create_task(self._run()))
192193

193-
def _get_target_components_from_dispatch(
194-
self, dispatch: Dispatch
195-
) -> TargetComponents:
196-
if all(isinstance(comp, int) for comp in dispatch.target):
197-
# We've confirmed all elements are integers, so we can cast.
198-
int_components = cast(list[int], dispatch.target)
199-
return [ComponentId(cid) for cid in int_components]
200-
# If not all are ints, then it must be a list of ComponentCategory
201-
# based on the definition of ClientTargetComponents.
202-
return cast(list[ComponentCategory], dispatch.target)
203-
204194
async def _start_actor(self, dispatch: Dispatch) -> None:
205195
"""Start the actor the given dispatch refers to."""
206196
dispatch_update = DispatchInfo(
207-
components=self._get_target_components_from_dispatch(dispatch),
197+
components=_convert_target_components(dispatch.target),
208198
dry_run=dispatch.dry_run,
209199
options=dispatch.payload,
210200
_src=dispatch,
@@ -301,3 +291,13 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None:
301291
await self._start_actor(dispatch)
302292
else:
303293
await self._stop_actor(dispatch, "Dispatch stopped")
294+
295+
296+
def _convert_target_components(target: ClientTargetComponents) -> TargetComponents:
297+
if all(isinstance(comp, int) for comp in target):
298+
# We've confirmed all elements are integers, so we can cast.
299+
int_components = cast(list[int], target)
300+
return [ComponentId(cid) for cid in int_components]
301+
# If not all are ints, then it must be a list of ComponentCategory
302+
# based on the definition of ClientTargetComponents.
303+
return cast(list[ComponentCategory], target)

0 commit comments

Comments
 (0)