0
0
Files
puxiaohu-smarthome-release/install-mac.sh

208 lines
6.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ============================================================
# 朴小护智慧家居 v1.0.0 — Mac 一键安装脚本
# ============================================================
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
INSTALL_DIR="$HOME/.puxiaohu-smarthome"
HA_SRC="$INSTALL_DIR/ha-core"
HERMES_SRC="$INSTALL_DIR/hermes-prod"
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} 朴小护智慧家居 v1.0.0 — Mac 安装器${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
# ---- 1. 检查系统 ----
echo -e "${YELLOW}[1/6] 检查系统环境...${NC}"
if [[ "$(uname)" != "Darwin" ]]; then
echo -e "${RED}错误: 此安装器仅支持 macOS${NC}"
exit 1
fi
ARCH=$(uname -m)
echo " 架构: $ARCH"
# 检查 Homebrew
if ! command -v brew &>/dev/null; then
echo -e "${YELLOW} 安装 Homebrew...${NC}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# ---- 2. 安装运行时 ----
echo -e "${YELLOW}[2/6] 安装运行时依赖...${NC}"
# Python 3.14
if ! python3.14 --version &>/dev/null; then
echo " 安装 Python 3.14..."
brew install python@3.14
fi
echo " Python: $(python3.14 --version)"
# Node.js 20+
if ! command -v node &>/dev/null || [[ $(node -v | cut -d. -f1 | tr -d 'v') -lt 20 ]]; then
echo " 安装 Node.js 22..."
brew install node@22
fi
echo " Node.js: $(node --version)"
echo " npm: $(npm --version)"
# ---- 3. 创建目录结构 ----
echo -e "${YELLOW}[3/6] 创建安装目录...${NC}"
mkdir -p "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR/config/custom_components"
mkdir -p "$INSTALL_DIR/config/www"
# 清理旧的 storage确保全新安装无残留
rm -rf "$INSTALL_DIR/config/.storage"
echo " 已清理旧凭据"
# 写最小配置
cat > "$INSTALL_DIR/config/configuration.yaml" << 'YEOF'
homeassistant:
name: 朴小护智慧家居
frontend:
http:
server_port: 8123
YEOF
# ---- 4. 安装 HomeAssistant (精简版) ----
echo -e "${YELLOW}[4/6] 安装 HomeAssistant (朴小护精简版)...${NC}"
# 解压源码
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
tar xzf "$SCRIPT_DIR/puxiaohu-homeassistant-1.0.0.tar.gz" -C "$INSTALL_DIR/"
mv "$INSTALL_DIR/homeassistant" "$INSTALL_DIR/ha-core/" 2>/dev/null || true
# tar may have created ha-core structure already
if [ ! -d "$HA_SRC" ]; then
# tar extracted to current dir
mv "$INSTALL_DIR/pyproject.toml" "$INSTALL_DIR/ha-core/" 2>/dev/null || true
fi
# 创建 venv
python3.14 -m venv "$INSTALL_DIR/venv"
source "$INSTALL_DIR/venv/bin/activate"
# 安装 pip 依赖 (清华镜像, 国内快)
echo " 从清华镜像安装依赖..."
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn -r "$SCRIPT_DIR/deps.txt"
# 安装 HA
echo " 安装朴小护 HomeAssistant..."
cd "$HA_SRC" 2>/dev/null || { mkdir -p "$HA_SRC" && tar xzf "$SCRIPT_DIR/puxiaohu-homeassistant-1.0.0.tar.gz" -C "$INSTALL_DIR/"; cd "$INSTALL_DIR/"; }
pip install -e . 2>/dev/null || pip install --no-build-isolation .
# 创建默认配置
cat > "$INSTALL_DIR/config/configuration.yaml" << 'YAMLEOF'
# 朴小护智慧家居 — 精简配置
homeassistant:
name: 朴小护智慧家居
auth_providers:
- type: trusted_networks
trusted_networks:
- 127.0.0.1
- 192.168.0.0/24
allow_bypass_login: true
default_config:
http:
server_port: 8123
logger:
default: warning
recorder:
db_url: sqlite:///config/home-assistant_v2.db
# 小米设备 (安装后自动通过 MIoT 云发现)
# xiaomi_miio: 通过 config_entries API 添加
# TTS
tts:
- platform: edge_tts
language: zh-cn
YAMLEOF
deactivate
# ---- 5. 安装 Hermes Agent (精简版) ----
echo -e "${YELLOW}[5/6] 安装 Hermes Agent...${NC}"
RELEASE_URL="https://git.pwhealth100.com/xiaoou/puxiaohu-smarthome-release/raw/branch/master"
# 下载 Hermes 包(如果本地没有)
if [ -f "$SCRIPT_DIR/hermes-prod.tar.gz" ]; then
tar xzf "$SCRIPT_DIR/hermes-prod.tar.gz" -C "$INSTALL_DIR/"
else
echo " 从 release 仓库下载 Hermes..."
curl -sL -o /tmp/hermes-prod.tar.gz "$RELEASE_URL/hermes-prod.tar.gz"
tar xzf /tmp/hermes-prod.tar.gz -C "$INSTALL_DIR/"
rm /tmp/hermes-prod.tar.gz
fi
# Hermes Python 依赖
source "$INSTALL_DIR/venv/bin/activate"
pip install -r "$HERMES_SRC/pyproject.toml" 2>/dev/null || true
deactivate
# Hermes Node.js 依赖 (仅 production, 无 workspace)
cd "$HERMES_SRC"
npm install --production --workspaces=false 2>&1 | tail -3
echo -e "${GREEN} Hermes 安装完成${NC}"
# ---- 6. 创建启动服务 ----
echo -e "${YELLOW}[6/6] 创建启动服务...${NC}"
# HA launchd plist
cat > ~/Library/LaunchAgents/com.puxiaohu.homeassistant.plist << PLISTEOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.puxiaohu.homeassistant</string>
<key>ProgramArguments</key>
<array>
<string>$INSTALL_DIR/venv/bin/hass</string>
<string>-c</string>
<string>$INSTALL_DIR/config</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$INSTALL_DIR/logs/ha.log</string>
<key>StandardErrorPath</key>
<string>$INSTALL_DIR/logs/ha-error.log</string>
</dict>
</plist>
PLISTEOF
mkdir -p "$INSTALL_DIR/logs"
launchctl load ~/Library/LaunchAgents/com.puxiaohu.homeassistant.plist 2>/dev/null || true
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} 安装完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo " 🏠 HomeAssistant: http://localhost:8123"
echo " 📁 配置目录: $INSTALL_DIR/config"
echo " 📁 www 目录: $INSTALL_DIR/config/www (放自定义前端)"
echo " 📝 日志: $INSTALL_DIR/logs/"
echo ""
echo " 下一步:"
echo " 1. 在浏览器打开 http://localhost:8123"
echo " 2. 将朴小护前端放入 $INSTALL_DIR/config/www/"
echo " 3. 启动 Hermes: cd $HERMES_SRC && python run_agent.py"
echo ""