MENU

【教程】Ubuntu安装Home Assistant

December 3, 2018 • Read: 350 • 技术,Home Assistant,python阅读设置

接触Home Assistant快一年了,之前是用树莓派装的Hassbian但是由于TF卡的瓶颈坏了好几张卡速度也不咋滴,也没时间 (jiu shi lan de) 写教程,今天有时间给自己做个记录,正好身边有一个闲置的NUC把它装上了Ubuntu,我选的是17.10版本的第一步这里一定要选择英文,不然后面安装会报错

ubuntu.png

选择第一个Install Ubuntu Server 进行安装。
install.png

从这开始就可以选择中文了
QQ截图20181203221529.png

下面一路 “continue”
QQ截图20181203221858.png

我这里选择第一个使用整个硬盘进行安装。
QQ截图20181203222026.png

直到出现下图重启之后这里一台Ubuntu Server 的机器就安装完毕了。
QQ截图20181203222301.png

首先改一下sudoer的设置,以免sudo时总要求输密码

sudo nano /etc/sudoers
#################在末尾添加,其中ubuntu为用户名#################
ubuntu ALL=NOPASSWD: ALL

如果在安装ubuntu时没有更改时区的,使用下面的代码更改时区

sudo dpkg-reconfigure tzdata

更换国内源

sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
sudo nano /etc/apt/sources.list
deb http://mirrors.163.com/ubuntu/ artful main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ artful-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ artful-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ artful-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ artful-backports main restricted universe multiverse
############################## Ubuntu18.04版本 ################################################
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

更新源信息,安装更新

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get autoclean
sudo apt-get clean
sudo apt-get install gcc-multilib #ubuntu 18.04版本

直接安装python3-pip

sudo apt-get install python3-pip 

安装Python3虚拟环境

sudo apt-get install python3-venv

添加一个名为homeassistant的用户

sudo useradd -rm homeassistant

转到/srv目录,建立homeassistant文件夹

cd /srv
sudo mkdir homeassistant

更改此文件夹的所有者和所属组

sudo chown homeassistant:homeassistant homeassistant

更换用户

sudo su -s /bin/bash homeassistant

切换目录,创建并进入虚拟环境

cd /srv/homeassistant
python3 -m venv homeassistant_venv
source /srv/homeassistant/homeassistant_venv/bin/activate

虚拟环境下更新pip, setuptools(此步非常重要,否则安装HA时会报错)

pip install --upgrade pip
pip install --upgrade setuptools

安装依赖netdisco和warrant(此步非常重要,否则安装完HA会报discovery等错误)

pip install netdisco
pip install warrant

开始安装Home Assistant

pip install homeassistant

安装完毕,退出虚拟环境

exit

设置开机启动,建立service文件,将#中间的部分拷入,按ctrl + x, y 退出。

sudo nano /etc/systemd/system/home-assistant@homeassistant.service

[Unit]
Description=Home Assistant
After=network.target

[Service]
Type=simple
User=homeassistant
Environment=PATH="$VIRTUAL_ENV/bin:$PATH"
ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

更新系统设置

sudo systemctl daemon-reload

设置Home Assistant开机启动

sudo systemctl enable home-assistant@homeassistant.service

启动Home Assistant

sudo systemctl start home-assistant@homeassistant.service

安装MOTT

sudo apt-get install mosquitto

更改MQTT设置

sudo nano /etc/mosquitto/conf.d/mqtt.conf
allow_anonymous false
password_file /etc/mosquitto/pwfile

设置用户和密码,这里用户为mqtt,可改为你想设的用户名,密码自设,这里都设为mqtt

sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt

设置开机启动

sudo /etc/init.d/mosquitto enable

在configuration.yaml中加入

mqtt:
  broker: localhost
  port: 1883
  client_id: home-assistant-1
  keepalive: 60
  username: mqtt
  password: mqtt
  protocol: 3.1.1
  discovery: true

到这里Home Assistant和MQTT就安装完成了

注意:

在这里有一个坑,就是在Home Assistant启动之后发现MQTT死活连不上,查看了HA和MQTT的服务状态配置都是正常的的就是连不上,只能通过先重启HA才能连上这才知道是在Home Assistant启动之前MQTT没有启动,然后就给Home Assistant做了一个延时启动。

sudo nano /etc/systemd/system/homeassistant-delay.timer
    
    
[Unit]
Description=Run homeassistant on boot delay
 
[Timer]
OnBootSec=30second  #延时启动的时间
Unit=home-assistant@homeassistant.service
 
[Install]
WantedBy=multi-user.target

先取消Home Assistant的开机启动服务并设置Home Assistant的延时启动服务

sudo systemctl disable home-assistant@homeassistant.service
sudo systemctl daemon-reload
sudo systemctl enable homeassistant-delay.timer

重启ubuntu之后Home Assistant正常运行
最后再上几张效果图

效果1.png

效果2.png

效果3.png

效果4.png

效果5.png