How To Upgrade HomeAssistant In A Python Venv

By Khalid on 2022年06月15日 - 17:51, last updated 2023年11月20日 - 14:36

Home Assistant is an open source home automation platform, written in Python. I have been running it for several years for various tasks.

However, I am running it using a less common Home Assistant Core installation method, consciously deciding not to run the custom Raspberry Pi image for Home Assistant, nor the docker container version for many reasons.

These reasons include:

  • running custom shell scripts that are called by Home Assistant for notifications and such,
  • running other programs on the Raspberry Pi (e.g. RTL-SDR for weather station, InfluxDB for a weather panel, ...),
  • having access to Ubuntu's vast repository of applications easily installable using apt,
  • retaining sysadmin control over the Raspberry Pi.

Instead, I chose to run it as a Python Virtual Environment (venv). This allows me to run Home Assistant on the ARM 64 bit of Ubuntu 20.04 LTS, which runs very well on the Raspberry Pi 4 that I have.

One annoying thing though: the Home Assistant development team do not stick with a certain Python version for long enough. You see, one main reason for using Ubuntu is the Long Term Release (LTS) versions, which are supported for 5 years. That helps a lot in not having to keep upgrading things too often, so less time for maintenance, and less disruptions.

Disappointingly, the Home Assistant team decided in late 2021 to abandon Python 3.8 and mandate Python 3.9 instead.

Luckily, Ubuntu 20.04 LTS supports Python 3.9 (in the focal-updates repository), but that requires upgrading the venv that you have from 3.8 to 3.9.

The upgrade process for a Home Assistant venv is not too hard though, despite many posts on the web claiming otherwise.

Here are the steps in case you are in a similar situation:

Prepare For The Upgrade

First, set the location where you have Home Assistant's venv in. The first line should be the directory that has the two files pyvenv.cfg and requirements.txt.

HA_DIR="$HOME/ha/homeassistant"
CONF_DIR="$HOME_DIR/ha/conf"

Install The New Python Version

Then, using apt, install the required Python packages for the new version.

sudo apt install python3.9-venv python3.9-dev

Upgrade The Venv

python3.9 -m venv --upgrade $HA_DIR

Install PyMySQL

Next, if you are using MariaDB or MySQL for the recorder and history integrations in Home Assistnat, then you need again install pymysql:

. $HA_DIR/bin/activate
pip3 install pymysql

Upgrade Home Assistant

pip3 install --upgrade homeassistant

Check The Configuration File Syntax

Now, make sure that your configuration files are compatible with the new syntax changes of the new version.

$HA_DIR/bin/hass -c $CONF_DIR --script check_config

If you see errors or deprecated warnings, you need to fix them now.

Restart Home Assistant

Kill the running hass process, then start a new one

$HA_DIR/bin/hass -c $CONF_DIR 

There you go, that is all you need to get your Home Assistant venv upgraded. That is, until the Home Assistant team decides to jump to a newer Python version. Hopefully, not too soon ...

Contents:
You must have JavaScript enabled to use this form.
The content of this field is kept private and will not be shown publicly.
Formatting options

Default

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also color it based on syntax.
  • Lines and paragraphs break automatically.