Continuation from Part I

Software Design

As their website states: Home Assistant is an open-source home automation platform running on Python 3. Track and control all devices at home and automate control. Perfect to run on a Raspberry Pi.

Because I was going to run my Raspberry Pi headless, I installed PuTTY on my Windows 10 machine for SSH.

One of the first things I noticed was that my keyboard number pad didn’t work in PuTTY. It turns out that in the Raspberry Pi the default keyboard is “gb” and I needed to change it to “us”. This was done by editing the /etc/default/keyboard file to change XBBLAYOUT=”us”.

I also found the right mouse button, which is used for pasting text, to be overly sensitive. I changed the PuTTY configuration for the “Action of mouse buttons:” to “Windows…” from “Compromise…” so that a right mouse click now displays a context menu instead of just pasting what’s in the buffer.

There a currently three ways to install Home Assistant on a Raspberry Pi: from a complete turnkey installation, Hass.io to an All-in-One installer on top of Raspbian to a customized operating system, HASSbian.

I decided to go with HASSbian because it still allowed some access to the OS, and then used the standard instructions for a wired network. HASSbian provided support for most of what I needed but not SmartThings which required additional steps for installation and configuration. Also, since Home Assistant is written Python 3, you have access to all the source files at /srv/homeassistant/lib/python3.4/site-packages/homeassistant in case you need to edit them.

These are the steps I did to update and configure HASSbian after the initial installation. Steps nine and ten are for changing the database that is used for the recorder to MySQL, which has much better performance. Steps nine through fourteen are for adding SmartThings support.

  1. Change the password of the default user pi from ‘raspberry’
    • Passwd

  2. Change swappiness to 10 in 98-rpi.conf since swap on SD Card
    • Sudo nano /etc/sysctl.d/ 98-rpi.conf
    • Add
      • vm.swappiness = 10

  3. Edit config.txt to enable PiFace (platform: rpi_pfio)
    • Sudo nano /boot/config.txt
      • Uncomment
        • dtparam=spi=on

  4. Install raspi-gpio for checking rpi_gpio pins
    • sudo apt-get install raspi-gpio
    • sudo raspi-gpio get xx (xx - GPIO pin) 

  5. Edit Home Assistant configuration file to use PushBullet
    • cd /home/homeassistant/.homeassistant
    • sudo nano configuration.yaml
      • Add
            
        notify:
           
        platform: pushbullet
            api_key: 'YOUR_KEY'
            name: pushbullet

  6. Use hasbian-config to install additional packages
    • sudo hassbian-config install mosquitto
         
      (MQTT broker)
    • sudo hassbian-config install samba
         
      (Adds home share for the raspberry Pi)

  7. Edit smb.conf to add additional shares
    • sudo nano /etc/samba/smb.conf
      • Add
          [home]
            path = /home/pi
            writeable = yes
            guest ok = yes
            create mask = 0644
            directory mask = 0755
            force user = pi
    • sudo systemctl restart smbd.service


  8. With samba, you can use Notepad++ to edit configuration YAML files on PC

  9. Install MySQL and MySQL Client from switched instructions
    • sudo apt-get install libmysqlclient-dev
                  Or if using the MariaDB:
    • sudo apt-get install libmariadbclient-dev

    • sudo apt-get install python-dev python3-dev
    • sudo apt-get install mysql-server
         
      (Create root password when prompted)
    • sudo mysql -u root -p
          (Enter root password)    
    • CREATE DATABASE hass_db;
    • CREATE USER 'hassuser'@'localhost' IDENTIFIED BY 'XXX';
         
      (XXX is your password)
    • GRANT ALL PRIVILEGES ON *.* TO 'hassuser'@'localhost';
    • FLUSH PRIVILEGES;
    • QUIT;
    • sudo su -s /bin/bash homeassistant
         
      (Enter the virtual environment)
    • source /srv/homeassistant/bin/activate
    • pip3 install --upgrade mysqlclient
    • exit

  10. Edit Home Assistant configuration file to use MySQL
    • cd /home/homeassistant/.homeassistant
    • sudo nano configuration.yaml
      • Add
          recorder:
             db_url: mysql://hassuser:XXX@127.0.0.1/hass_db?charset=utf8

        (XXX is your password)

  11. Install Docker from their repository per TaperCrimp instructions
    • curl -sSL https://get.docker.com | sh

  12. Rebuild the Docker instance of smartthings-mqtt-bridge (all one line)
    • sudo docker build -t smartthings-mqtt-bridge -f Dockerfile-rpi \
      https://github.com/stjohnjohnson/\
      smartthings-mqtt-bridge.git
    • sudo docker run --restart=always -d --name="mqtt-bridge" -v \
      /opt/mqtt-bridge:/config -p 8080:8080 smartthings-mqtt-bridge

  13. Edit mosquito.conf to allow anonymous connections
    • sudo nano /etc/mosquito/mosquito.conf
      • Add
        • allow_anonymous true

  14. Edit the MQTT configuration file
    • sudo nano /opt/mqtt-bridge/config.yml
      • Add
          mqtt:
            host: mqtt://IP Address of Pi
            preface: smartthings
            port: 8080

  15. Edit Home Assistant configuration file to use MQTT
    • cd /home/homeassistant/.homeassistant
    • sudo nano configuration.yaml
      • Add
          mqtt:
            broker: localhost
            discovery: true
            discovery_prefix: smartthings

  16. Edit rc.local to auto start mqtt-bridge
    • sudo nano /etc/rc.local
      • Add
        • sudo docker restart mqtt-bridge

  17. Update HASSbian
    • sudo apt-get update
    • sudo apt-get -y upgrade

  18. Update Home Assistant
    • sudo systemctl stop This email address is being protected from spambots. You need JavaScript enabled to view it.
    • sudo su -s /bin/bash homeassistant
    • source /srv/homeassistant/bin/activate
    • pip3 install --upgrade homeassistant
    • exit
    • sudo systemctl start This email address is being protected from spambots. You need JavaScript enabled to view it.

For SmartThings to communicate to the Home Assistant running on the Raspberry Pi, support needs to be added for the MQTT Bridge.These steps are based on stjohnjohnson instructions for smartthings-mqtt-bridge

  1. Install Device Handler using SmartThings web site
    • My Device Handlers -> Create New Device Handler -> From Code
    • Then click Publish -> For Me

  2. Add the “MQTT Device” to the local SmartTHINGS hub
    • My Locations ->Home -> My Devices -> New Device
              
      Name:              MQTT Bridge
         
      Device Network ID: MAC Address of Pi
         
      Type:              MQTT Bridge
         
      Version:           Self-Published
              Location:          Home

  3. Configure the “MQTT Device” in the local SmartThings hub
    • My Devices ->  MQTT Bridge -> Preferences (edit)
              MQTT Bridge IP Address:  IP Address of Pi
          MQTT Bridge Port:        8080
          MQTT Bridge MAC Address: MAC Address of Pi

  4. Add the MQTT Bridge Smart App code to the local SmartTHINGS hub
    • My SmartApps  -> New SmartApp -> From Code

  5. Add the MQTT Bridge Smart App to the SmartApps using the iOS App
    • Automation -> SmartApps -> Add a SmartApp-> My Apps

  6. Add devices to the MQTT Smart App using the iOS App
    • Automation -> SmartApps -> MQTT Bridge


Continue to Part III