From 1a6b3230fe1f35194167ff9d4d608d544d0c1255 Mon Sep 17 00:00:00 2001 From: Hristo Date: Mon, 5 Feb 2024 20:13:23 +0200 Subject: [PATCH] Add system restart button in home assistant --- README.md | 2 ++ rpi-mqtt-monitor.service | 2 +- src/config.py.example | 3 +++ src/rpi-cpu2mqtt.py | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6896387..dc661fb 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and ## What is new +* 2024-02-05: System Restart button added (only works when running as service) +* 2024-01-28: Remote updates via Home Assistant are now available * 2024-01-28: Improved error handling for the MQTT connection * 2024-01-28: Script version is displayed in home assistant device information * 2024-01-28: Update the script by calling it with command line argument --update diff --git a/rpi-mqtt-monitor.service b/rpi-mqtt-monitor.service index 49981f7..59e5ad1 100644 --- a/rpi-mqtt-monitor.service +++ b/rpi-mqtt-monitor.service @@ -11,4 +11,4 @@ Restart=always User=YOUR_USER [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target diff --git a/src/config.py.example b/src/config.py.example index b244199..cbacaa5 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -18,6 +18,9 @@ mqtt_topic_prefix = "rpi-MQTT-monitor" # Only works when group_messages is not used discovery_messages = True +# Enable remote restart button in Home Assistant +restart_button = True + # Binary sensor that displays when there are updates git_update = True diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 294cdb6..64a6c27 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -316,6 +316,10 @@ def config_json(what_config): data["payload_install"] = "install" data['release_url'] = "https://github.com/hjelev/rpi-mqtt-monitor/releases/tag/" + version data['entity_picture'] = "https://masoko.net/rpi-mqtt-monitor.png" + elif what_config == "restart_button": + data["icon"] = "mdi:restart" + data["name"] = "System Restart" + data["command_topic"] = "homeassistant/update/" + hostname + "/command" else: return "" # Return our built discovery config @@ -435,7 +439,11 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_rpi5_fan_speed/config", config_json('rpi5_fan_speed'), qos=config.qos) client.publish(config.mqtt_topic_prefix + "/" + hostname + "/rpi5_fan_speed", rpi5_fan_speed, qos=config.qos, retain=config.retain) - + if config.restart_button: + if config.discovery_messages: + client.publish("homeassistant/button/" + hostname + "/config", + config_json('restart_button'), qos=config.qos) + client.publish(config.mqtt_topic_prefix + "/" + hostname + "/rpi5_fan_speed", rpi5_fan_speed, qos=config.qos, retain=config.retain) # client.publish(config.mqtt_topic_prefix + "/" + hostname + "/git_update", git_update, qos=config.qos, retain=config.retain) # disconnect from mqtt server client.disconnect() @@ -497,6 +505,7 @@ def parse_arguments(): else: print("No update available") exit() + return args @@ -578,6 +587,11 @@ def on_message(client, userdata, msg): thread1.join() # Wait for thread1 to finish thread2.join() # Wait for thread2 to finish sys.exit(0) # Exit the script + elif msg.payload.decode() == "PRESS": + print("Restarting the application...") + # restart the system + print("Restarting the system...") + os.system("sudo reboot") exit_flag = False