I couldn’t seem to find any news relating to when official RedHat/CentOS packages for Python 3.7 (or later) might become available, so I decided to tackle things myself. After wrestling with the update process, I thought I’d post the process I used in case other folks find it useful. The very basic steps are:
- Compile and install the desired version of Python.
- Create a new virtual environment using the new version of Python.
- Install a new Home Assistant instance into the new virtual environment that will use the settings of the previous instance.
My current Home Assistant virtual environment is located on my server at /home/homeassistant/srv/homeassistant
.
My configuration files are located at /home/homeassistant/.homeassistant
.
The user that owns/runs my instance is named homeassistant
.
Compile and Install Python
In my case, I decided to use Python 3.8.0 since it’s currently the most recent stable version. But you should theoretically be able to use whichever 3.x version of Python you’d like – just download the relevant tarball. I made use of the following references:
$ sudo systemctl stop home-assistant.service
$ sudo yum install sqlite-devel.x86_64 libsqlite3x-devel.x86_64
$ cd ~/Downloads
$ mkdir python38
$ wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
$ tar xzf Python-3.8.0.tgz
$ cd Python-3.8.0
$ ./configure --enable-optimizations --enable-loadable-sqlite-extensions
$ make
$ sudo make altinstall
An important item to note here is that I needed to compile the new version of Python with the enable-loadable-sqlite-extensions
option, as this is not enabled by default and is required by some Home Assistant modules (found this out the hard way). In my case I also needed to install the relevant sqlite dev packages via yum install sqlite-devel.x86_64 libsqlite3x-devel.x86_64
.
Create new virtual environment and install new HA instance
This follows the references below pretty closely with a few tweaks:
$ cd /home/homeassistant/srv
$ mv homeassistant homeassistant_old
$ mkdir homeassistant
$ chown homeassistant:homeassistant homeassistant
$ sudo -u homeassistant -H -s
$ cd /home/homeassistant/srv/homeassistant
$ /usr/local/bin/python3.8 -m venv .
$ source bin/activate
$ pip install --upgrade pip
$ pip3 install homeassistant
$ pip3 install --upgrade homeassistant-pyozw
$ pip3 install HAP-python
$ pip3 install pymyq
Here I am stashing my current virtenv (by moving it to homeassistant_old
) and creating a new virtenv in /home/homeassistant/srv/homeassistant
using the newly compiled version of Python. I then switch into the new virtenv and install the Home Assistant packages. Note that I manually install a few packages I use in my personal setup: the Z-Wave package (homeassistant-pyozw
), HomeKit package (HAP-python
), and MyQ package (pymyq
). For whatever reason, Home Assistant is not able to install/compile these packages automatically when I start it up, although it tries and generates errors in the log (again, learned the hard way). But manually installing them from within the virtenv seems to work just fine. No idea why . But this is where you would install any other Python packages you might need for your HA instance.
Another minor tweak specific to my setup is the need to implement the fixes previously described at Aeotec Door/Window Sensor 7 compatibility.
Use prior HA configuration
I needed to modify the systemd service I had previously created based on the advice from https://www.home-assistant.io/docs/installation/centos/. I am no longer using the Python 3.6 package from the RedHat SCL repo, and I specifically note the location of my Home Assistant configuration files so the new virtenv and HA instance will use them. My systemd service config file is located at /etc/systemd/system/home-assistant.service
.
[Unit]
Description=Home Assistant
After=network.target
[Service]
Type=simple
User=homeassistant
# Make sure the virtualenv Python binary is used
Environment=VIRTUAL_ENV="/home/homeassistant/srv/homeassistant"
Environment=PATH="$VIRTUAL_ENV/bin:$PATH"
# New ExecStart using Python virtual environment
ExecStart=/home/homeassistant/srv/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant"
[Install]
WantedBy=multi-user.target
Start HA
Now I can start up my Home Assistant instance (while crossing my fingers). Since I modified a systemd service, I will also need to reload the config files.
$ sudo systemctl daemon-reload
$ sudo systemctl start home-assistant.service
And there you have it. My HA instance started up with all my previous settings intact, and it now uses Python 3.8 – so it will survive the upcoming deprecation while I wait for RedHat/CentOS to release official Python 3.7/3.8 packages.
Automating Home Assistant Updates
I also use a shell script (/usr/local/bin/home-assistant_update.sh
) to automatically update my HA instance from time to time. The script is run on a routine basis as a cronjob. The script also needed some tweaking since I am not using the SCL package anymore, and I figured I’d provide it here for those who are interested.
#!/bin/bash
# Print the current date
date
# Stop Home Assistant service
systemctl stop home-assistant
# Become user 'homeassistant'
su -s /bin/bash homeassistant <<'EOF'
# Activate the Python virtual environment
source /home/homeassistant/srv/homeassistant/bin/activate
# Update Home Assistant and related packages
pip install --upgrade pip
pip3 install --upgrade homeassistant-pyozw
pip3 install --upgrade homeassistant
exit
EOF
# Restart Home Assistant service
systemctl start home-assistant
printf "\n\n"