-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
199 lines (168 loc) · 5.53 KB
/
run.sh
File metadata and controls
199 lines (168 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
# Allow2 Home Assistant Add-on Startup Script
# This script copies the integration to Home Assistant and monitors health
set -e
# Configuration from environment
CONFIG_PATH="/data/options.json"
HA_CONFIG_DIR="/config"
CUSTOM_COMPONENTS_DIR="${HA_CONFIG_DIR}/custom_components"
ALLOW2_SOURCE_DIR="/app/custom_components/allow2"
ALLOW2_DEST_DIR="${CUSTOM_COMPONENTS_DIR}/allow2"
# Logging functions
log_info() {
echo "[INFO] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_warning() {
echo "[WARNING] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo "[ERROR] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Read add-on options
get_option() {
local key=$1
local default=$2
if [ -f "$CONFIG_PATH" ]; then
local value
value=$(jq -r ".$key // empty" "$CONFIG_PATH" 2>/dev/null)
if [ -n "$value" ] && [ "$value" != "null" ]; then
echo "$value"
return
fi
fi
echo "$default"
}
# Health check endpoint
start_health_server() {
log_info "Starting health check server on port 8099..."
while true; do
echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nOK" | nc -l -p 8099 -q 1 2>/dev/null || true
done &
HEALTH_PID=$!
log_info "Health server started with PID $HEALTH_PID"
}
# Copy integration files to Home Assistant
copy_integration() {
log_info "Copying Allow2 integration to Home Assistant..."
# Ensure custom_components directory exists
if [ ! -d "$CUSTOM_COMPONENTS_DIR" ]; then
log_info "Creating custom_components directory..."
mkdir -p "$CUSTOM_COMPONENTS_DIR"
fi
# Check if source exists
if [ ! -d "$ALLOW2_SOURCE_DIR" ]; then
log_error "Source integration not found at $ALLOW2_SOURCE_DIR"
return 1
fi
# Remove existing installation if present
if [ -d "$ALLOW2_DEST_DIR" ]; then
log_info "Removing existing Allow2 installation..."
rm -rf "$ALLOW2_DEST_DIR"
fi
# Copy the integration
log_info "Installing Allow2 to $ALLOW2_DEST_DIR..."
cp -r "$ALLOW2_SOURCE_DIR" "$ALLOW2_DEST_DIR"
# Remove pycache directories
find "$ALLOW2_DEST_DIR" -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
# Set permissions
chmod -R 755 "$ALLOW2_DEST_DIR"
# Verify installation
if [ -f "$ALLOW2_DEST_DIR/__init__.py" ] && [ -f "$ALLOW2_DEST_DIR/manifest.json" ]; then
log_info "Allow2 integration installed successfully!"
return 0
else
log_error "Integration installation verification failed"
return 1
fi
}
# Verify integration installation
verify_installation() {
log_info "Verifying Allow2 integration installation..."
local required_files=(
"__init__.py"
"manifest.json"
"config_flow.py"
"api.py"
"const.py"
"sensor.py"
"binary_sensor.py"
"strings.json"
)
local missing=0
for file in "${required_files[@]}"; do
if [ ! -f "$ALLOW2_DEST_DIR/$file" ]; then
log_warning "Missing file: $file"
missing=$((missing + 1))
fi
done
if [ $missing -gt 0 ]; then
log_warning "$missing required files missing"
return 1
fi
log_info "All required files present"
return 0
}
# Main startup sequence
main() {
log_info "=========================================="
log_info "Allow2 Home Assistant Add-on Starting"
log_info "=========================================="
# Get configuration options
LOG_LEVEL=$(get_option "log_level" "info")
UPDATE_INTERVAL=$(get_option "update_interval" "300")
COPY_ON_START=$(get_option "copy_on_start" "true")
log_info "Configuration:"
log_info " Log Level: $LOG_LEVEL"
log_info " Update Interval: ${UPDATE_INTERVAL}s"
log_info " Copy on Start: $COPY_ON_START"
# Start health check server
start_health_server
# Copy integration files if enabled
if [ "$COPY_ON_START" = "true" ]; then
if copy_integration; then
verify_installation
else
log_error "Failed to copy integration files"
fi
else
log_info "Skipping integration copy (copy_on_start disabled)"
if [ -d "$ALLOW2_DEST_DIR" ]; then
verify_installation
else
log_warning "Allow2 integration not found in Home Assistant"
log_warning "Enable copy_on_start or manually install the integration"
fi
fi
log_info "=========================================="
log_info "Allow2 Add-on Running"
log_info "Integration Location: $ALLOW2_DEST_DIR"
log_info "=========================================="
log_info ""
log_info "To use Allow2:"
log_info "1. Restart Home Assistant to load the integration"
log_info "2. Go to Settings > Devices & Services"
log_info "3. Click '+ Add Integration' and search for 'Allow2'"
log_info "4. Enter your Allow2 credentials to pair"
log_info ""
# Keep the container running
log_info "Monitoring Allow2 integration..."
while true; do
# Periodic health check
if [ ! -d "$ALLOW2_DEST_DIR" ]; then
log_warning "Allow2 integration directory missing, attempting reinstall..."
copy_integration || true
fi
sleep "$UPDATE_INTERVAL"
done
}
# Signal handlers
cleanup() {
log_info "Shutting down Allow2 add-on..."
if [ -n "$HEALTH_PID" ]; then
kill "$HEALTH_PID" 2>/dev/null || true
fi
exit 0
}
trap cleanup SIGTERM SIGINT SIGHUP
# Run main function
main "$@"