|
12 | 12 |
|
13 | 13 | from frequenz.channels import Broadcast, Receiver, Sender, select |
14 | 14 | from frequenz.client.common.microgrid.components import ComponentCategory |
| 15 | +from frequenz.client.dispatch.types import TargetComponents as ClientTargetComponents |
15 | 16 | from frequenz.client.microgrid import ComponentId |
16 | 17 | from frequenz.sdk.actor import Actor, BackgroundService |
17 | 18 |
|
@@ -242,21 +243,10 @@ def start(self) -> None: |
242 | 243 | """Start the background service.""" |
243 | 244 | self._tasks.add(asyncio.create_task(self._run())) |
244 | 245 |
|
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 | | - |
256 | 246 | async def _start_actor(self, dispatch: Dispatch) -> None: |
257 | 247 | """Start the actor the given dispatch refers to.""" |
258 | 248 | dispatch_update = DispatchInfo( |
259 | | - components=self._get_target_components_from_dispatch(dispatch), |
| 249 | + components=_convert_target_components(dispatch.target), |
260 | 250 | dry_run=dispatch.dry_run, |
261 | 251 | options=dispatch.payload, |
262 | 252 | _src=dispatch, |
@@ -337,3 +327,13 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None: |
337 | 327 | await self._start_actor(dispatch) |
338 | 328 | else: |
339 | 329 | 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