Developing Home Assistant on Windows using the Subsystem for Linux

I love Home Assistant and use it to control my house. I run it on a Pine64 board and it handles everything. However for developing and modifying it I want to have a slightly better environment than nano and a console screen :) So using the Subsystem for Linux I have a Linux based setup to run the hass instance in and can use Visual Studio code to develop the modules as I go, best of both worlds.

Installing the Windows Subsystem for Linux

To install the subsystem go to add/remove programs and tick the Windows Subsystem for Linux (Beta) then run bash.exe and this will start the installation, this may take a while as it pulls down the bits. Once completed and you restarted you can run bash again and then you are ready to start. [caption id=”attachment_1213” align=”aligncenter” width=”534”]Installing the Windows Subsystem for Linux Installing the Windows Subsystem for Linux[/caption]

Setting up the Home Assistant development environment

The https://home-assistant.io/developers/ page has lots of great information but here is how I do it condensed into a single process. In windows I have a dev folder under my user profile. This is c:\Users\myaccount\dev, in the bash console it is mapped to /mnt/c/Users/myaccount/dev/. This way you can work on the files in windows and run them in Linux, just watch out for a CR ;) Run the following in the bash terminal and replace the myaccount with your windows account and myrepo with your fork.

# ensure linux is up to date.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git

# make local repo
mkdir /mnt/c/Users/myaccount/dev/
mkdir /mnt/c/Users/myaccount/dev/home-assistant-bash-config
cd /mnt/c/Users/myaccount/dev/
git clone https://github.com/myrepo/home-assistant.git home-assistant-bash
cd home-assistant-bash
git remote add upstream https://github.com/home-assistant/home-assistant.git

# Ensure we are up to date
git fetch upstream dev
git rebase upstream/dev

# dev environment setup
sudo apt-get install python-pip python3-dev
sudo apt-get install build-essential libssl-dev libffi-dev python-cffi
sudo pip install --upgrade virtualenv

# make and switch to virtual environment.
virtualenv -p python3 .
source ./bin/activate

# prepare dev bits and install all the requirements, this does take a while.
pip install --upgrade cryptography flake8 flake8-docstrings tox colorlog
script/setup

Using Visual Studio Code

If you do not have it download it here http://code.visualstudio.com/ once it is installed got to File -> Open Folder and navigate to c:\Users\myaccount\dev\home-assistant-bash and open that location. Visual Studio Code (or just Code from now on) is smart enough to know it is on an enlistment in Linux and will work with just LF endings so you do not have to worry about that. Code has a concept of extensions, for Python I use Python, MagicPython and Python for VSCode. These provide syntax checking, lint and other actions in the UI to make life easier. Also as you work with files Code may suggest other extension to help you along, and it handles YAML as a bonus and I do all my configuration editing in it.

Starting HASS

Open up bash and run the following.

cd /mnt/c/Users/myaccount/dev/
virtualenv -p python3 .
source ./bin/activate
hass -c /mnt/c/Users/myaccount/dev/home-assistant-bash-config

Development Cycle

This is the rough process I use.

  1. Open up bash and shift to the virtual environment and run hass as a check to see if all is good.
  2. Rebase with upstream to ensure I am not drifting to much (the code base in Home Assistant is very fast moving)
  3. Run hass again to ensure all is good.
  4. Open up Code against the hass folder code c:\Users\myaccount\dev\home-assistant-bash
  5. Open up Code against the configuration folder code c:\Users\myaccount\dev\home-assistant-bash-config
  6. Hack away!
  7. Restart hass as needed and switch between windows while drinking coffee and wondering how you can automate the walking of the dog.

Loading message from the API: ...