Skip to content

Commit

Permalink
Add config topic
Browse files Browse the repository at this point in the history
  • Loading branch information
dchesterton committed May 28, 2021
1 parent f5b655d commit df45393
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
25 changes: 25 additions & 0 deletions src/amcrest2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
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")
Expand Down Expand Up @@ -102,6 +114,10 @@ def signal_handler(sig, frame):
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)

Expand Down Expand Up @@ -129,6 +145,7 @@ def signal_handler(sig, frame):

# 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",
Expand Down Expand Up @@ -263,6 +280,14 @@ def signal_handler(sig, frame):

# 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...")
Expand Down

0 comments on commit df45393

Please sign in to comment.