Home Assistant and Python upgrade

Posted on and updated in category «Tech» by fnv with tags ,

I don't follow HomeAssistant development closelly but they force you to upgrade to Python higher version regullarly in short period of time. I can't remember when it was last time but I think maybe one or one and halt year ago... So, let's go...
I use Armbian Bullseye on RockPro64. The step may be a bit different on another platforms.
I get inspiration here Upgrading python virtual environment and forum post.

Python

Let start with required packages under root account:

apt install pkg-config build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

And continue under root account to get, configure, make and install Python:

mkdir Python
wget https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tgz 
tar xvfJ Python-3.11.5.tar.xz
./configure --prefix=/usr/local --enable-optimizations --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make -j 2
make altinstall

Now, let's configure latest installed Python version to default in the system:

update-alternatives --install /usr/bin/python python /usr/local/bin/python3.11 3
update-alternatives --config python
update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.11 2
update-alternatives --config pip
update-alternatives --install /usr/bin/pip3 pip3 /usr/local/bin/pip3.11 2
update-alternatives --config pip3

And last step for Python, upgrade pip:

pip install --user --upgrade pip

Home Assistant

Under root account stop Home Assistant application:

systemctl stop homeassistant

Switch to homeassistant account and save required packages:

sudo -u homeassistant -H -s
cd /opt/homeassistant
source bin/activate
pip3.10 freeze --local > requirements.txt
deactivate

Again under root account backup current HA version and prepare dir for new one:

cd /opt
mv homeassistant homeassistant.310
mkdir homeassistant
chown homeassistant:homeassistant homeassistant

And go back to homeassistant account and finally prepare and install homeassistant:

cd /opt/homeassistant
python3.11 -m venv .
source bin/activate
cp ../homeassistant.310/requirements.txt .
python -m pip install wheel
pip install --upgrade setuptools
pip install --upgrade homeassistant

Under root account start HA:

systemctl start homeassistant