I’ve looked at this a couple of times - the trouble is that the virtual_env needs to activated for the running shell, not the child shells that spawn to run the commands. I suspect that it will need to be a bash function to work correctly - still a WIP for me
I’m looking to achieve the same thing (I’m also using a VirtualEnv, via the All-In-One Raspberry Pi installer) – if anyone comes up with a solution please post!
I automated the update process using the following script:
#!/bin/bash
# Become user 'hass'
sudo su -s /bin/bash hass <<'EOF'
# Activate the virtualenv
source /opt/home-assistant/bin/activate
# Install Home Assistant
pip3 install --upgrade homeassistant
exit
EOF
# Restart Home Assistant
sudo systemctl restart home-assistant@hass
The trick is to combine all commands that need to be run by the hass user into a section that starts and ends with EOF.
This script works on Ubuntu 16.04.1 LTS in a virtual environment - please adapt the path to your installation passed into the source command.
As you can see, I am actually updating the software without stopping it, and instead restart after the update has finished. The update just takes seconds here, and I haven’t seen issues. Might be safer to stop first, then update, then start.
Im trying to run this script from HA (Raspberry Pi venv) but Im getting this:
[sudo] password for homeassistant:
The script runs fine out of the virtual environment. I tried changing the script location to /home/homeassistant/.homeassistant/scripts/hass_update.sh without success.
My script:
#!/bin/bash
# Become user 'hass'
sudo su -s /bin/bash homeassistant <<'EOF'
# Activate the virtualenv
source /srv/homeassistant/bin/activate
# Install Home Assistant
pip3 install --upgrade homeassistant
exit
EOF
# Restart Home Assistant
sudo systemctl restart home-assistant@homeassistant
I have this on the shell_commands file:
#Update Home Assistant
hass_update: "/home/pi/hass_update.sh"
The user wasn’t there, i added the user and now is working. The only thing is that is not working is the reboot at the end of the scrip. I have to manually reboot the Pi.
Login to Raspberry Pi ssh pi@your_raspberry_pi_ip
Change to homeassistant user sudo su -s /bin/bash homeassistant
Change to virtual enviroment source /srv/homeassistant/homeassistant_venv/bin/activate
Update Home Assistant pip3 install --upgrade homeassistant
Type exit to logout the hass user and return to the pi user.
If i try to use the command
sudo systemctl stop home-assistant@homeassistant
I receive this error: