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