Skip to content

Instantly share code, notes, and snippets.

@jinjie
Created May 16, 2020 06:21
Show Gist options
  • Save jinjie/ea1a54f3b21c9c31c50815dc1750e19f to your computer and use it in GitHub Desktop.
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
#!/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: home-assistant@homeassistant.service *"
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/home-assistant@homeassistant.service
$SYSTEMCTL --system daemon-reload && \
$SYSTEMCTL enable home-assistant@homeassistant.service
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 home-assistant@homeassistant.service *"
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
@jinjie
Copy link
Author

jinjie commented May 16, 2020

This is to automate the long list of instructions from https://www.home-assistant.io/docs/installation/virtualenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment