From df453934531d637032c809c117aa537e00901815 Mon Sep 17 00:00:00 2001 From: Daniel Chesterton Date: Fri, 28 May 2021 11:19:55 +0100 Subject: [PATCH] Add config topic --- Dockerfile | 1 + src/amcrest2mqtt.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Dockerfile b/Dockerfile index 2ed72f0..3d5b5c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ RUN pip install --prefix=/install -r /requirements.txt FROM base COPY --from=builder /install /usr/local COPY src /app +COPY VERSION /app WORKDIR /app CMD [ "python", "-u", "/app/amcrest2mqtt.py" ] diff --git a/src/amcrest2mqtt.py b/src/amcrest2mqtt.py index 3fc2c7b..d21ec1d 100644 --- a/src/amcrest2mqtt.py +++ b/src/amcrest2mqtt.py @@ -27,6 +27,18 @@ mqtt_password = os.getenv("MQTT_PASSWORD") # can be None home_assistant = os.getenv("HOME_ASSISTANT") == "true" home_assistant_prefix = os.getenv("HOME_ASSISTANT_PREFIX") or "homeassistant" +def read_file(file_name): + with open(file_name, 'r') as file: + data = file.read().replace('\n', '') + + return data + +def read_version(): + if os.path.isfile("./VERSION"): + return read_file("./VERSION") + + return read_file("../VERSION") + # Helper functions and callbacks def log(msg, level="INFO"): ts = datetime.now(timezone.utc).strftime("%d/%m/%Y %H:%M:%S") @@ -102,6 +114,10 @@ if mqtt_username is None: log("Please set the MQTT_USERNAME environment variable", level="ERROR") sys.exit(1) +version = read_version() + +log(f"App Version: {version}") + # Handle interruptions signal.signal(signal.SIGINT, signal_handler) @@ -129,6 +145,7 @@ log(f"Device name: {device_name}") # MQTT topics topics = { + "config": f"amcrest2mqtt/{serial_number}/config", "status": f"amcrest2mqtt/{serial_number}/status", "event": f"amcrest2mqtt/{serial_number}/event", "motion": f"amcrest2mqtt/{serial_number}/motion", @@ -263,6 +280,14 @@ if home_assistant: # Main loop mqtt_publish(topics["status"], "online") +mqtt_publish(topics["config"], { + "version": version, + "device_type": device_type, + "device_name": device_name, + "sw_version": sw_version, + "serial_number": serial_number, +}, json=True) + refresh_storage_sensors() log("Listening for events...")