improve code

This commit is contained in:
Hristo
2024-02-02 01:23:46 +02:00
parent 1484853190
commit e231dd3ca7

View File

@@ -332,8 +332,7 @@ def on_connect(client, userdata, flags, rc):
print("Error: Unable to connect to MQTT broker, return code:", rc) print("Error: Unable to connect to MQTT broker, return code:", rc)
def publish_update_status_to_mqtt(git_update): def create_mqtt_client():
client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname) client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname)
client.username_pw_set(config.mqtt_user, config.mqtt_password) client.username_pw_set(config.mqtt_user, config.mqtt_password)
client.on_log = on_log client.on_log = on_log
@@ -342,6 +341,14 @@ def publish_update_status_to_mqtt(git_update):
client.connect(config.mqtt_host, int(config.mqtt_port)) client.connect(config.mqtt_host, int(config.mqtt_port))
except Exception as e: except Exception as e:
print("Error connecting to MQTT broker:", e) print("Error connecting to MQTT broker:", e)
return None
return client
def publish_update_status_to_mqtt(git_update):
client = create_mqtt_client()
if client is None:
return return
client.loop_start() client.loop_start()
@@ -355,19 +362,14 @@ def publish_update_status_to_mqtt(git_update):
if config.discovery_messages: if config.discovery_messages:
client.publish("homeassistant/update/" + hostname + "/config", client.publish("homeassistant/update/" + hostname + "/config",
config_json('update'), qos=config.qos) config_json('update'), qos=config.qos)
client.disconnect()
def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0, def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0): uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0):
# connect to mqtt server # connect to mqtt server
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() client.loop_start()