|
40 | 40 | from frequenz.client.assets._client import AssetsApiClient |
41 | 41 | from frequenz.client.assets.exceptions import ApiClientError |
42 | 42 |
|
43 | | -from ._utils import print_electrical_components, print_microgrid_details |
| 43 | +from ._utils import ( |
| 44 | + print_component_connections, |
| 45 | + print_electrical_components, |
| 46 | + print_microgrid_details, |
| 47 | +) |
44 | 48 |
|
45 | 49 |
|
46 | 50 | @click.group(invoke_without_command=True) |
@@ -201,6 +205,91 @@ async def list_microgrid_electrical_components( |
201 | 205 | raise click.Abort() |
202 | 206 |
|
203 | 207 |
|
| 208 | +@cli.command("component-connections") |
| 209 | +@click.pass_context |
| 210 | +@click.argument("microgrid-id", required=True, type=int) |
| 211 | +@click.option( |
| 212 | + "--source", |
| 213 | + "source_component_ids", |
| 214 | + help="Filter connections by source component ID(s). Can be specified multiple times.", |
| 215 | + type=int, |
| 216 | + multiple=True, |
| 217 | + required=False, |
| 218 | +) |
| 219 | +@click.option( |
| 220 | + "--destination", |
| 221 | + "destination_component_ids", |
| 222 | + help="Filter connections by destination component ID(s). Can be specified multiple times.", |
| 223 | + type=int, |
| 224 | + multiple=True, |
| 225 | + required=False, |
| 226 | +) |
| 227 | +async def list_microgrid_electrical_component_connections( |
| 228 | + ctx: click.Context, |
| 229 | + microgrid_id: int, |
| 230 | + source_component_ids: tuple[int, ...], |
| 231 | + destination_component_ids: tuple[int, ...], |
| 232 | +) -> None: |
| 233 | + """ |
| 234 | + Get and display electrical component connections by microgrid ID. |
| 235 | +
|
| 236 | + This command fetches detailed information about all electrical component connections |
| 237 | + in a specific microgrid from the Assets API and displays it in JSON format. |
| 238 | + The output can be piped to other tools for further processing. |
| 239 | +
|
| 240 | + Args: |
| 241 | + ctx: Click context object containing the initialized API client. |
| 242 | + microgrid_id: The unique identifier of the microgrid to retrieve. |
| 243 | + source_component_ids: Optional filter for connections from specific |
| 244 | + source component IDs. |
| 245 | + destination_component_ids: Optional filter for connections to specific |
| 246 | + destination component IDs. |
| 247 | +
|
| 248 | + Raises: |
| 249 | + click.Abort: If there is an error printing the electrical component connections. |
| 250 | +
|
| 251 | + Example: |
| 252 | + ```bash |
| 253 | + # Get all connections for microgrid with ID 123 |
| 254 | + assets-cli component-connections 123 |
| 255 | +
|
| 256 | + # Filter by source component |
| 257 | + assets-cli component-connections 123 --source 5 |
| 258 | +
|
| 259 | + # Filter by destination component |
| 260 | + assets-cli component-connections 123 --destination 10 |
| 261 | +
|
| 262 | + # Filter by both source and destination |
| 263 | + assets-cli component-connections 123 --source 5 --destination 10 |
| 264 | +
|
| 265 | + # Filter by multiple source components |
| 266 | + assets-cli component-connections 123 --source 5 --source 6 --source 7 |
| 267 | +
|
| 268 | + # Pipe output to jq for filtering |
| 269 | + assets-cli component-connections 123 | jq ".[]" |
| 270 | + ``` |
| 271 | + """ |
| 272 | + try: |
| 273 | + client = ctx.obj["client"] |
| 274 | + component_connections = ( |
| 275 | + await client.list_microgrid_electrical_component_connections( |
| 276 | + microgrid_id, |
| 277 | + source_component_ids=source_component_ids, |
| 278 | + destination_component_ids=destination_component_ids, |
| 279 | + ) |
| 280 | + ) |
| 281 | + print_component_connections(component_connections) |
| 282 | + except ApiClientError as e: |
| 283 | + error_dict = { |
| 284 | + "error_type": type(e).__name__, |
| 285 | + "server_url": e.server_url, |
| 286 | + "operation": e.operation, |
| 287 | + "description": e.description, |
| 288 | + } |
| 289 | + click.echo(json.dumps(error_dict, indent=2)) |
| 290 | + raise click.Abort() |
| 291 | + |
| 292 | + |
204 | 293 | def main() -> None: |
205 | 294 | """ |
206 | 295 | Initialize and run the CLI application. |
|
0 commit comments