|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""CLI tool for gridpool functionality.""" |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +import asyncclick as click |
| 9 | +from frequenz.client.assets import AssetsApiClient |
| 10 | +from frequenz.client.common.microgrid import MicrogridId |
| 11 | + |
| 12 | +from frequenz.gridpool import ComponentGraphGenerator |
| 13 | + |
| 14 | + |
| 15 | +@click.group() |
| 16 | +async def cli() -> None: |
| 17 | + """CLI tool for gridpool functionality.""" |
| 18 | + |
| 19 | + |
| 20 | +@cli.command() |
| 21 | +@click.argument("microgrid_id", type=int) |
| 22 | +@click.option( |
| 23 | + "--prefix", |
| 24 | + type=str, |
| 25 | + default="{component}", |
| 26 | + help="Prefix format for the output (Supports {microgrid_id} and {component} placeholders).", |
| 27 | +) |
| 28 | +async def print_formulas( |
| 29 | + microgrid_id: int, |
| 30 | + prefix: str, |
| 31 | +) -> None: |
| 32 | + """Fetch and print component graph formulas for a microgrid.""" |
| 33 | + url = os.environ.get("ASSETS_API_URL") |
| 34 | + key = os.environ.get("ASSETS_API_AUTH_KEY") |
| 35 | + secret = os.environ.get("ASSETS_API_SIGN_SECRET") |
| 36 | + if not url or not key or not secret: |
| 37 | + raise click.ClickException( |
| 38 | + "ASSETS_API_URL, ASSETS_API_AUTH_KEY, ASSETS_API_SIGN_SECRET must be set." |
| 39 | + ) |
| 40 | + |
| 41 | + async with AssetsApiClient( |
| 42 | + url, |
| 43 | + auth_key=key, |
| 44 | + sign_secret=secret, |
| 45 | + ) as client: |
| 46 | + cgg = ComponentGraphGenerator(client) |
| 47 | + |
| 48 | + graph = await cgg.get_component_graph(MicrogridId(microgrid_id)) |
| 49 | + power_formulas = { |
| 50 | + "consumption": graph.consumer_formula(), |
| 51 | + "generation": graph.producer_formula(), |
| 52 | + "grid": graph.grid_formula(), |
| 53 | + "pv": graph.pv_formula(None), |
| 54 | + "battery": graph.battery_formula(None), |
| 55 | + "chp": graph.chp_formula(None), |
| 56 | + "ev": graph.ev_charger_formula(None), |
| 57 | + } |
| 58 | + |
| 59 | + for component, formula in power_formulas.items(): |
| 60 | + print( |
| 61 | + prefix.format(component=component, microgrid_id=microgrid_id) |
| 62 | + + f' = "{formula}"' |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def main() -> None: |
| 67 | + """Run the CLI tool.""" |
| 68 | + cli(_anyio_backend="asyncio") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments