GithubHelp home page GithubHelp logo

home-assistant-lxc's Introduction

This is my personal documentation on how I installed Home-Assistant in LXC.

Main setup

Install dependecies:

apt-get update 
apt-get upgrade -y
apt-get install -y python3.9 python3.9-dev python3.9-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5 libturbojpeg0-dev libturbojpeg tzdata ssmtp

Create System User

useradd -r -M -d /srv/homeassistant -s /bin/bash homeassistant
sudo mkdir /srv/homeassistant
sudo chown homeassistant:homeassistant /srv/homeassistant

Install Home-Assistant

cd /srv/homeassistant
su homeassistant
python3.9 -m venv .
source bin/activate
pip install homeassistant
exit

Setup System Service

nano /etc/systemd/system/home-assistant.service
[Unit]
Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=homeassistant
WorkingDirectory=/srv/homeassistant/.homeassistant
ExecStart=/srv/homeassistant/bin/hass -c "/srv/homeassistant/.homeassistant"
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

Activate service

systemctl --system daemon-reload
systemctl enable home-assistant
systemctl start home-assistant
systemctl status home-assistant

Optional

Setup Auto update:

Check if updates available:

nano /srv/homeassistant/check-for-updates.sh 
#!/bin/bash

source /srv/homeassistant/bin/activate

if [ `grep -c homeassistant <<< $(pip list --outdated --format freeze)` -ge 1 ]; then
  echo 1
else
  echo 0
fi

exit 0
chmod +x /srv/homeassistant/update-check.sh
/srv/homeassistant/update-check.sh
chown homeassistant:homeassistant /srv/homeassistant/update-check.sh

Upgrade script:

nano /srv/homeassistant/upgrade-home-assistant.sh
#!/bin/bash

source /srv/homeassistant/bin/activate
pip3 install --upgrade homeassistant

exit 0
chown homeassistant:homeassistant /srv/homeassistant/upgrade-home-assistant.sh
chmod +x /srv/homeassistant/upgrade-home-assistant.sh

Setup mailer:

nano /etc/ssmtp/ssmtp.conf
UseSTARTTLS=YES
FromLineOverride=YES
[email protected]
mailhub=smtp.gmail.com:587
[email protected]
AuthPass=YOUR-PASSWORD!
  • If your Gmail account is secured with two-factor authentication, you need to generate a unique App Password to use in ssmtp.conf. You can do so on your App Passwords page. Use you Gmail username (not the App Name) in the AuthUser line and use the generated 16-character password in the AuthPass line, spaces in the password can be omitted.

  • If you do not use two-factor authentication, you need to allow access to unsecure apps. You can do so on your Less Secure Apps page.

(Source: https://wiki.archlinux.org/title/SSMTP#Forward_to_a_Gmail_mail_server)

Setup cronjob:

Backup config before upgrade, rotate logs and backups.

nano home-assistant-updater.cron
#!/bin/bash
echo "Starting updater $(date)"
if systemctl is-active --quiet home-assistant.service; then
  if [ $(su -c '/srv/homeassistant/check-for-updates.sh' homeassistant) -ne 0 ]; then
    echo "Stoping service"
    systemctl stop home-assistant.service
    echo "Backing up config"
    mkdir -p /srv/homeassistant/backups
    tar -czf "/srv/homeassistant/backups/Home-Assistant-config-$(date -u -I).tar.gz" -C /srv/homeassistant/ .homeassistant
    echo "Starting upgrade"
    su -c '/srv/homeassistant/upgrade-home-assistant.sh' homeassistant
    echo "Starting service"
    systemctl start home-assistant.service
    echo "Checking service"
    sleep 5
    if systemctl is-active --quiet home-assistant.service; then
      if [ $(ls /srv/homeassistant/backups | wc -l) -ge 5 ]; then
        echo "Deleting old backups"
        echo "removing $(ls -t /srv/homeassistant/backups/Home-Assistant-config-*.tar.gz | tail -1)"
        rm "$(ls -t /srv/homeassistant/backups/Home-Assistant-config-*.tar.gz | tail -1)"
      fi
      if [ $(ls /var/log/home-assistant-update/ | wc -l) -ge 4 ]; then
        echo "Deleting old logs"
        echo "removing $(ls -t /var/log/home-assistant-update/*.log | tail -1)"
        rm "$(ls -t /var/log/home-assistant-update/*.log | tail -1)"
      fi
      sendmail [email protected] <<< "Subject: Home Assistant Update successful" <<<  "Home Assistant is now up-to-date"
      echo "Update successful"
    else
      sendmail [email protected] <<< "Subject: Home Assistant Update FAILED" <<<  "Home Assistant is broken!"
      echo "FAILURE! Service is not comming up!"
      exit 1
    fi
  else
    echo "Home Assistant is up-to-date"
  fi
else
  echo "FAILURE! Service is not running!"
  exit 1
fi
echo ""
exit 0

Run every saturday at 04:00, log to /var/log/home-assistant-update/

crontab -e
  0 4    *   *   6   /root/home-assistant-updater.cron 2>&1 >> "/var/log/home-assistant-update/$(date -u -I).log"

home-assistant-lxc's People

Contributors

mietzen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.