1212from frequenz .api .dispatch .v1 .dispatch_pb2 import (
1313 CreateMicrogridDispatchRequest as PBDispatchCreateRequest ,
1414)
15-
16- # pylint: enable=no-name-in-module
15+ from frequenz .api .dispatch .v1 .dispatch_pb2 import DispatchData
1716from google .protobuf .json_format import MessageToDict
17+ from google .protobuf .struct_pb2 import Struct
1818
1919from frequenz .client .base .conversion import to_datetime , to_timestamp
2020
2525 component_selector_to_protobuf ,
2626)
2727
28+ # pylint: enable=no-name-in-module
29+
2830
2931# pylint: disable=too-many-instance-attributes
3032@dataclass (kw_only = True )
@@ -42,8 +44,12 @@ class DispatchCreateRequest:
4244 start_time : datetime
4345 """The start time of the dispatch in UTC."""
4446
45- duration : timedelta
46- """The duration of the dispatch, represented as a timedelta."""
47+ duration : timedelta | None
48+ """The duration of the dispatch, represented as a timedelta.
49+
50+ If None, the dispatch is considered to be "infinite" or "instantaneous",
51+ like a command to turn on a component.
52+ """
4753
4854 selector : ComponentSelector
4955 """The component selector specifying which components the dispatch targets."""
@@ -78,13 +84,19 @@ def from_protobuf(
7884 Returns:
7985 The converted dispatch.
8086 """
87+ duration = (
88+ timedelta (seconds = pb_object .dispatch_data .duration )
89+ if pb_object .dispatch_data .HasField ("duration" )
90+ else None
91+ )
92+
8193 return DispatchCreateRequest (
8294 microgrid_id = pb_object .microgrid_id ,
8395 type = pb_object .dispatch_data .type ,
8496 start_time = rounded_start_time (
8597 to_datetime (pb_object .dispatch_data .start_time )
8698 ),
87- duration = timedelta ( seconds = pb_object . dispatch_data . duration ) ,
99+ duration = duration ,
88100 selector = component_selector_from_protobuf (pb_object .dispatch_data .selector ),
89101 active = pb_object .dispatch_data .is_active ,
90102 dry_run = pb_object .dispatch_data .is_dry_run ,
@@ -98,24 +110,24 @@ def to_protobuf(self) -> PBDispatchCreateRequest:
98110 Returns:
99111 The converted protobuf dispatch create request.
100112 """
101- pb_request = PBDispatchCreateRequest ()
102-
103- pb_request .microgrid_id = self .microgrid_id
104- pb_request .dispatch_data .type = self .type
105- pb_request .dispatch_data .start_time .CopyFrom (to_timestamp (self .start_time ))
106- pb_request .dispatch_data .duration = round (self .duration .total_seconds ())
107- pb_request .dispatch_data .selector .CopyFrom (
108- component_selector_to_protobuf (self .selector )
113+ payload = Struct ()
114+ payload .update (self .payload )
115+
116+ return PBDispatchCreateRequest (
117+ microgrid_id = self .microgrid_id ,
118+ dispatch_data = DispatchData (
119+ type = self .type ,
120+ start_time = to_timestamp (self .start_time ),
121+ duration = (
122+ round (self .duration .total_seconds ()) if self .duration else None
123+ ),
124+ selector = component_selector_to_protobuf (self .selector ),
125+ is_active = self .active ,
126+ is_dry_run = self .dry_run ,
127+ payload = payload ,
128+ recurrence = self .recurrence .to_protobuf () if self .recurrence else None ,
129+ ),
109130 )
110- pb_request .dispatch_data .is_active = self .active
111- pb_request .dispatch_data .is_dry_run = self .dry_run
112- pb_request .dispatch_data .payload .update (self .payload )
113- if self .recurrence :
114- pb_request .dispatch_data .recurrence .CopyFrom (self .recurrence .to_protobuf ())
115- else :
116- pb_request .dispatch_data .ClearField ("recurrence" )
117-
118- return pb_request
119131
120132
121133def rounded_start_time (start_time : datetime ) -> datetime :
0 commit comments