安装pip
sudo pip3 install -U pip
安装python虚拟环境
sudo pip3 install virtualenv
添加用户:
sudo useradd -rm homeassistant
sudo mkdir /srv/homeassistant
sudo chown homeassistant:homeassistant /srv/homeassistant
切换用户
sudo su -s /bin/bash homeassistant
创建虚拟运行环境并进入
virtualenv -p python3 /srv/homeassistant
source ./homeassistant/bin/activate
安装依赖
pip3 install netdisco
安装homeassistant
pip3 install -U homeassistant
如果安装中出现“the ssl module in Python is not available”错误,则需要安装libssl-dev和openssl依赖包,并重新编译安装python3(3.5.3以上)。配置编译时添加 --with-ssl参数。
安装homebridge:
安装curl
sudo apt-get install -y curl
安装nodejs
sudo apt-get update
sudo apt-get install -y nodejs
安装homebridge
sudo npm install -g --unsafe-perm homebridge
安装插件
sudo npm install -g homebridge-homeassistant
添加homebridge用户
sudo useradd -rm homebridge
sudo mkdir /var/opt/homebridge
sudo cp -R ~/.homebridge/* /var/opt/homebridge
sudo chown -R homebridge:homebridge /var/opt/homebridge
安装mqtt
sudo apt-get install -y mosquitto mosquitto-clients
(测试运行:开两个终端
订阅主题:mosquitto_sub -h localhost -t hello/world -u username -P password
发布主题:mosquitto_pub -h localhost -t hello/world -m "HELLO" -u username -P password
)
配置文件 /etc/mosquitto/mosquitto.conf
allow_anonymous允许匿名
password_file密码文件
acl_file访问控制列表
添加用户和密码
sudo chown mosquitto:mosquitto mosquitto.conf
sudo touch pwfile
sudo mosquitto_passwd -b pwfile "username" "password"
sudo chown mosquitto:mosquitto pwfile
sudo chmod u+rw pwfile
启动
sudo /etc/init.d/mosquitto start
配置文件:
修改/home/homeassistant/.homeassistant/configuration.yaml
添加
mqtt:
broker: xxx.xxx.xxx.xxx
port: 1883
username: username
password: password
修改/var/opt/homebridge/config.json
{
"bridge": {
"name": "Homebridge",
"username": "xx:xx:xx:xx:xx:xx",
"port": 51826,
"pin": "123-45-678"
},
"platforms": [
{
"platform": "HomeAssistant",
"name": "HomeAssistant",
"host": "http://xxx.xxx.xxx.xxx:8123",
"password": "",
"supported_types": ["automation", "binary_sensor", "climate", "cover", "device_tracker", "fan", "group", "input_boolean", "light", "lock", "media_player", "remote", "scene", "sensor", "switch"],
"default_visibility": "visible"
}
]
}
需要增加两个脚本去启动homeassistant和homebridge,即切换到对应用户环境下,然后启动服务即可。
启动两个服务后,使用iphone的home app扫描homebridge生成的二维码,在周围设备中也可以看到,直接添加:
至此软件环境搭好了。
三、硬件环境
1、首先,需要在homeassistant的配置文件中添加:
light:
platform: mqtt
name: 'Light'
state_topic: 'bedroom/rgb1/light/status'
command_topic: 'bedroom/rgb1/light/switch'
brightness_state_topic: 'bedroom/rgb1/brightness/status'
brightness_command_topic: 'bedroom/rgb1/brightness/set'
rgb_state_topic: 'bedroom/rgb1/rgb/status'
rgb_command_topic: 'bedroom/rgb1/rgb/set'
brightness_scale: 100
重启hass便可以看到概览界面多了一个灯,这里可以在同一局域网下的电脑在浏览器中访问设备ip:8123端口号访问