Add system restart button in home assistant

This commit is contained in:
Hristo
2024-02-05 20:13:23 +02:00
parent 3b1f6f7ab3
commit 1a6b3230fe
4 changed files with 21 additions and 2 deletions

View File

@@ -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

View File

@@ -11,4 +11,4 @@ Restart=always
User=YOUR_USER
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target

View File

@@ -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

View File

@@ -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