Debian Codegraph @

testing/e2e/homeassistant/setup.sh

raw
1 #!/bin/bash
2 #
3 # Home Assistant E2E Test Environment Setup Script
4 #
5 # This script starts a local Home Assistant server, completes onboarding,
6 # and writes SHOUTRRR_HOMEASSISTANT_URL for end-to-end tests.
7 #
8 # Usage:
9 # ./setup.sh [OPTIONS] [COMMAND]
10 #
11 # Commands:
12 # start-server Start Home Assistant using docker compose
13 # stop-server Stop Home Assistant
14 # status Check the status of Home Assistant
15 # setup-all Start the server, onboard, and write .env (default)
16 #
17 # Options:
18 # --help, -h Show this help message
19 # --verbose, -v Enable verbose output
20 #
21
22 set -euo pipefail
23
24 readonly RED='\033[0;31m'
25 readonly GREEN='\033[0;32m'
26 readonly YELLOW='\033[1;33m'
27 readonly BLUE='\033[0;34m'
28 readonly NC='\033[0m'
29
30 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
31 ENV_FILE="${SCRIPT_DIR}/.env"
32 COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yaml"
33 CONFIG_DIR="${SCRIPT_DIR}/config"
34
35 readonly HA_HOST="localhost:8123"
36 readonly HA_BASE="http://${HA_HOST}"
37 readonly HA_CLIENT_ID="https://example.com/app"
38 readonly HA_USERNAME="shoutrrr"
39 readonly HA_PASSWORD="shoutrrr-e2e-password"
40 readonly HA_NAME="Shoutrrr"
41 readonly CURL_CONNECT_TIMEOUT=5
42 readonly CURL_MAX_TIME=15
43
44 VERBOSE=false
45 COMPOSE_CMD=""
46
47 error() {
48 echo -e "${RED}[ERROR]${NC} $1" >&2
49 exit 1
50 }
51
52 info() {
53 echo -e "${GREEN}[INFO]${NC} $1"
54 }
55
56 warn() {
57 echo -e "${YELLOW}[WARN]${NC} $1"
58 }
59
60 debug() {
61 if [[ "$VERBOSE" == "true" ]]; then
62 echo -e "${BLUE}[DEBUG]${NC} $1"
63 fi
64 }
65
66 usage() {
67 echo "Home Assistant E2E Test Environment Setup Script"
68 echo ""
69 echo "Usage:"
70 echo " ./setup.sh [OPTIONS] [COMMAND]"
71 echo ""
72 echo "Commands:"
73 echo " start-server Start Home Assistant using docker compose"
74 echo " stop-server Stop Home Assistant"
75 echo " status Check the status of Home Assistant"
76 echo " setup-all Start the server, onboard, and write .env (default)"
77 echo ""
78 echo "Options:"
79 echo " --help, -h Show this help message"
80 echo " --verbose, -v Enable verbose output"
81 }
82
83 check_requirements() {
84 local missing_cmds=()
85
86 if ! command -v docker &> /dev/null; then
87 missing_cmds+=("docker")
88 fi
89
90 if docker compose version &> /dev/null 2>&1; then
91 COMPOSE_CMD="docker compose"
92 elif command -v docker-compose &> /dev/null; then
93 COMPOSE_CMD="docker-compose"
94 else
95 missing_cmds+=("docker-compose")
96 fi
97
98 if ! command -v curl &> /dev/null; then
99 missing_cmds+=("curl")
100 fi
101
102 if ! command -v python3 &> /dev/null; then
103 missing_cmds+=("python3")
104 fi
105
106 if [[ ${#missing_cmds[@]} -gt 0 ]]; then
107 error "Missing required commands: ${missing_cmds[*]}"
108 fi
109
110 debug "Using compose command: ${COMPOSE_CMD}"
111 }
112
113 wait_for_onboarding() {
114 local max_attempts="${1:-90}"
115 local attempt=1
116
117 info "Waiting for Home Assistant onboarding API at ${HA_BASE}..."
118
119 while [[ $attempt -le $max_attempts ]]; do
120 if curl -s --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" \
121 -o /dev/null -w "%{http_code}" "${HA_BASE}/api/onboarding" 2>/dev/null | grep -q "200"; then
122 info "Home Assistant onboarding API is ready"
123 return 0
124 fi
125
126 debug "Attempt ${attempt}/${max_attempts}: onboarding API not ready yet"
127 sleep 2
128 ((attempt++))
129 done
130
131 error "Home Assistant did not become ready after ${max_attempts} attempts"
132 }
133
134 json_field() {
135 local field="$1"
136 python3 -c "import json,sys; print(json.load(sys.stdin)[${field@Q}])"
137 }
138
139 urlencode() {
140 python3 -c "import sys,urllib.parse; print(urllib.parse.quote(sys.stdin.read().rstrip('\n'), safe=''))"
141 }
142
143 complete_onboarding() {
144 info "Creating Home Assistant user via onboarding API"
145
146 local user_json user_status
147 user_json="$(curl -sS --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" \
148 -w "\n%{http_code}" \
149 -X POST "${HA_BASE}/api/onboarding/users" \
150 -H "Content-Type: application/json" \
151 -d "{\"client_id\":\"${HA_CLIENT_ID}\",\"name\":\"${HA_NAME}\",\"username\":\"${HA_USERNAME}\",\"password\":\"${HA_PASSWORD}\",\"language\":\"en\"}")"
152 user_status="${user_json##*$'\n'}"
153 user_json="${user_json%$'\n'*}"
154 debug "onboarding/users HTTP ${user_status}"
155
156 local auth_code
157 auth_code="$(printf '%s' "${user_json}" | json_field "auth_code")" || error "Failed to parse onboarding auth_code"
158
159 local token_json token_status
160 token_json="$(curl -sS --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" \
161 -w "\n%{http_code}" \
162 -X POST "${HA_BASE}/auth/token" \
163 -H "Content-Type: application/x-www-form-urlencoded" \
164 --data-urlencode "grant_type=authorization_code" \
165 --data-urlencode "code=${auth_code}" \
166 --data-urlencode "client_id=${HA_CLIENT_ID}")"
167 token_status="${token_json##*$'\n'}"
168 token_json="${token_json%$'\n'*}"
169 debug "auth/token HTTP ${token_status}"
170
171 local access_token
172 access_token="$(printf '%s' "${token_json}" | json_field "access_token")" || error "Failed to parse access_token"
173
174 curl -sS --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" \
175 -o /dev/null -X POST "${HA_BASE}/api/onboarding/core_config" \
176 -H "Authorization: Bearer ${access_token}" \
177 -H "Content-Type: application/json" \
178 -d '{}' || warn "core_config onboarding step returned an error"
179
180 curl -sS --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" \
181 -o /dev/null -X POST "${HA_BASE}/api/onboarding/analytics" \
182 -H "Authorization: Bearer ${access_token}" \
183 -H "Content-Type: application/json" \
184 -d '{}' || warn "analytics onboarding step returned an error"
185
186 local encoded_token
187 encoded_token="$(printf '%s' "${access_token}" | urlencode)"
188
189 cat > "${ENV_FILE}" <<EOF
190 SHOUTRRR_HOMEASSISTANT_URL=homeassistant://${encoded_token}@localhost:8123/?disabletls=yes
191 EOF
192
193 info "Wrote ${ENV_FILE}"
194 }
195
196 start_server() {
197 info "Starting Home Assistant"
198
199 if [[ ! -f "$COMPOSE_FILE" ]]; then
200 error "Docker compose file not found at ${COMPOSE_FILE}"
201 fi
202
203 if [[ -z "$COMPOSE_CMD" ]]; then
204 check_requirements
205 fi
206
207 cd "$SCRIPT_DIR"
208
209 mkdir -p "${CONFIG_DIR}"
210
211 if $COMPOSE_CMD up -d; then
212 info "Home Assistant started"
213 info "Server is available at ${HA_BASE}"
214 else
215 error "Failed to start Home Assistant"
216 fi
217 }
218
219 stop_server() {
220 info "Stopping Home Assistant"
221
222 if [[ -z "$COMPOSE_CMD" ]]; then
223 check_requirements
224 fi
225
226 cd "$SCRIPT_DIR"
227
228 if $COMPOSE_CMD down; then
229 info "Home Assistant stopped"
230 else
231 error "Failed to stop Home Assistant"
232 fi
233 }
234
235 check_status() {
236 if [[ -z "$COMPOSE_CMD" ]]; then
237 check_requirements
238 fi
239
240 cd "$SCRIPT_DIR"
241
242 info "Checking Home Assistant status"
243 $COMPOSE_CMD ps
244 }
245
246 setup_all() {
247 info "Starting Home Assistant E2E test environment setup"
248 echo ""
249
250 check_requirements
251
252 echo ""
253 info "=== Step 1/3: Resetting config and starting Home Assistant ==="
254 cd "$SCRIPT_DIR"
255 $COMPOSE_CMD down -v >/dev/null 2>&1 || true
256 rm -rf "${CONFIG_DIR}"
257 mkdir -p "${CONFIG_DIR}"
258 start_server
259
260 echo ""
261 info "=== Step 2/3: Waiting for onboarding API ==="
262 wait_for_onboarding
263
264 echo ""
265 info "=== Step 3/3: Completing onboarding ==="
266 complete_onboarding
267
268 echo ""
269 info "=========================================="
270 info "Home Assistant E2E setup complete"
271 info "=========================================="
272 info ""
273 info "Run the E2E tests with:"
274 info " go test -v ./testing/e2e/homeassistant/..."
275 info ""
276 info "To stop the server, run:"
277 info " ${SCRIPT_DIR}/setup.sh stop-server"
278 }
279
280 main() {
281 local command="setup-all"
282
283 while [[ $# -gt 0 ]]; do
284 case "$1" in
285 --help|-h)
286 usage
287 exit 0
288 ;;
289 --verbose|-v)
290 VERBOSE=true
291 shift
292 ;;
293 start-server|stop-server|status|setup-all)
294 command="$1"
295 shift
296 ;;
297 *)
298 error "Unknown option or command: $1"
299 ;;
300 esac
301 done
302
303 case "$command" in
304 start-server)
305 check_requirements
306 start_server
307 ;;
308 stop-server)
309 check_requirements
310 stop_server
311 ;;
312 status)
313 check_requirements
314 check_status
315 ;;
316 setup-all)
317 setup_all
318 ;;
319 esac
320 }
321
322 main "$@"