Skip to content

Commit f0cd7d2

Browse files
committed
Add gridpool cli tool to print formulas
1 parent 179cb7a commit f0cd7d2

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ classifiers = [
2626
]
2727
requires-python = ">= 3.11, < 4"
2828
dependencies = [
29+
"asyncclick >= 8.3.0.4, < 9",
2930
"typing-extensions >= 4.14.1, < 5",
3031
"frequenz-microgrid-component-graph @ git+https://github.com/shsms/frequenz-microgrid-component-graph-python.git@c4a458e06e541846de0a25142d5b821dd7934f47",
3132
"frequenz-client-assets @ git+https://github.com/shsms/frequenz-client-assets-python@bbf09104df24ddfac497e2a3dd66fe68cfdacd25"
3233
]
3334
dynamic = ["version"]
3435

36+
[project.scripts]
37+
gridpool-cli = "frequenz.gridpool.cli.__main__:main"
38+
3539
[[project.authors]]
3640
name = "Frequenz Energy-as-a-Service GmbH"
3741
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Package for CLI tool for gridpool functionality."""
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.common.microgrid import MicrogridId
10+
11+
from frequenz.gridpool import ComponentGraphGenerator
12+
13+
14+
@click.group()
15+
async def cli() -> None:
16+
"""CLI tool for gridpool functionality."""
17+
18+
19+
@cli.command()
20+
@click.argument("microgrid_id", type=int)
21+
@click.option(
22+
"--prefix",
23+
type=str,
24+
default="{component}",
25+
help="Prefix format for the output (Supports wildcards {microgrid_id} and {component}).",
26+
)
27+
async def print_formulas(
28+
microgrid_id: int,
29+
prefix: str,
30+
) -> None:
31+
"""Fetch and print component graph formulas for a microgrid."""
32+
url = os.environ.get("ASSETS_API_URL")
33+
key = os.environ.get("ASSETS_API_KEY")
34+
secret = os.environ.get("ASSETS_API_SECRET")
35+
if not url or not key or not secret:
36+
raise click.ClickException(
37+
"ASSETS_API_URL, ASSETS_API_KEY, ASSETS_API_SECRET must be set."
38+
)
39+
cgg = ComponentGraphGenerator(url, auth_key=key, sign_secret=secret)
40+
41+
graph = await cgg.get_component_graph(MicrogridId(microgrid_id))
42+
power_formulas = {
43+
"consumption": graph.consumer_formula(),
44+
"generation": graph.producer_formula(),
45+
"grid": graph.grid_formula(),
46+
"pv": graph.pv_formula(None),
47+
"battery": graph.battery_formula(None),
48+
"chp": graph.chp_formula(None),
49+
"ev": graph.ev_charger_formula(None),
50+
}
51+
52+
for component, formula in power_formulas.items():
53+
print(
54+
prefix.format(component=component, microgrid_id=microgrid_id)
55+
+ f' = "{formula}"'
56+
)
57+
58+
59+
def main() -> None:
60+
"""Run the CLI tool."""
61+
cli()
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)