Wednesday, March 26, 2025

Arch Linux: Installing Home Assistant in a Python virtual environment

To install Home Assistant, follow these steps:

1. Update the system and dependencies:
sudo pacman -Syu

2. Install dependencies and components:
sudo pacman -S python python-pip python-virtualenv base-devel libffi openssl

3. Install FFmpeg:
sudo pacman -S ffmpeg

During installation, you will be prompted to choose between two options: "jack2" and "pipewire-jack". In most cases, "pipewire-jack" is recommended. After installation, verify the version with: "ffmpeg -version".

4. Create a user and directories:
sudo useradd -rm homeassistant -d /srv/homeassistant -s /bin/bash
sudo mkdir -p /srv/homeassistant
sudo chown -R homeassistant:homeassistant /srv/homeassistant


5. Set up a virtual environment:
sudo su - homeassistant # Switch to the homeassistant user
cd /srv/homeassistant
python -m venv venv # Create the virtual environment
source venv/bin/activate # Activate the environment

6. Install Home Assistant via pip:
pip install wheel zlib-ng isal
pip install homeassistant


7. Configure the directory:
mkdir -p /srv/homeassistant/.homeassistant
exit
# Exit the homeassistant user
sudo chown -R homeassistant:homeassistant /srv/homeassistant/.homeassistant

8. Create a systemd-service:
sudo nano /etc/systemd/system/homeassistant.service

Add the following content to the file:

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=homeassistant
WorkingDirectory=/srv/homeassistant/.homeassistant
ExecStart=/srv/homeassistant/venv/bin/hass -c "/srv/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target


9. Start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now homeassistant


10. Verify installation:
Open http://server_name_or_ip:8123 in your browser. If the page does not load after a few minutes:

Check if the port is open:
sudo ss -tlnp | grep ':8123'

View logs:
journalctl -u homeassistant -f


Backup Procedure:

1. Stop Home Assistant:
sudo systemctl stop homeassistant

2. Archive the configuration:
sudo tar -czvf bkp_$(date +%Y-%m-%d).tar.gz -C /srv/homeassistant/.homeassistant .

3. Copy the archive to secure storage (e.g., cloud or external drive).

4. Restart the service:
sudo systemctl start homeassistant

5. Add a weekly cron job:
0 3 * * 0 sudo systemctl stop homeassistant && tar -czvf /backup/ha_backup_$(date +\%Y-\%m-\%d).tar.gz -C /srv/homeassistant/.homeassistant . && sudo systemctl start homeassistant


Upgrading Home Assistant:

1. Perform a backup (see above).

2. Stop the service:
sudo systemctl stop homeassistant

3. Activate the virtual environment:
sudo su - homeassistant
source /srv/homeassistant/venv/bin/activate


4. Upgrade Home Assistant:
pip install --upgrade homeassistant
exit
# Exit the homeassistant user

5. Start the service:
sudo systemctl start homeassistant

6. Check logs for errors:
journalctl -u homeassistant -f


Notes:

To roll back to a previous version, restore from a backup and run: "pip install homeassistant==x.x.x" (Replace "x.x.x" with the desired version).
If the Python version changes after an Arch Linux update, recreate the virtual environment:

1. Stop Home Assistant:
sudo systemctl stop homeassistant

2. Check the Python version:
python --version

3. Back up the configuration:
sudo tar -czvf bkp_$(date +%Y-%m-%d).tar.gz -C /srv/homeassistant/.homeassistant .

4. Remove the old virtual environment:
sudo su - homeassistant
cd /srv/homeassistant
rm -rf venv


5. Create a new virtual environment:
python -m venv venv # Uses the updated Python version
source venv/bin/activate

6. Reinstall Home Assistant:
pip install wheel zlib-ng isal homeassistant
exit
# Exit the homeassistant user

7. Start the service:
sudo systemctl start homeassistant

No comments:

Post a Comment