bookmark_borderDeveloping 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.

Installing the Windows Subsystem for Linux
Installing the Windows Subsystem for Linux

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.
Sytone

Iā€™m a principal engineer working at Microsoft. I have run and supported services for over twenty years and worked in multiple countries. I love working with passionate people and new technology.

bookmark_borderUsing Microsoft Flow with Home Assistant

Now that Microsoft Flow is out of Beta I thought I would play with it and integrate it with Home Assistant running my house. The Microsoft Flow is fairly flexible and has more flow options than IFTTT to make choices.

To use this with Home Assistant I am going to use the REST API to make and set a sensor value, you can then take this and make it as complicated as you want. šŸ™‚

Settings

You need to ensure you have this information at hand.
* Home Assistant Endpoint – https://publicdomin:optionalPort/api
* Your password for the http component. If you public expose HA without a password, stop and fix it now!
* The name of the sensor you want to update. sensor.kitchen_temperature in this case.

  1. Go to https://flow.microsoft.com and login.
  2. Make a new Flow from a blank template
  3. Search for button (you can use other triggers but this is an easy start)
  4. Add a new step of type action.
  5. Select HTTP
  6. Change Method to POST
  7. In URI put the full URI of the sensor you want to change. Using the data above this is https://publicdomin:optionalPort/api/states/sensor.kitchen_temperature
  8. In headers add the following JSON blob.
    {
    “x-ha-access”: “YOURPASSWORD”,
    “Content-Type”: “application/json”
    }
  9. In Body put the JSON to update the sensor.
    {
    “state”: “25”,
    “attributes”: {
    “unit_of_measurement”: “Ā°C”
    }
    }
  10. Save the Flow!

Now open up Flow on your phone and log in with the same account, you will see a nice big button with manual -> HTTP on it. Click that button and look at the state of the sensor in Home Assistant. You will see it with the value you set in the body.

To trouble shoot, in the web UI click on the i with a circle around it. It will show a log of successes and failures. If you click on that you can drill into the details of the flow and the responses from Home Assistant.

Sytone

Iā€™m a principal engineer working at Microsoft. I have run and supported services for over twenty years and worked in multiple countries. I love working with passionate people and new technology.