From b0fdd89a5cf6a03c415fb8269995db8640527f65 Mon Sep 17 00:00:00 2001 From: Rostyslav Date: Mon, 22 Jul 2024 13:22:52 +0200 Subject: [PATCH] feat: update to python 3.12 and paho mqtt v2 (#65) --- Dockerfile | 2 +- ecoflow_exporter.py | 30 +++++++++++++++--------------- requirements.txt | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index c99a19c..fdd054d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.11-alpine +FROM python:3.12-alpine LABEL org.opencontainers.image.authors="Yaroslav Berezhinskiy " LABEL org.opencontainers.image.description="An implementation of a Prometheus exporter for EcoFlow portable power stations" diff --git a/ecoflow_exporter.py b/ecoflow_exporter.py index aad96b1..9afd2aa 100644 --- a/ecoflow_exporter.py +++ b/ecoflow_exporter.py @@ -121,7 +121,7 @@ def connect(self): self.client.loop_stop() self.client.disconnect() - self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, self.client_id) + self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, self.client_id) self.client.username_pw_set(self.username, self.password) self.client.tls_set(certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED) self.client.tls_insecure_set(False) @@ -153,34 +153,34 @@ def idle_reconnect(self): else: log.error("Reconnection errored out, or timed out, attempted to reconnect...") - def on_connect(self, client, userdata, flags, rc): + def on_connect(self, client, userdata, flags, reason_code, properties): # Initialize the time of last message at least once upon connection so that other things that rely on that to be # set (like idle_reconnect) work self.last_message_time = time.time() - match rc: - case 0: + match reason_code: + case "Success": self.client.subscribe(self.topic) log.info(f"Subscribed to MQTT topic {self.topic}") - case -1: + case "Keep alive timeout": log.error("Failed to connect to MQTT: connection timed out") - case 1: - log.error("Failed to connect to MQTT: incorrect protocol version") - case 2: + case "Unsupported protocol version": + log.error("Failed to connect to MQTT: unsupported protocol version") + case "Client identifier not valid": log.error("Failed to connect to MQTT: invalid client identifier") - case 3: + case "Server unavailable": log.error("Failed to connect to MQTT: server unavailable") - case 4: + case "Bad user name or password": log.error("Failed to connect to MQTT: bad username or password") - case 5: + case "Not authorized": log.error("Failed to connect to MQTT: not authorised") case _: - log.error(f"Failed to connect to MQTT: another error occured: {rc}") + log.error(f"Failed to connect to MQTT: another error occured: {reason_code}") return client - def on_disconnect(self, client, userdata, rc): - if rc != 0: - log.error(f"Unexpected MQTT disconnection: {rc}. Will auto-reconnect") + def on_disconnect(self, client, userdata, flags, reason_code, properties): + if reason_code > 0: + log.error(f"Unexpected MQTT disconnection: {reason_code}. Will auto-reconnect") time.sleep(5) def on_message(self, client, userdata, message): diff --git a/requirements.txt b/requirements.txt index d72d9ab..232b378 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -prometheus-client>=0.15.0 -paho-mqtt>=2.0 -requests>=2.28.1 +prometheus-client>=0.20.0 +paho-mqtt>=2.1.0 +requests>=2.32.3