|
13 | 13 | from frequenz.channels import Broadcast, Receiver, Sender, select |
14 | 14 | from frequenz.channels.timer import SkipMissedAndDrift, Timer |
15 | 15 | from frequenz.client.common.microgrid.components import ComponentCategory, ComponentId |
| 16 | +from frequenz.client.dispatch.types import TargetComponents as ClientTargetComponents |
16 | 17 | from frequenz.sdk.actor import Actor, BackgroundService |
17 | 18 |
|
18 | 19 | from ._dispatch import Dispatch |
@@ -190,21 +191,10 @@ def start(self) -> None: |
190 | 191 | """Start the background service.""" |
191 | 192 | self._tasks.add(asyncio.create_task(self._run())) |
192 | 193 |
|
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 | | - |
204 | 194 | async def _start_actor(self, dispatch: Dispatch) -> None: |
205 | 195 | """Start the actor the given dispatch refers to.""" |
206 | 196 | dispatch_update = DispatchInfo( |
207 | | - components=self._get_target_components_from_dispatch(dispatch), |
| 197 | + components=_convert_target_components(dispatch.target), |
208 | 198 | dry_run=dispatch.dry_run, |
209 | 199 | options=dispatch.payload, |
210 | 200 | _src=dispatch, |
@@ -301,3 +291,13 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None: |
301 | 291 | await self._start_actor(dispatch) |
302 | 292 | else: |
303 | 293 | 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