一、准备条件
1.一台树莓派
2.更改系统源和pip源,并使树莓派可以实现远程桌面连接,我选择的是Xrdp
二、接入步骤
1.homeassistant的安装
以下内容在putty和终端上进行都可以,若要复制请复制 $ 后的内容
下面是homeassistant官方的手动安装文档,本文与官方文档一致:
1.1更改树莓派密码
$ passwd
如果不更换密码后续将会出错
1.2更新系统
$ sudo apt-get update
$ sudo apt-get upgrade -y
1.3查看自己树莓派python和python3版本
$ python -V
$ python3 -V
由于homeassistant官方文档要求设备必须安装python 3.7.3以上版本,如果版本不是python 3.7.3以上的用户请自行安装python 3.7.3 或重新烧录raspberry Pi官网上最新的Raspberry Pi OS系统
1.4安装依赖
$sudo apt-get install python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev autoconf build-essential libopenjp2-7
1.5为homeassistant创建一个名为homeassistant的账户
$sudo useradd -rm homeassistant -G dialout,gpio,i2c
由于此帐户仅用于运行Home Assistant Core,-rm
因此添加了额外的参数来创建系统帐户和创建主目录。该参数-G dialout,gpio,i2c
将用户添加到dialout
,gpio
和i2c
组。使用Z-Wave和Zigbee控制器需要第一个,而与Raspberry的GPIO通信则需要第二个。
1.6创建安装目录,并将此目录的所有者改为 homeassistant
$cd /srv
$sudo mkdir homeassistant
$sudo chown homeassistant:homeassistant homeassistant
1.7创建并进入到 homeassistant 虚拟环境
$sudo -u homeassistant -H -s
$cd /srv/homeassistant
$python3 -m venv .
$source bin/activate
1.8安装依赖的python包
$python3 -m pip install wheel
1.9安装HomeAssistant
$pip3 install homeassistant
1.10运行hass
$hass
此步骤可能比较慢,会自动安装缺少的配置文件,安装好后在浏览器中输入ip:8123即可
2. 设置homeassistant自动启动
2.1在终端命令行输入
$cd /etc/init.d/
$sudo nano hass-daemon
2.2之后将以下内容复制到hass-daemon中
#!/bin/sh
### BEGIN INIT INFO
# Provides: hass
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Home\ Assistant
### END INIT INFO
# /etc/init.d Service Script for Home Assistant
# Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh
PRE_EXEC="cd /srv/homeassistant; python3 -m venv .; source bin/activate;"
# Typically /usr/bin/hass
HASS_BIN="hass"
RUN_AS="homeassistant"
PID_DIR="/var/run/hass"
PID_FILE="$PID_DIR/hass.pid"
CONFIG_DIR="/var/opt/homeassistant"
LOG_DIR="/var/log/homeassistant"
LOG_FILE="$LOG_DIR/home-assistant.log"
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --log-file $LOG_FILE --daemon"
start() {
create_piddir
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
echo 'Service already running' >&2
return 1
fi
echo -n 'Starting service… ' >&2
local CMD="$PRE_EXEC $HASS_BIN $FLAGS"
su -s /bin/bash -c "$CMD" $RUN_AS
if [ $? -ne 0 ]; then
echo "Failed" >&2
else
echo 'Done' >&2
fi
}
stop() {
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
echo 'Service not running' >&2
return 1
fi
echo -n 'Stopping service… ' >&2
kill $(cat "$PID_FILE")
while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
rm -f $PID_FILE
echo 'Done' >&2
}
install() {
echo "Installing Home Assistant Daemon (hass-daemon)"
update-rc.d hass-daemon defaults
create_piddir
mkdir -p $CONFIG_DIR
chown $RUN_AS $CONFIG_DIR
mkdir -p $LOG_DIR
chown $RUN_AS $LOG_DIR
}
uninstall() {
echo "Are you really sure you want to uninstall this service? The INIT script will"
echo -n "also be deleted! That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
remove_piddir
echo "Notice: The config directory has not been removed"
echo $CONFIG_DIR
echo "Notice: The log directory has not been removed"
echo $LOG_DIR
update-rc.d -f hass-daemon remove
rm -fv "$0"
echo "Home Assistant Daemon has been removed. Home Assistant is still installed."
fi
}
create_piddir() {
if [ ! -d "$PID_DIR" ]; then
mkdir -p $PID_DIR
chown $RUN_AS "$PID_DIR"
fi
}
remove_piddir() {
if [ -d "$PID_DIR" ]; then
if [ -e "$PID_FILE" ]; then
rm -fv "$PID_FILE"
fi
rmdir -v "$PID_DIR"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
install)
install
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|install|uninstall}"
esac
2.3将文件设置为可执行
$sudo chmod +x /etc/init.d/hass-daemon
2.4注册自启动程序
$sudo service hass-daemon install
2.5重启系统
$sudo reboot
2.6在web端输入``树莓派IP:8123`即可访问homeassistant界面
3.Mosquitto安装
3.1更新一下系统,更新前记得更换pip和系统源
$sudo apt-get update
$sudo apt-get upgrade -y
3.2安装mosquitto和mosquitto client
$sudo apt-get mosquitto mosquitto-clients
3.3更改mosquitto设置
$sudo nano /etc/mosquitto/conf.d/mqtt.conf
写入
$allow_anonymous false
$password_file /etc/mosquitto/pwfile mqtt
3.4设置用户名和密码
$sudo mosquitto_passwd -c /etc/mosquitto/pwfile 你喜欢的名字
3.5重启系统
$sudo reboot
3.6配置homeassistant的mqtt
打开configuration.yaml,根据安装目录而定
$sudo nano /home/homeassistant/.homeassistant/configuration.yaml
在文件中输入
mqtt:
broker: 127.0.0.1
port: 1883
client_id: home-assistant-1
keepalive: 60
username: 自己设的
password: 自己设的
protocol: 3.1.1
discovery:true
discovery_prefix:homeassistant
4.米家蓝牙温度计通过树莓派接入MQTT并且被hass订阅
4.1安装bluez
sudo apt-get install bluetooth bluez blueman
sudo apt-get install python-bluez
4.2安装setuptools
sudo apt-get install python-setuptools
wget http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
4.3下载压缩包(在树莓派上进行)
https://bbs.hassbian.com/forum.php?mod=attachment&aid=MjA1MDV8NTBlZDM5MGJ8MTYwMTEzMjE1MXwzNzA4MHw5MDM3
解压缩
进入文件夹(执行 cd LYWSD03MMC_2MQTT
)
赋予可执行权限
chmod +x ./main.sh
chmod +x ./report.sh
写配置文件
sudo nano ./conf.cfg
写入
device_name=这里填设备名,自己写一个
device_MAC_Address=这里写小米温湿度计蓝牙MAC,格式近似AA:BB:CC:DD:EE:FF,米家app可查看
mqtt_host=这里写MQTT服务器地址,如果mqtt和homeassistant装在同一台机子上就写“localhost”(不含引号)
mqtt_username=这里写MQTT用户名,如果用的Homeassistant自带mqtt就写“homeassistant”(不含引号)
mqtt_passwd=这里写MQTT密码,如果用的Homeassistant自带mqtt就写你登录homeassistant用的密码
mqtt_port=这里写MQTT端口号,不知道就先写1883
执行 ./main.sh
效果如图所示