Add config topic

pull/21/head 1.0.2
Daniel Chesterton 4 years ago
parent f5b655dbf1
commit df45393453

@ -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" ]

@ -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...")

Loading…
Cancel
Save