前期准备
我购买的是腾讯云的轻量云服务器,上面自己装了国产运维面板 1Panel,docker 环境都配置好了。
安装HomeAssistant
可以直接在 1Panel 的应用商店中找到 Home Assistant。
安装需要一段时间,安装完之后需要在防火墙打开8123端口。
然后在浏览器中输入你的公网IP:8123就可以进入 HomeAssistant ,注册完之后,就可以关闭了。
安装 Hacs
按照上图进入 HomeAssistant 的容器内部(前面忘加 sudo 了)
sudo docker exec -it homeassistant bash #类似这样的命令
进入之后输入以下命令安装:
wget -O - https://get.hacs.xyz | bash -
安装命令可能变动,具体参考官方文档:https://hacs.xyz/docs/setup/download
以上命令可能需要代理,但容器内部无法访问到外部的代理。刚好 HA 的安装需要 Host 模式,可以直接使用宿主机的网络,所以可以先在容器外部创建好代理,然后再去执行命令。
在宿主机上运行
ip addr show
或ifconfig
,查找 Docker 网络接口(通常是名为docker0
的接口)的 IP 地址。使用这个 IP 地址替换
127.0.0.1
设置代理,例如export HTTP_PROXY="http://docker0-ip-address:7890"
。
# 设置代理示例
export http_proxy="http://docker0-ip-address:7890"
export https_proxy="http://docker0-ip-address:7890"
# 取消代理
unset http_proxy
unset https_proxy
但这样尝试之后并没有设置代理,发现可以 ping 通宿主机,但无法 curl 到主机的服务,十分奇怪。
折腾半天后换了一个办法,直接修改 docker-compose ,为容器设置代理。然后发现还是不行,有可能是 1Panel 的问题。
又一个解决方法
直接访问 wget 中的网址:https://get.hacs.xyz,把脚本的内容复制下来
#!/bin/bash
# wget -O - https://get.hacs.xyz | bash -
set -e
RED_COLOR='\033[0;31m'
GREEN_COLOR='\033[0;32m'
GREEN_YELLOW='\033[1;33m'
NO_COLOR='\033[0m'
declare haPath
declare -a paths=(
"$PWD"
"$PWD/config"
"/config"
"/homeassistant"
"$HOME/.homeassistant"
"/usr/share/hassio/homeassistant"
)
declare currentVersion
declare currentYear
declare currentMonth
declare currentPatch
declare targetVersion
declare targetYear
declare targetMonth
declare targetPatch
function info () { echo -e "${GREEN_COLOR}INFO: $1${NO_COLOR}";}
function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}";}
function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; }
function checkRequirement () {
if [ -z "$(command -v "$1")" ]; then
error "'$1' is not installed"
fi
}
checkRequirement "wget"
checkRequirement "unzip"
info "Trying to find the correct directory..."
for path in "${paths[@]}"; do
if [ -n "$haPath" ]; then
break
fi
if [ -f "$path/.HA_VERSION" ]; then
haPath="$path"
fi
done
if [ -n "$haPath" ]; then
info "Found Home Assistant configuration directory at '$haPath'"
cd "$haPath" || error "Could not change path to $haPath"
if [ ! -d "$haPath/custom_components" ]; then
info "Creating custom_components directory..."
mkdir "$haPath/custom_components"
fi
info "Changing to the custom_components directory..."
cd "$haPath/custom_components" || error "Could not change path to $haPath/custom_components"
info "Downloading HACS"
rm -f "$haPath/custom_components/hacs.zip"
wget "https://github.com/hacs/integration/releases/latest/download/hacs.zip"
if [ -d "$haPath/custom_components/hacs" ]; then
warn "HACS directory already exist, cleaning up..."
rm -R "$haPath/custom_components/hacs"
fi
info "Creating HACS directory..."
mkdir "$haPath/custom_components/hacs"
info "Unpacking HACS..."
unzip "$haPath/custom_components/hacs.zip" -d "$haPath/custom_components/hacs" >/dev/null 2>&1
echo
info "Verifying versions"
targetVersion=$(sed -n -e '/^MINIMUM_HA_VERSION/p' "$haPath/custom_components/hacs/const.py" | cut -d '"' -f 2)
currentVersion=$(cat "$haPath/.HA_VERSION")
info "Current version is ${currentVersion}, minimum version is ${targetVersion}"
targetYear=$(echo "${targetVersion}" | cut -d "." -f 1)
currentYear=$(echo "${currentVersion}" | cut -d "." -f 1)
if [ "${currentVersion}" == "2023.12.0" ]; then
rm -R "$haPath/custom_components/hacs"
rm -f "$haPath/custom_components/hacs.zip"
error "HACS will not work on version 2023.12.0 of Home Assistant, upgrade to 2023.12.1 (or newer) before re-running this script."
fi
if [ "${currentYear}" -lt "${targetYear}" ]; then
rm -R "$haPath/custom_components/hacs"
rm -f "$haPath/custom_components/hacs.zip"
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}"
fi
if [ "${currentYear}" == "${targetYear}" ]; then
targetMonth=$(echo "${targetVersion}" | cut -d "." -f 2)
currentMonth=$(echo "${currentVersion}" | cut -d "." -f 2)
if [ "${currentMonth}" -lt "${targetMonth}" ]; then
rm -R "$haPath/custom_components/hacs"
rm -f "$haPath/custom_components/hacs.zip"
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}"
fi
if [ "${currentMonth}" == "${targetMonth}" ]; then
targetPatch=$(echo "${targetVersion}" | cut -d "." -f 3)
currentPatch=$(echo "${currentVersion}" | cut -d "." -f 3)
if [ "${currentPatch}" -lt "${targetPatch}" ]; then
rm -R "$haPath/custom_components/hacs"
rm -f "$haPath/custom_components/hacs.zip"
error "Version ${currentVersion} is not new enough, needs at least ${targetVersion}"
fi
fi
fi
echo
info "Removing HACS zip file..."
rm -f "$haPath/custom_components/hacs.zip"
info "Installation complete."
echo
info "Remember to restart Home Assistant before you configure it"
else
echo
error "Could not find the directory for Home Assistant" false
echo "Manually change the directory to the root of your Home Assistant configuration"
echo "With the user that is running Home Assistant"
echo "and run the script again"
exit 1
fi
然后在容器内:
vi install_hacs.sh
把脚本复制进去,再
bash install_hacs.sh
下载速度有点缓慢,但至少进度条动了
等了一会到了83%又给我停了。。看来这还是一个玄学问题。
后来连接 github 还是失败了。
最终解决方案
直接在 https://github.com/hacs/integration/releases/ 中下好 zip 文件。
然后点这个进入容器安装目录:
再然后 /data/custom_components ,把 zip 解压复制到该文件夹中。
在 Home Assistant 中添加集成
搜索之后发现什么都没有,经过上网查询得知还需要修改 manifest 。但是是21年的老博客了,我这里没有 manifest 文件。
哦草,我忘记重启 HA 服务了。重启之后就有了。
但是的但是,他给我报错了。。
为什么会报错呢,官方脚本的逻辑也就是简单地下好了 zip 包,然后放到自定义组件的文件夹中。我打算重新弄一个容器。
重复操作,记得打开高级模式。然后成功了。
但是这次又有另一个报错信息:
太逆天了,最开始不能用命令一键安装就是因为网络的问题,现在手动给它装好了,还是遇到了网络问题。
看来还是得折腾一个 docker 容器内的代理。又试了一下,成了。也不知道是运气好突然可以连上 github 还是因为配置好了代理。
开始配置Hacs
上来就当头一棒。但在这个 issue 中找到了解决办法:https://github.com/al-one/hass-xiaomi-miot/issues/1248
找到里面提到的博客:https://www.ququ123.top/2023/09/homeassistant_xiaomi_login/
根据它的原理,首先修改云服务器的 ssh 配置文件,在 /etc/ssh/sshd_config
,把AllowTcpForwarding yes
前面的#
删除,保存并重启服务:systemctl reload sshd
然后终端中输入 ssh -ND 3600 username@your-remote-server.com
,注意这个时候终端输入密码(如果要输入密码)之后没有反应,这是正常的。
然后配置浏览器,我使用的是 chrome ,但它没有原生的代理功能,所以需要一个插件:Proxy SwitchyOmega
之后这样设置,然后在浏览器中启用
之后再去 HA 中进行登录,然后点击链接验证,还是无果。这时候去登录 mi.com ,再回来登录 HA 。就成功了。
集成米家设备到Homekit
先安装 Homekit :
安装完之后,选择区域,然后在通知栏中用你的 iPhone 扫描二维码,进入家庭应用,就实现了把米家家居导入到 Homekit 中了!
来来回回折腾了几个小时,总算是成了。
坑
查看 hacs 官方的安装脚本,发现一个坑:这里似乎会检查版本,而我发现 1Panel 的 HA 版本是2023.8,比较老了。
看来即使是有网络代理,也无法使用官方的脚本运行。