Skip to content

Commit

Permalink
add monitor control support (#110)
Browse files Browse the repository at this point in the history
* add monitor control support

* removed useless code
  • Loading branch information
hjelev authored May 7, 2024
1 parent 6a1aff7 commit 0d1ae21
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ print_yellow(){
}

check_and_install_pip(){

pip_ver=$(${python} -m pip --version 2>&1);
if [[ "$pip_ver" == *"No"* ]]; then
echo "- Pip is not installed, installing it."
Expand Down Expand Up @@ -100,6 +99,8 @@ update_config(){
esac
fi

user=$(whoami)
sed -i "s/os_user_to_be_replaced/${user}/" src/config.py

print_green "+ Copy config.py.example to config.py"
cp src/config.py.example src/config.py
Expand Down
6 changes: 6 additions & 0 deletions src/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ restart_button = True
# Enable remote shutdown button in Home Assistant
shutdown_button = True

# Enable control of attached display(s) via Home Assistant
display_control = True

# user for which display_control is enabled
os_user = 'os_user_to_be_replaced'

# Binary sensor that displays when there are updates
git_update = True

Expand Down
27 changes: 25 additions & 2 deletions src/rpi-cpu2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def print_measured_values(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_c
Wifi Signal: {} %
Wifi Signal dBm: {}
RPI5 Fan Speed: {} RPM
Update Available: {}
Update: {}
""".format(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, check_git_update(script_dir))

print(output)
Expand Down Expand Up @@ -377,6 +377,18 @@ def config_json(what_config):
data["command_topic"] = "homeassistant/update/" + hostname + "/command"
data["payload_press"] = "shutdown"
data["device_class"] = "restart"
elif what_config == "display_on":
data["icon"] = "mdi:monitor"
data["name"] = "Monitor ON"
data["command_topic"] = "homeassistant/update/" + hostname + "/command"
data["payload_press"] = "display_on"
data["device_class"] = "restart"
elif what_config == "display_off":
data["icon"] = "mdi:monitor"
data["name"] = "Monitor OFF"
data["command_topic"] = "homeassistant/update/" + hostname + "/command"
data["payload_press"] = "display_off"
data["device_class"] = "restart"
else:
return ""
# Return our built discovery config
Expand Down Expand Up @@ -512,6 +524,12 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s
if config.discovery_messages:
client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_shutdown/config",
config_json('shutdown_button'), qos=config.qos)
if config.display_control:
if config.discovery_messages:
client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_display_on/config",
config_json('display_on'), qos=config.qos)
client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_display_off/config",
config_json('display_off'), qos=config.qos)
while len(client._out_messages) > 0:
time.sleep(0.1)
client.loop()
Expand Down Expand Up @@ -684,7 +702,12 @@ def update_and_exit():
elif msg.payload.decode() == "shutdown":
print("Shutting down the system...")
os.system("sudo shutdown now")

elif msg.payload.decode() == "display_off":
print("Turn off display")
os.system('su -l {} -c "xset -display :0 dpms force off"'.format(config.os_user))
elif msg.payload.decode() == "display_on":
print("Turn on display")
os.system('su -l {} -c "xset -display :0 dpms force on"'.format(config.os_user))

exit_flag = False
stop_event = threading.Event()
Expand Down

1 comment on commit 0d1ae21

@ErikApption
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick comment here is that this will likely not work with wayland, I haven't had any luck getting xset to work with gnome wayland

Please sign in to comment.