前言
昨天,已經(jīng)運(yùn)行半年 HomeAssistant
服務(wù)的那個(gè)樹莓派肴颊,我決定對(duì) HomeAssistant 進(jìn)行升級(jí)轧粟,但是升級(jí)之后導(dǎo)致出現(xiàn)一系列問題,最終導(dǎo)致 HomeAssistant 服務(wù)無法啟動(dòng)泡一,所以決定重裝,鑒于之前安裝時(shí)沒有任何記錄觅廓,所以決定記錄下來最詳細(xì)的安裝步驟鼻忠,給自己留下一個(gè)記錄,也希望杈绸,這篇文章能夠幫助到更多的人帖蔓。
截止到寫這篇文章的時(shí)候,HomeAssistant 更新的最新版本是 0.65.5 所以這篇文章就基于此版本進(jìn)行介紹瞳脓。
1. 樹莓派安裝系統(tǒng)
請(qǐng)參考 http://www.reibang.com/p/0884f105fd50
2.安裝 HomeAssistant
以下內(nèi)容與官方指導(dǎo)的參考頁面完全一致: https://home-assistant.io/docs/installation/raspberry-pi/
以下內(nèi)容都是在樹莓派終端或者 通過 SSH 遠(yuǎn)程控制樹莓派操作塑娇,如果想要直接復(fù)制代碼到命令行,青蔥 $ 符號(hào)之后開始復(fù)制劫侧,不要復(fù)制 $ 符號(hào)及其之前的任何內(nèi)容钝吮。
個(gè)人建議在網(wǎng)速良好的環(huán)境下安裝或者將軟件源更換到國內(nèi),否則很有可能安裝完之后無法打開頁面板辽,或者頁面返回 404 錯(cuò)誤
2.1 使用以下指令修改樹莓派 pi 用戶的默認(rèn)密碼奇瘦,注意,一定要修改劲弦,否則安裝會(huì)出錯(cuò)耳标。
$ passwd
2.2 更新系統(tǒng)
$ sudo apt-get update
$ sudo apt-get upgrade -y
2.3 安裝依賴
如果你是下載的最新的系統(tǒng),此步驟應(yīng)該會(huì)很快
$ sudo apt-get install python3 python3-venv python3-pip
2.4 使用如下指令添加為系統(tǒng)添加一個(gè) 名為 homeassistant 的賬戶
$ sudo useradd -rm homeassistant
2.5 創(chuàng)建安裝目錄邑跪,并將此目錄的所有者改為 homeassistant
$ cd /srv
$ sudo mkdir homeassistant
$ sudo chown homeassistant:homeassistant homeassistant
2.6 創(chuàng)建并進(jìn)入到 homeassistant 虛擬環(huán)境
$ sudo su -s /bin/bash homeassistant
$ cd /srv/homeassistant
$ python3 -m venv .
$ source bin/activate
2.7 安裝依賴的 python 包
(homeassistant) homeassistant@raspberrypi:/srv/homeassistant $ python3 -m pip install wheel
2.8 安裝 Home Assistant
(homeassistant) homeassistant@raspberrypi:/srv/homeassistant $ pip3 install homeassistant
2.9 啟動(dòng) homeassistant
使用下面一條指令啟動(dòng) Home Assistant 次坡,在此過程中將會(huì)創(chuàng)建配置文件呼猪,并且安裝所需要的一些軟件。此過程實(shí)測(cè)在 3B 上大約耗時(shí)20-30 分鐘砸琅。你可以打開瀏覽器宋距,輸入 ipaddress:8123(將 ipaddress 換成樹莓派的 IP 地址),直到網(wǎng)頁成功加載即可症脂。
(homeassistant) $ hass
如果你可到網(wǎng)頁加載成功加載了 homeassistant 的主頁谚赎,那么切回命令行,先用 Ctrl + C 退出 homeassistant 诱篷,再輸入如下指令退出 python 虛擬環(huán)境壶唤,因?yàn)槲覀冞€要安裝其他軟件。
(homeassistant) $ exit
3. 安裝 Homebridge 與 Mosquitto
此過程也可以參考 https://sspai.com/post/38849 棕所,在我第二次安裝 homeassistant 的過程中闸盔,博主的文章給我提供了很多幫助,在此表示感謝琳省。此部分以及下一部分中部分代碼部分參考了博主的文章迎吵,在此一并聲明。
3.1 安裝 Mosquitto
pi@raspberrypi:~ $ sudo apt-get install mosquitto
3.1 運(yùn)行 Mosquitto
pi@raspberrypi:~ $ mosquitto
之后按下 Ctrl + C 退出 Mosquitto 针贬。
3.3 安裝 Homebridge
pi@raspberrypi:~ $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
pi@raspberrypi:~ $ sudo apt-get install -y nodejs
pi@raspberrypi:~ $ sudo apt-get install libavahi-compat-libdnssd-dev
pi@raspberrypi:~ $ sudo apt-get install npm
pi@raspberrypi:~ $ sudo npm install -g --unsafe-perm homebridge
pi@raspberrypi:~ $ sudo npm install -g homebridge-homeassistant
3.4 運(yùn)行 Homebridge
pi@raspberrypi:~ $ homebridge
之后按下 Ctrl + C 退出 homebridge击费。
3.5 設(shè)置 Homebridge
pi@raspberrypi:~ $ sudo nano /home/pi/.homebridge/config.json
然后將下列內(nèi)容粘貼到文件中,Ctrl + O 保存坚踩,回車確定荡灾,Ctrl + X退出瓤狐。
{
"bridge": {
"name": "Homebridge",
"username": "B8:27:EB:DB:D8:84",
"port": 51826,
"pin": "123-45-678"
},
"platforms": [{
"platform": "HomeAssistant",
"name": "HomeAssistant",
"host": "http://127.0.0.1:8123",
"password": "oplm1234",
"supported_types": ["automation", "binary_sensor", "climate", "cover", "device_tracker", "fan", "group", "input_boolean", "light", "lock", "media_player", "remote", "scene", "script", "sensor", "switch", "vacuum"],
"default_visibility": "hidden",
"logging": true,
"verify_ssl": true
}
]
}
記得將 "username": "B8:27:EB:34:D8:84",
這一行的內(nèi)容指的是樹莓派的 MAC 地址瞬铸,記得改為你自己的樹莓派的 MAC 地址。所有字母一律大寫础锐,小寫無效Iそ凇!皆警!
之后按下 Ctrl + C 退出 homebridge拦宣。
3.6 設(shè)置 mosquitto 用戶名與密碼
pi@raspberrypi:~ $ mosquitto_passwd -c /etc/mosquitto/passwd pi
這里的 pi 是創(chuàng)建的 mosquitto 的用戶名,運(yùn)行完之后將會(huì)提示輸入密碼信姓,改密碼將會(huì)密文存儲(chǔ)鸵隧,所以請(qǐng)牢記。你也可以將 pi 換成其他你喜歡的用戶名
4.設(shè)置開機(jī)自啟
4.1 設(shè)置 homeassistant 開機(jī)自啟
對(duì)于 raspbian 來說你可以通過以下方式意推,其他操作系統(tǒng)請(qǐng)參考官方文檔 https://home-assistant.io/docs/autostart/ 豆瘫。
4.1.1 創(chuàng)建文件
執(zhí)行如下指令創(chuàng)建文件:
pi@raspberrypi:~ $ sudo nano -w /etc/systemd/system/home-assistant@homeassistant.service
然后將如下內(nèi)容復(fù)制到文件中,然后按 Ctrl + O 再按 Enter 保存菊值,再按 Ctrl + X 退出外驱。如果你的安裝目錄改變了育灸,請(qǐng)將 ExecStart= 之后的 /srv/homeassistant/bin/hass 更改為你的 homeassistant 的安裝目錄,如果你是按照本教程操作昵宇,則無需更改磅崭。
[Unit]
Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=%i
ExecStart=/srv/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant"
[Install]
WantedBy=multi-user.target
4.1.1 配置 systemd
先執(zhí)行如下指令重新加載 systemd
pi@raspberrypi:~ $ sudo systemctl --system daemon-reload
再執(zhí)行如下指令以重新以允許 homeassistant 開機(jī)自啟。
pi@raspberrypi:~ $ sudo systemctl enable home-assistant@homeassistant
4.1 設(shè)置 homebridge 與 mosquitto 開機(jī)自啟
注意:配置 HB 前程序會(huì)運(yùn)行錯(cuò)誤瓦哎,提示缺少配置文件砸喻,可先不理會(huì)。
cd /etc/default
sudo nano homebridge
粘貼一下內(nèi)容到文件中
# Defaults / Configuration options for homebridge
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS= /home/pi/.homebridge
所有帶有 # 字的行皆為注釋杭煎,可整行刪除不影響恩够。上述指令的作用是指定 Homebridge 的配置文件及緩存所在文件夾。
如果你想要 Homebridge 顯示更詳細(xì)的日志羡铲,可以繼續(xù)添加如下字段:
DEBUG=*
之后 Ctrl+X蜂桶,Y,回車。繼續(xù)輸入:
cd /etc/systemd/system
sudo nano homebridge.service
復(fù)制粘貼
[Unit]
Description=Node.js HomeKit Server
After=syslog.target network-online.target
[Service]
Type=simple
User=pi
EnvironmentFile=/etc/default/homebridge
ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target
Ctrl+X植阴,Y极景,回車
cd /
sudo systemctl daemon-reload
sudo systemctl enable homebridge
sudo systemctl start homebridge
sudo systemctl status homebridge
sudo systemctl enable mosquitto
然后執(zhí)行sudo reboot
重啟即可。