樹(shù)莓派安裝 HomeAssistant

前言

昨天,已經(jīng)運(yùn)行半年 HomeAssistant

服務(wù)的那個(gè)樹(shù)莓派,我決定對(duì) HomeAssistant 進(jìn)行升級(jí),但是升級(jí)之后導(dǎo)致出現(xiàn)一系列問(wèn)題,最終導(dǎo)致 HomeAssistant 服務(wù)無(wú)法啟動(dòng),所以決定重裝,鑒于之前安裝時(shí)沒(méi)有任何記錄,所以決定記錄下來(lái)最詳細(xì)的安裝步驟,給自己留下一個(gè)記錄,也希望,這篇文章能夠幫助到更多的人。

截止到寫(xiě)這篇文章的時(shí)候,HomeAssistant 更新的最新版本是 0.65.5 所以這篇文章就基于此版本進(jìn)行介紹。

1. 樹(shù)莓派安裝系統(tǒng)

請(qǐng)參考 http://www.itdecent.cn/p/0884f105fd50

2.安裝 HomeAssistant

以下內(nèi)容與官方指導(dǎo)的參考頁(yè)面完全一致: https://home-assistant.io/docs/installation/raspberry-pi/

以下內(nèi)容都是在樹(shù)莓派終端或者 通過(guò) SSH 遠(yuǎn)程控制樹(shù)莓派操作,如果想要直接復(fù)制代碼到命令行,青蔥 $ 符號(hào)之后開(kāi)始復(fù)制,不要復(fù)制 $ 符號(hào)及其之前的任何內(nèi)容。

個(gè)人建議在網(wǎng)速良好的環(huán)境下安裝或者將軟件源更換到國(guó)內(nèi),否則很有可能安裝完之后無(wú)法打開(kāi)頁(yè)面,或者頁(yè)面返回 404 錯(cuò)誤

2.1 使用以下指令修改樹(shù)莓派 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 ,在此過(guò)程中將會(huì)創(chuàng)建配置文件,并且安裝所需要的一些軟件。此過(guò)程實(shí)測(cè)在 3B 上大約耗時(shí)20-30 分鐘。你可以打開(kāi)瀏覽器,輸入 ipaddress:8123(將 ipaddress 換成樹(shù)莓派的 IP 地址),直到網(wǎng)頁(yè)成功加載即可。

    (homeassistant) $ hass
image

如果你可到網(wǎng)頁(yè)加載成功加載了 homeassistant 的主頁(yè),那么切回命令行,先用 Ctrl + C 退出 homeassistant ,再輸入如下指令退出 python 虛擬環(huán)境,因?yàn)槲覀冞€要安裝其他軟件。

(homeassistant) $ exit

3. 安裝 Homebridge 與 Mosquitto

此過(guò)程也可以參考 https://sspai.com/post/38849 ,在我第二次安裝 homeassistant 的過(guò)程中,博主的文章給我提供了很多幫助,在此表示感謝。此部分以及下一部分中部分代碼部分參考了博主的文章,在此一并聲明。

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)容指的是樹(shù)莓派的 MAC 地址,記得改為你自己的樹(shù)莓派的 MAC 地址。所有字母一律大寫(xiě),小寫(xiě)無(wú)效!??!
之后按下 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è)置開(kāi)機(jī)自啟

4.1 設(shè)置 homeassistant 開(kāi)機(jī)自啟

對(duì)于 raspbian 來(lái)說(shuō)你可以通過(guò)以下方式,其他操作系統(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 的安裝目錄,如果你是按照本教程操作,則無(wú)需更改。

[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 開(kāi)機(jī)自啟。

pi@raspberrypi:~ $ sudo systemctl enable home-assistant@homeassistant

4.1 設(shè)置 homebridge 與 mosquitto 開(kāi)機(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 重啟即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容