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.
- Change the password of the default user pi from ‘raspberry’
- Passwd
- Passwd
- 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
- vm.swappiness = 10
- Edit config.txt to enable PiFace (platform: rpi_pfio)
- Sudo nano /boot/config.txt
- Uncomment
- dtparam=spi=on
- dtparam=spi=on
- Uncomment
- Sudo nano /boot/config.txt
- Install raspi-gpio for checking rpi_gpio pins
- sudo apt-get install raspi-gpio
- sudo raspi-gpio get xx (xx - GPIO pin)
- 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
- Add
- 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)
- sudo hassbian-config install mosquitto
- 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
- Add
- sudo systemctl restart smbd.service
- sudo nano /etc/samba/smb.conf
- With samba, you can use Notepad++ to edit configuration YAML files on PC
- 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
- sudo apt-get install libmysqlclient-dev
- 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)
- Add
- Install Docker from their repository per TaperCrimp instructions
- curl -sSL https://get.docker.com | sh
- curl -sSL https://get.docker.com | sh
- 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
- sudo docker build -t smartthings-mqtt-bridge -f Dockerfile-rpi \
- Edit mosquito.conf to allow anonymous connections
- sudo nano /etc/mosquito/mosquito.conf
- Add
- allow_anonymous true
- allow_anonymous true
- Add
- sudo nano /etc/mosquito/mosquito.conf
- Edit the MQTT configuration file
- sudo nano /opt/mqtt-bridge/config.yml
- Add
mqtt:
host: mqtt://IP Address of Pi
preface: smartthings
port: 8080
- Add
- sudo nano /opt/mqtt-bridge/config.yml
- 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
- Add
- Edit rc.local to auto start mqtt-bridge
- sudo nano /etc/rc.local
- Add
- sudo docker restart mqtt-bridge
- sudo docker restart mqtt-bridge
- Add
- sudo nano /etc/rc.local
- Update HASSbian
- sudo apt-get update
- sudo apt-get -y upgrade
- 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
- Install Device Handler using SmartThings web site
- My Device Handlers -> Create New Device Handler -> From Code
- Then click Publish -> For Me
- My Device Handlers -> Create New Device Handler -> From Code
- 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
- My Locations ->Home -> My Devices -> New Device
- 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
- My Devices -> MQTT Bridge -> Preferences (edit)
- Add the MQTT Bridge Smart App code to the local SmartTHINGS hub
- My SmartApps -> New SmartApp -> From Code
- My SmartApps -> New SmartApp -> From Code
- Add the MQTT Bridge Smart App to the SmartApps using the iOS App
- Automation -> SmartApps -> Add a SmartApp-> My Apps
- Automation -> SmartApps -> Add a SmartApp-> My Apps
- Add devices to the MQTT Smart App using the iOS App
- Automation -> SmartApps -> MQTT Bridge