77from datetime import datetime , timedelta
88from importlib .resources import files
99from pathlib import Path
10- from typing import Any , AsyncIterator , Awaitable , Iterator , cast
10+ from typing import Any , AsyncIterator , Awaitable , Iterator , Literal , cast
1111
1212# pylint: disable=no-name-in-module
1313from frequenz .api .common .v1 .pagination .pagination_params_pb2 import PaginationParams
4242from ._internal_types import DispatchCreateRequest
4343from .recurrence import RecurrenceRule
4444from .types import (
45- ComponentSelector ,
4645 Dispatch ,
4746 DispatchEvent ,
48- _component_selector_to_protobuf ,
47+ TargetComponents ,
48+ _target_components_to_protobuf ,
4949)
5050
5151# pylint: enable=no-name-in-module
@@ -110,7 +110,7 @@ async def list(
110110 self ,
111111 microgrid_id : int ,
112112 * ,
113- component_selectors : Iterator [ComponentSelector ] = iter (()),
113+ target_components : Iterator [TargetComponents ] = iter (()),
114114 start_from : datetime | None = None ,
115115 start_to : datetime | None = None ,
116116 end_from : datetime | None = None ,
@@ -136,7 +136,7 @@ async def list(
136136
137137 Args:
138138 microgrid_id: The microgrid_id to list dispatches for.
139- component_selectors : optional, list of component ids or categories to filter by.
139+ target_components : optional, list of component ids or categories to filter by.
140140 start_from: optional, filter by start_time >= start_from.
141141 start_to: optional, filter by start_time < start_to.
142142 end_from: optional, filter by end_time >= end_from.
@@ -166,9 +166,9 @@ def to_interval(
166166 # Setup parameters
167167 start_time_interval = to_interval (start_from , start_to )
168168 end_time_interval = to_interval (end_from , end_to )
169- selectors = list (map (_component_selector_to_protobuf , component_selectors ))
169+ targets = list (map (_target_components_to_protobuf , target_components ))
170170 filters = DispatchFilter (
171- selectors = selectors ,
171+ targets = targets ,
172172 start_time_interval = start_time_interval ,
173173 end_time_interval = end_time_interval ,
174174 is_active = active ,
@@ -254,9 +254,9 @@ async def create( # pylint: disable=too-many-positional-arguments
254254 self ,
255255 microgrid_id : int ,
256256 type : str , # pylint: disable=redefined-builtin
257- start_time : datetime ,
257+ start_time : datetime | Literal [ "NOW" ] ,
258258 duration : timedelta | None ,
259- selector : ComponentSelector ,
259+ target : TargetComponents ,
260260 * ,
261261 active : bool = True ,
262262 dry_run : bool = False ,
@@ -268,10 +268,10 @@ async def create( # pylint: disable=too-many-positional-arguments
268268 Args:
269269 microgrid_id: The microgrid_id to create the dispatch for.
270270 type: User defined string to identify the dispatch type.
271- start_time: The start time of the dispatch.
271+ start_time: The start time of the dispatch. Can be "NOW" for immediate start.
272272 duration: The duration of the dispatch. Can be `None` for infinite
273273 or no-duration dispatches (e.g. switching a component on).
274- selector : The component selector for the dispatch.
274+ target : The component target for the dispatch.
275275 active: The active status of the dispatch.
276276 dry_run: The dry_run status of the dispatch.
277277 payload: The payload of the dispatch.
@@ -283,19 +283,23 @@ async def create( # pylint: disable=too-many-positional-arguments
283283 Raises:
284284 ValueError: If start_time is in the past.
285285 """
286- if start_time <= datetime .now (tz = start_time .tzinfo ):
287- raise ValueError ("start_time must not be in the past" )
286+ if isinstance (start_time , datetime ):
287+ if start_time <= datetime .now (tz = start_time .tzinfo ):
288+ raise ValueError ("start_time must not be in the past" )
288289
289- # Raise if it's not UTC
290- if start_time .tzinfo is None or start_time .tzinfo .utcoffset (start_time ) is None :
291- raise ValueError ("start_time must be timezone aware" )
290+ # Raise if it's not UTC
291+ if (
292+ start_time .tzinfo is None
293+ or start_time .tzinfo .utcoffset (start_time ) is None
294+ ):
295+ raise ValueError ("start_time must be timezone aware" )
292296
293297 request = DispatchCreateRequest (
294298 microgrid_id = microgrid_id ,
295299 type = type ,
296300 start_time = start_time ,
297301 duration = duration ,
298- selector = selector ,
302+ target = target ,
299303 active = active ,
300304 dry_run = dry_run ,
301305 payload = payload or {},
@@ -353,8 +357,8 @@ async def update(
353357 msg .update .ClearField ("duration" )
354358 else :
355359 msg .update .duration = round (val .total_seconds ())
356- case "selector " :
357- msg .update .selector .CopyFrom (_component_selector_to_protobuf (val ))
360+ case "target " :
361+ msg .update .target .CopyFrom (_target_components_to_protobuf (val ))
358362 case "is_active" :
359363 msg .update .is_active = val
360364 case "payload" :
0 commit comments