Skip to content

Commit

Permalink
feat: update to python 3.12 and paho mqtt v2 (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostyslav authored Jul 22, 2024
1 parent efaf5a7 commit b0fdd89
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11-alpine
FROM python:3.12-alpine

LABEL org.opencontainers.image.authors="Yaroslav Berezhinskiy <[email protected]>"
LABEL org.opencontainers.image.description="An implementation of a Prometheus exporter for EcoFlow portable power stations"
Expand Down
30 changes: 15 additions & 15 deletions ecoflow_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b0fdd89

Please sign in to comment.