fix: handle Paho v2 reason codes

This commit is contained in:
sascha 2026-07-31 09:21:22 +02:00
parent 8d77fc2cb5
commit f558b69119

View file

@ -162,7 +162,9 @@ def enqueue(metric_line):
def on_connect(client, userdata, flags, reason_code, properties=None): def on_connect(client, userdata, flags, reason_code, properties=None):
ok = int(reason_code) == 0 # Paho v2 passes a ReasonCode object; direct comparison is supported,
# converting it with int() is not.
ok = reason_code == 0
with lock: with lock:
state["mqtt_up"] = 1 if ok else 0 state["mqtt_up"] = 1 if ok else 0
if ok: if ok:
@ -337,7 +339,7 @@ class HealthHandler(BaseHTTPRequestHandler):
return return
with lock: with lock:
payload = { payload = {
"status": "ok" if state["influx_up"] else "degraded", "status": "ok" if state["influx_up"] and state["mqtt_up"] else "degraded",
"mqtt_up": bool(state["mqtt_up"]), "influx_up": bool(state["influx_up"]), "mqtt_up": bool(state["mqtt_up"]), "influx_up": bool(state["influx_up"]),
"homeassistant_up": bool(state["ha_up"]), "homeassistant_http_status": state["ha_http_status"], "homeassistant_up": bool(state["ha_up"]), "homeassistant_http_status": state["ha_http_status"],
"topics_seen": len(state["topics"]), "messages_total": state["messages_total"], "topics_seen": len(state["topics"]), "messages_total": state["messages_total"],
@ -345,7 +347,7 @@ class HealthHandler(BaseHTTPRequestHandler):
"uptime_seconds": int(time.time() - state["started"]), "uptime_seconds": int(time.time() - state["started"]),
} }
body = json.dumps(payload, separators=(",", ":")).encode() body = json.dumps(payload, separators=(",", ":")).encode()
self.send_response(200 if payload["influx_up"] else 503) self.send_response(200 if payload["influx_up"] and payload["mqtt_up"] else 503)
self.send_header("Content-Type", "application/json") self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body))) self.send_header("Content-Length", str(len(body)))
self.end_headers() self.end_headers()