@@ -156,6 +156,73 @@ def validate_reccurance(ctx: click.Context, param: click.Parameter, value: Any)
156156 return value
157157
158158
159+ recurrence_options : list [click .Parameter ] = [
160+ click .Option (
161+ ["--frequency" , "-f" ],
162+ type = click .Choice (
163+ [
164+ frequency .name
165+ for frequency in Frequency
166+ if frequency != Frequency .UNSPECIFIED
167+ ],
168+ case_sensitive = False ,
169+ ),
170+ help = "Frequency of the dispatch" ,
171+ callback = validate_reccurance ,
172+ is_eager = True ,
173+ ),
174+ click .Option (
175+ ["--interval" ],
176+ type = int ,
177+ help = "Interval of the dispatch, based on frequency" ,
178+ default = 0 ,
179+ ),
180+ click .Option (
181+ ["--count" ],
182+ type = int ,
183+ help = "Number of occurrences of the dispatch" ,
184+ callback = validate_reccurance ,
185+ ),
186+ click .Option (
187+ ["--until" ],
188+ type = FuzzyDateTime (),
189+ help = "End time of the dispatch" ,
190+ callback = validate_reccurance ,
191+ ),
192+ click .Option (
193+ ["--by-minute" ],
194+ type = int ,
195+ help = "Minute of the hour for the dispatch" ,
196+ multiple = True ,
197+ callback = validate_reccurance ,
198+ ),
199+ click .Option (
200+ ["--by-hour" ],
201+ type = int ,
202+ help = "Hour of the day for the dispatch" ,
203+ multiple = True ,
204+ callback = validate_reccurance ,
205+ ),
206+ click .Option (
207+ ["--by-weekday" ],
208+ type = click .Choice (
209+ [weekday .name for weekday in Weekday if weekday != Weekday .UNSPECIFIED ],
210+ case_sensitive = False ,
211+ ),
212+ help = "Day of the week for the dispatch" ,
213+ multiple = True ,
214+ callback = validate_reccurance ,
215+ ),
216+ click .Option (
217+ ["--by-monthday" ],
218+ type = int ,
219+ help = "Day of the month for the dispatch" ,
220+ multiple = True ,
221+ callback = validate_reccurance ,
222+ ),
223+ ]
224+
225+
159226@cli .command ()
160227@click .argument ("microgrid-id" , required = True , type = int )
161228@click .argument (
@@ -172,70 +239,6 @@ def validate_reccurance(ctx: click.Context, param: click.Parameter, value: Any)
172239 "--payload" , "-p" , type = JsonDictParamType (), help = "JSON payload for the dispatch"
173240)
174241@click .pass_context
175- @click .option (
176- "--frequency" ,
177- "-f" ,
178- type = click .Choice (
179- [
180- frequency .name
181- for frequency in Frequency
182- if frequency != Frequency .UNSPECIFIED
183- ],
184- case_sensitive = False ,
185- ),
186- help = "Frequency of the dispatch" ,
187- callback = validate_reccurance ,
188- is_eager = True ,
189- )
190- @click .option (
191- "--interval" ,
192- type = int ,
193- help = "Interval of the dispatch, based on frequency" ,
194- default = 0 ,
195- )
196- @click .option (
197- "--count" ,
198- type = int ,
199- help = "Number of occurrences of the dispatch" ,
200- callback = validate_reccurance ,
201- )
202- @click .option (
203- "--until" ,
204- type = FuzzyDateTime (),
205- help = "End time of the dispatch" ,
206- callback = validate_reccurance ,
207- )
208- @click .option (
209- "--by-minute" ,
210- type = int ,
211- help = "Minute of the hour for the dispatch" ,
212- multiple = True ,
213- callback = validate_reccurance ,
214- )
215- @click .option (
216- "--by-hour" ,
217- type = int ,
218- help = "Hour of the day for the dispatch" ,
219- multiple = True ,
220- callback = validate_reccurance ,
221- )
222- @click .option (
223- "--by-weekday" ,
224- type = click .Choice (
225- [weekday .name for weekday in Weekday if weekday != Weekday .UNSPECIFIED ],
226- case_sensitive = False ,
227- ),
228- help = "Day of the week for the dispatch" ,
229- multiple = True ,
230- callback = validate_reccurance ,
231- )
232- @click .option (
233- "--by-monthday" ,
234- type = int ,
235- help = "Day of the month for the dispatch" ,
236- multiple = True ,
237- callback = validate_reccurance ,
238- )
239242async def create (
240243 ctx : click .Context ,
241244 / ,
@@ -269,20 +272,36 @@ async def create(
269272@click .option ("--duration" , type = FuzzyTimeDelta ())
270273@click .option ("--selector" , type = SelectorParamType ())
271274@click .option ("--active" , type = bool )
275+ @click .option (
276+ "--payload" , "-p" , type = JsonDictParamType (), help = "JSON payload for the dispatch"
277+ )
272278@click .pass_context
273279async def update (
274280 ctx : click .Context , dispatch_id : int , ** new_fields : dict [str , Any ]
275281) -> None :
276282 """Update a dispatch."""
277- # Remove keys with `None` value from new_fields
278- new_fields = {k : v for k , v in new_fields .items () if v is not None }
283+
284+ def skip_field (value : Any ) -> bool :
285+ return value is None or value == [] or value == ()
286+
287+ # Every field is initialized with `None`, repeatable ones with `()` and `[]`.
288+ # We want to filter these out to not send them to the server.
289+ new_fields = {k : v for k , v in new_fields .items () if not skip_field (v )}
290+ recurrence = parse_recurrence (new_fields )
291+
292+ # Convert recurrence fields to nested fields as expected by update()
293+ for key in recurrence .__dict__ if recurrence else []:
294+ val = getattr (recurrence , key )
295+ if val is not None and val != []:
296+ new_fields [f"recurrence.{ key } " ] = val
279297
280298 if len (new_fields ) == 0 :
281299 raise click .BadArgumentUsage ("At least one field must be given to update." )
282300
283301 try :
284302 await ctx .obj ["client" ].update (dispatch_id = dispatch_id , new_fields = new_fields )
285- click .echo ("Dispatch updated." )
303+ click .echo ("Dispatch updated:" )
304+ click .echo (pformat (await ctx .obj ["client" ].get (dispatch_id ), compact = True ))
286305 except grpc .RpcError as e :
287306 raise click .ClickException (f"Update failed: { e } " )
288307
@@ -378,6 +397,12 @@ async def display_help() -> None:
378397 click .echo (e )
379398
380399
400+ # Add recurrence options to the create command
401+ create .params += recurrence_options # pylint: disable=no-member
402+ # Add recurrence options to the update command
403+ update .params += recurrence_options # pylint: disable=no-member
404+
405+
381406def main () -> None :
382407 """Entrypoint for the CLI."""
383408 if len (sys .argv ) > 1 :
0 commit comments