Skip to content

Commit 22279f3

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

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)