Created
May 16, 2020 06:21
-
-
Save jinjie/ea1a54f3b21c9c31c50815dc1750e19f to your computer and use it in GitHub Desktop.
Setup Home Assistant easily on Raspberry Pi with venv. Improved from https://www.home-assistant.io/docs/installation/virtualenv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################# | |
# WARNING: DO NOT RUN THIS SCRIPT DIRECTLY. # | |
# THIS SHOULD BE RUN AS ROOT IN A RPI # | |
############################################# | |
if [ ! "$(whoami)" == "root" ]; | |
then | |
echo "YOU MUST RUN THIS AS ROOT" | |
exit 1 | |
fi | |
if [ ! "$1" == "confirm" ]; | |
then | |
echo "WARNING: DO NOT RUN THIS SCRIPT DIRECTLY. IF YOU ARE SURE WHAT YOU ARE DOING, RUN THIS AS:" | |
echo "setup_script.sh confirm" | |
exit 1 | |
fi | |
apt-get update && \ | |
apt-get -y upgrade && \ | |
apt-get -y install python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev && \ | |
# It's okay if this fails, attempt to create the group so useradd will work | |
groupadd dialout | |
groupadd gpio | |
groupadd i2c | |
useradd -rm homeassistant -G dialout,gpio,i2c | |
mkdir -p /srv/homeassistant | |
chown -R homeassistant.homeassistant /srv/homeassistant | |
runuser -l homeassistant -c "cd /srv/homeassistant && python3 -m venv ." && \ | |
runuser -l homeassistant -c "cd /srv/homeassistant && . bin/activate && python3 -m pip install wheel" && \ | |
runuser -l homeassistant -c "cd /srv/homeassistant && . bin/activate && pip3 install homeassistant" | |
SYSTEMCTL=`which systemctl` | |
if [ "$SYSTEMCTL" ]; | |
then | |
echo | |
echo "************************************************************" | |
echo "* Setting up service: [email protected] *" | |
echo "************************************************************" | |
# Setup systemd | |
echo "[Unit] | |
Description=Home Assistant | |
After=network-online.target | |
[Service] | |
Type=simple | |
User=%i | |
ExecStart=/srv/homeassistant/bin/hass -c \"/home/%i/.homeassistant\" | |
Restart=on-failure | |
RestartSec=5s | |
[Install] | |
WantedBy=multi-user.target | |
" > /etc/systemd/system/[email protected] | |
$SYSTEMCTL --system daemon-reload && \ | |
$SYSTEMCTL enable [email protected] | |
else | |
echo | |
echo "***************************************************" | |
echo "* Systemd not found. Skipping installing service. *" | |
echo "***************************************************" | |
fi | |
if [ $? ]; | |
then | |
echo | |
echo "**********************************************************" | |
echo "* Successfully installed Home Assistant *" | |
if [ "$SYSTEMCTL" ]; | |
then | |
echo "* Systemd was installed. Start Home Assistant by *" | |
echo "* systemctl start [email protected] *" | |
else | |
echo "* Systemd not installed. You can start Home Assistant by *" | |
echo "* su homeassistant *" | |
echo "* cd /srv/homeassistant *" | |
echo "* . bin/activate *" | |
echo "* bin/hass *" | |
fi | |
echo "**********************************************************" | |
exit 0 | |
else | |
echo | |
echo "***********************" | |
echo "* Installation FAILED *" | |
echo "***********************" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is to automate the long list of instructions from https://www.home-assistant.io/docs/installation/virtualenv