-
Notifications
You must be signed in to change notification settings - Fork 58
-
I'm struggling to make a working script to reboot my pi when i click on a button. I made this script:
import os
import subprocess
@pyscript_executor
def restart_system():
subprocess.run(['sudo', 'reboot'])
return 'Rebooting', 200
@service
def _restart_system():
restart_system()
and assigned the service to my button but i get this log in HA:
2024年02月01日 15:47:41.971 ERROR (MainThread) [custom_components.pyscript.file.test._restart_system] Exception in <file.test._restart_system> line 13:
restart_system()
^
FileNotFoundError: [Errno 2] No such file or directory: 'sudo'
I understood that once you wrapped a function in @pyscript_executor you can do anything you want, am i wrong ?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 1 reply
-
Why don't you just homeassistant.restart
to restart HA Core or hassio.host_reboot
to restart the HA VM/machine?
Beta Was this translation helpful? Give feedback.
All reactions
-
I guess my question doesn't work on pi. At least according to more sophisticated solutions from https://community.home-assistant.io/t/reboot-pi-with-script/5130
Beta Was this translation helpful? Give feedback.
All reactions
-
Is it just a PATH
problem? If sudo
is installed and works without a password, then perhaps you need an explicit path, eg, /usr/bin/sudo
(not sure about the correct path on your install), instead of just sudo
?
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you both,
@ALERTua I didn't want to just restart HA, i wanted to reboot the system.
I should have said in the first place, i'm running ha in a docker container, and i found out that i can't easily reboot the host from a container...
But for anyone who come here for solutions, i managed to make pyscript discuss with a flask server running on my pi and it did the job.
I can do almost what ever i want on the host from the container through my flask server:
My pyscript function...
@pyscript_executor
def restart_system():
url = "http://localhost:5002/reboot_system"
response = requests.post(url)
print(response.text)
@service
def _restart_system():
restart_system()
...send command to the flask route:
@app_ui.route('/reboot_system', methods=['POST'])
def reboot_system():
subprocess.run(['sudo', 'reboot'])
return 'Rebooting', 200
Beta Was this translation helpful? Give feedback.