From 8f43c657cfd490657d43cac6388d7b5469eee387 Mon Sep 17 00:00:00 2001 From: Stephan Fudeus Date: Mon, 4 Nov 2024 23:50:58 +0100 Subject: [PATCH] Support env vars for CLI --- README.md | 3 +++ exporter.py | 29 +++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6399250..1b0c18c 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,9 @@ options: --dump_device_names Do not start exporter, just dump device names ``` +All CLI arguments with uppercase arguments can set those via environment variables as well. + + ## Metrics Metrics are all prefixed with `homematic_`, the remaining name is based on the parameter name within the device descriptor. diff --git a/exporter.py b/exporter.py index fe829f1..eb4992b 100755 --- a/exporter.py +++ b/exporter.py @@ -8,6 +8,7 @@ import json import re import sys +import os from socketserver import ThreadingMixIn from http.server import HTTPServer @@ -373,18 +374,30 @@ def read_mapped_names(self): class _ThreadingSimpleServer(ThreadingMixIn, HTTPServer): """Thread per request HTTP server.""" +class EnvDefault(argparse.Action): + def __init__(self, envvar, required=True, default=None, **kwargs): + if envvar: + if envvar in os.environ: + default = os.environ[envvar] + if required and default: + required = False + super(EnvDefault, self).__init__(default=default, required=required, + **kwargs) + + def __call__(self, parser, namespace, values, option_string=None): + setattr(namespace, self.dest, values) if __name__ == '__main__': PARSER = argparse.ArgumentParser() - PARSER.add_argument("--ccu_host", help="The hostname of the ccu instance", required=True) - PARSER.add_argument("--ccu_port", help="The port for the xmlrpc service (2001 for BidcosRF, 2010 for HmIP)", default=2010) - PARSER.add_argument("--ccu_user", help="The username for the CCU (if authentication is enabled)") - PARSER.add_argument("--ccu_pass", help="The password for the CCU (if authentication is enabled)") - PARSER.add_argument("--interval", help="The interval between two gathering runs in seconds", default=60) - PARSER.add_argument("--namereload", help="After how many intervals the device names are reloaded", default=30) - PARSER.add_argument("--port", help="The port where to expose the exporter", default=8010) - PARSER.add_argument("--config_file", help="A config file with e.g. supported types and device name mappings") + PARSER.add_argument("--ccu_host", action=EnvDefault, envvar="CCU_HOST", help="The hostname of the ccu instance", required=True) + PARSER.add_argument("--ccu_port", action=EnvDefault, envvar="CCU_PORT", help="The port for the xmlrpc service (2001 for BidcosRF, 2010 for HmIP)", default=2010) + PARSER.add_argument("--ccu_user", action=EnvDefault, envvar="CCU_USER", help="The username for the CCU (if authentication is enabled)", required=False) + PARSER.add_argument("--ccu_pass", action=EnvDefault, envvar="CCU_PASS", help="The password for the CCU (if authentication is enabled)", required=False) + PARSER.add_argument("--interval", action=EnvDefault, envvar="INTERVAL", help="The interval between two gathering runs in seconds", default=60) + PARSER.add_argument("--namereload", action=EnvDefault, envvar="NAMERELOAD", help="After how many intervals the device names are reloaded", default=30) + PARSER.add_argument("--port", action=EnvDefault, envvar="PORT", help="The port where to expose the exporter", default=8010) + PARSER.add_argument("--config_file", action=EnvDefault, envvar="CONFIG_FILE", help="A config file with e.g. supported types and device name mappings", required=False) PARSER.add_argument("--debug", action="store_true") PARSER.add_argument("--dump_devices", help="Do not start exporter, just dump device list", action="store_true") PARSER.add_argument("--dump_parameters", help="Do not start exporter, just dump device parameters of given device")