* edit paho client to support new versions of the modue - hope this is also good for old versions - need to test it

* set paho-mqtt to version 1.6.1
This commit is contained in:
Masoko
2024-02-11 10:35:30 +02:00
committed by GitHub
parent 6f086b7300
commit 8664d02e78
3 changed files with 11 additions and 20 deletions

View File

@@ -1 +1 @@
paho-mqtt paho-mqtt==1.6.1

View File

@@ -14,4 +14,3 @@ User=YOUR_USER
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@@ -459,16 +459,11 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl
values = cpu_load, cpu_temp, used_space, voltage, int(sys_clock_speed), swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update values = cpu_load, cpu_temp, used_space, voltage, int(sys_clock_speed), swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update
values = str(values)[1:-1] values = str(values)[1:-1]
client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname) client = create_mqtt_client()
client.username_pw_set(config.mqtt_user, config.mqtt_password) if client is None:
client.on_log = on_log
client.on_connect = on_connect
try:
client.connect(config.mqtt_host, int(config.mqtt_port))
except Exception as e:
print("Error connecting to MQTT broker:", e)
return return
client.loop_start()
# publish monitored values to MQTT # publish monitored values to MQTT
client.publish(config.mqtt_topic_prefix + "/" + hostname, values, qos=config.qos, retain=config.retain) client.publish(config.mqtt_topic_prefix + "/" + hostname, values, qos=config.qos, retain=config.retain)
@@ -606,15 +601,12 @@ if __name__ == '__main__':
args = parse_arguments(); args = parse_arguments();
if args.service: if args.service:
client = paho.Client() client = create_mqtt_client()
client.username_pw_set(config.mqtt_user, config.mqtt_password) if client is None:
client.on_message = on_message print("Error: Unable to connect to MQTT broker")
sys.exit(1)
try:
client.connect(config.mqtt_host, int(config.mqtt_port)) client.loop_start()
except Exception as e:
print("Error connecting to MQTT broker:", e)
sys.exit(1) # Exit the script
client.subscribe("homeassistant/update/" + hostname + "/command") # Replace with your MQTT topic client.subscribe("homeassistant/update/" + hostname + "/command") # Replace with your MQTT topic
print("Listening to topic : " + "homeassistant/update/" + hostname + "/command") print("Listening to topic : " + "homeassistant/update/" + hostname + "/command")