Skip to content

Commit

Permalink
Add subcommand make-config, for creating a configuration blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Apr 19, 2024
1 parent 61b62a5 commit d835ada
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.idea
.venv*
.coverage
.coverage*
coverage.xml
*.egg-info
*.pyc
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Remove support for Python 3.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

## v0.0.2 - 2024-04-15
- Publish as `ds18b20-datalogger` package
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include *.md
recursive-include docs *.md
recursive-include ds18b20_datalogger *.py *.json
recursive-include ds18b20_datalogger *.py *.json *.yaml
prune tests
8 changes: 8 additions & 0 deletions ds18b20_datalogger/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from ds18b20_datalogger.core import read_ds18b20_sensor_matrix, send_measurement_mqtt
from ds18b20_datalogger.model import Settings

if sys.version_info < (3, 9):
from importlib_resources import files
else:
from importlib.resources import files


def main():
if not sys.argv[1:]:
Expand All @@ -18,5 +23,8 @@ def main():
settings = Settings.from_file(configfile)
reading = read_ds18b20_sensor_matrix(settings.devicemap)
send_measurement_mqtt(settings.mqtt, reading)
elif subcommand == "make-config":
config_template = files("ds18b20_datalogger") / "datalogger.yaml"
print(config_template.read_text(), file=sys.stdout) # noqa: T201
else:
raise ValueError(f"Subcommand unknown: {subcommand}")
31 changes: 31 additions & 0 deletions ds18b20_datalogger/datalogger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Configuration file for ds18b20-datalogger
# https://github.com/hiveeyes/ds18b20-datalogger

mqtt:

# MQTT broker connection.
client_id: "ds18b20-datalogger"
host: "mqtt.example.org"
port: 1883
username: "username"
password: "password"

# MQTT topic in SensorWAN 3.0 format.
# https://hiveeyes.org/docs/arduino/SensorWAN.html
#
# Format: {realm}/{network}/{group}/{name}/data.json
topic: "organization/beekeeper/apiary/hive/data.json"

one-wire:
- name: temp-ir-1-1
path: /sys/bus/w1/devices/28-0346d4430b06
- name: temp-ir-1-2
path: /sys/bus/w1/devices/28-0cf3d443ba40
- name: temp-ir-1-3
path: /sys/bus/w1/devices/28-0e49d44343bd
- name: temp-ir-2-1
path: /sys/bus/w1/devices/28-2231d443d266
- name: temp-ir-2-2
path: /sys/bus/w1/devices/28-282bd4430f5e
- name: temp-ir-2-3
path: /sys/bus/w1/devices/28-2846d443e4f2
15 changes: 10 additions & 5 deletions etc/mois.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Configuration file for ds18b20-datalogger
# https://github.com/hiveeyes/ds18b20-datalogger

mqtt:

# MQTT broker connection.
Expand All @@ -8,11 +11,13 @@ mqtt:
password: "password"

# MQTT topic.
# {realm}/{network}/{gateway}/{node}/data.json
# realm: Beekeeper collective
# network: Beekeeper-ID
# gateway: Hive location
# node: Hive name
# Structure: {realm}/{network}/{gateway}/{node}/data.json
#
# Details:
# :realm: Beekeeper collective / group
# :network: Beekeeper identifier
# :gateway: Hive location
# :node: Hive name
topic: "hiveeyes/testdrive/area-42/array-one/data.json"

one-wire:
Expand Down
18 changes: 14 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import subprocess
import typing as t

import yaml


def invoke(command: str) -> t.Tuple[int, str]:
return subprocess.getstatusoutput(command) # noqa: S603, S605


def test_cli_run():
invoke("ds18b20-datalogger run")


def test_cli_no_subcommand():
exitcode, output = invoke("ds18b20-datalogger")
assert "Program needs a subcommand" in output
Expand All @@ -18,3 +16,15 @@ def test_cli_no_subcommand():
def test_cli_unknown_subcommand():
exitcode, output = invoke("ds18b20-datalogger foo")
assert "Subcommand unknown: foo" in output


def test_cli_run_no_config():
exitcode, output = invoke("ds18b20-datalogger run")
assert "Program needs a configuration file" in output


def test_cli_make_config():
exitcode, output = invoke("ds18b20-datalogger make-config")
config = yaml.safe_load(output)
assert "mqtt" in config
assert "one-wire" in config

0 comments on commit d835ada

Please sign in to comment.