Home Assistant Continuous Integration Workflow

Overview

Development pipelines such as version control to deploying changes automatically. In Home Assistant’s case, you can make changes in YAML text files anywhere and Home Assistant can pick up the changes and automatically load them.

Tools

I use GitHub to store my configurations. GitHub has a few advantages other than being free:

  1. GitHub Actions – Ability to execute scripts when changes are made.
  2. Travis CI – Run tests against changes.
  3. Others have done used GitHub for their Home Assistant deployment pipelines (see below for examples)

Goal


The aim would be to:

  1. Make changes to yaml file. Whether that’s in Home Assistant or on a different machine.
  2. The changes are commited to Git(Hub).
  3. GitHub & Travis CI will run their checks to validate the changes work to the extent it can.
  4. The changes are pulled into Home Assistant if the checks were successful.

Setup TravisCI

There are various guides to setup TravisCI with Github or other repositories on choice.

Once you have setup a build when a push is made, move onto the next part.

Setup Git In Home Assistant

There’s a great guide on Sharing your configuration on GitHub to doing this.

Make sure you backup your configuration as it states in the guide because it would potentially overwrite any files that exist in Home Assistant with the ones in GitHub.

Git Pull Add-On

Install the git pull add-on from the supervisor menu.

In the configuration, enter your private key. To do this using Puttygen, Generate an RSA key.

Add the public key (the top box) into Github as an SSH key.

Go to Conversions > Export OpenSSH Key. Give it a file name and then open the file using a text editor.

Copy everything between the “BEGIN” and “END” lines. In the example above, it would be line 2 to 26 into the deployment_key section. Make sure each line is indented by 2 spaces and starts with a hyphen.

Update the rest to your setup. For example the repository will be specific to your repository, git_branch is the relevant branch you want to pull from, etc.

Test the configuration by saving and starting the add-on. Check the logs to see everything ran successfully.

Set the following ready for the next part in the Git Pull add-on configuration:

repeat:
active: false
interval: 300

The above will stop the plug-in from checking for updates and we will control when it does a check and pull any changes after Travis CI has passed.

Home Assistant Travis CI automation

Create a rest sensor to get Travis CI status. This will check if Travis has change status such as detecting a change in git and performing a build and test against the new changes.

sensor:
# Travis CI build status
- platform: rest
resource: !secret travis_ci_url
headers:
Accept: application/vnd.travis-ci.2+json
Authorization: !secret travis_ci_token
Travis-API-Version: 3
value_template: "{{ value_json.builds[0].state }}"
name: "Travis CI Build Status"

Code can be found here. Add the following to your secret file:
travis_ci_token:
travis_ci_url:

where travis_ci_token is the API token from TravisCI and travis_ci_url is https://api.travis-ci.com/repos/{repository.id}/builds replacing the {repository.id} by doing:
$ curl -L http://api.travis-ci.org/repos/user_name/repo_name
replacing user_name and repo_name respectively.

Save and restart Home Assistant so that the new sensor is created. Create an automation to detect a successful Travis CI build from the new sensor:
automation:
- id: "1613937312554"
alias: "^Home Assistant CI"
description: "https://community.home-assistant.io/t/guide-to-setting-up-a-fully-automated-ci-for-hassio/51576"
trigger:
- platform: state
entity_id: sensor.travis_ci_build_status
from: started
to: passed
condition: []
action:
- service: hassio.addon_start
data:
addon: core_git_pull
mode: single

The automation triggers when a Travis CI build goes from started to passed. This would only change if Travis picks up a change to the code. If the build fails, the automation won’t trigger.

It will then start the add-on git pull which will check and pull in any changes. If you have your configuration set it will restart Home Assistant so that any changes will be loaded.

Summary

There’s a lot of complicated sets to get it working like getting the repository ID from Travis CI could be made more user friendly. At the end of this it means I can edit the YAML files from any where like my phone, push the changes and Home Assistant will automatically pull the changes and load them.

The down side is it needs to restart Home Assistant. A better way of doing this is to reload specific domains like scripts only if the scripts have changed. I doubt that is possible right now from detecting the changes to automating the reload of specific parts of Home Assistant.

The dream would be for anyone to contribute to the changes as well but it is highly specific to my family’s needs.

Full example can be found in https://github.com/dannytsang/homeassistant-config/blob/37de85308bfdccf59d4c2e313a2fb145b74e4b51/packages/integrations/git.yaml

Guide to setting up a fully automated CI for Hassio

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Home Automation, Houseware, Infrastructure, Software and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.