From 208b5aeb6f63528b9c12157016f233bf98871082 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Sat, 20 Apr 2024 01:05:01 +0200 Subject: [PATCH] Add subcommand `make-dashboard`, for creating a Grafana Dashboard --- CHANGES.md | 1 + ds18b20_datalogger/cli.py | 3 +++ tests/test_cli.py | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 65227e5..d8faea9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ - Define MQTT and sensor configuration separately from implementation. The data logger uses a YAML file now, for example like `etc/mois.yaml`. - Added subcommand `make-config`, for creating a configuration blueprint +- Added subcommand `make-dashboard`, for creating a Grafana Dashboard ## v0.0.2 - 2024-04-15 - Publish as `ds18b20-datalogger` package diff --git a/ds18b20_datalogger/cli.py b/ds18b20_datalogger/cli.py index 5b11304..b1bdb99 100644 --- a/ds18b20_datalogger/cli.py +++ b/ds18b20_datalogger/cli.py @@ -26,5 +26,8 @@ def main(): elif subcommand == "make-config": config_template = files("ds18b20_datalogger") / "datalogger.yaml" print(config_template.read_text(), file=sys.stdout) # noqa: T201 + elif subcommand == "make-dashboard": + dashboard = files("ds18b20_datalogger") / "grafana-dashboard.json" + print(dashboard.read_text(), file=sys.stdout) # noqa: T201 else: raise ValueError(f"Subcommand unknown: {subcommand}") diff --git a/tests/test_cli.py b/tests/test_cli.py index 9eb952c..d005e7b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,4 @@ +import json import subprocess import typing as t @@ -28,3 +29,11 @@ def test_cli_make_config(): config = yaml.safe_load(output) assert "mqtt" in config assert "one-wire" in config + + +def test_cli_make_dashboard(): + exitcode, output = invoke("ds18b20-datalogger make-dashboard") + dashboard = json.loads(output) + assert "annotations" in dashboard + assert "panels" in dashboard + assert "title" in dashboard