forked from BrainicHQ/tado-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·267 lines (230 loc) · 8.54 KB
/
install.sh
File metadata and controls
executable file
·267 lines (230 loc) · 8.54 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
echo "WARNING: This script requires root privileges. Please review the script before proceeding."
# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "Please run this script as root or with sudo privileges."
exit 1
fi
# 1. Install Dependencies
install_dependencies() {
echo "Installing dependencies..."
# Initialize the variables
NEED_CURL=0
NEED_JQ=0
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Detecting the distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO=$ID
fi
# Check if curl and jq are installed
if ! command -v curl &> /dev/null; then
NEED_CURL=1
fi
if ! command -v jq &> /dev/null; then
NEED_JQ=1
fi
case $DISTRO in
debian|ubuntu|raspbian)
if [[ $NEED_CURL -eq 1 ]] || [[ $NEED_JQ -eq 1 ]]; then
sudo apt-get update
fi
[[ $NEED_CURL -eq 1 ]] && sudo apt-get install -y curl
[[ $NEED_JQ -eq 1 ]] && sudo apt-get install -y jq
;;
fedora|centos|rhel)
[[ $NEED_CURL ]] && sudo yum install -y curl
[[ $NEED_JQ ]] && sudo yum install -y jq
;;
arch|manjaro)
[[ $NEED_CURL || $NEED_JQ ]] && sudo pacman -Sy
[[ $NEED_CURL ]] && sudo pacman -S curl
[[ $NEED_JQ ]] && sudo pacman -S jq
;;
suse|opensuse*)
[[ $NEED_CURL ]] && sudo zypper install curl
[[ $NEED_JQ ]] && sudo zypper install jq
;;
*)
echo "Unsupported Linux distribution"
exit 1
;;
esac
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
if ! command -v brew &> /dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
if ! command -v curl &> /dev/null; then
brew install curl
fi
if ! command -v jq &> /dev/null; then
brew install jq
fi
else
echo "Unsupported OS"
exit 1
fi
}
# 2. Set Environment Variables
set_env_variables() {
echo "Setting up environment variables for multiple Tado accounts..."
# Prompt for the number of accounts
read -rp "Enter the number of Tado accounts: " NUM_ACCOUNTS
# Initialize the env file with NUM_ACCOUNTS
echo "export NUM_ACCOUNTS=$NUM_ACCOUNTS" > /etc/tado-assistant.env
# Loop through each account for configuration
i=1
while [ "$i" -le "$NUM_ACCOUNTS" ]; do
echo "Configuring account $i..."
read -rp "Enter TADO_USERNAME for account $i: " TADO_USERNAME
read -rp "Enter TADO_PASSWORD for account $i: " TADO_PASSWORD
read -rp "Enter CHECKING_INTERVAL for account $i (default: 15): " CHECKING_INTERVAL
read -rp "Enter MAX_OPEN_WINDOW_DURATION for account $i (in seconds): " MAX_OPEN_WINDOW_DURATION
read -rp "Enable log for account $i? (true/false, default: false): " ENABLE_LOG
read -rp "Enter log file path for account $i (default: /var/log/tado-assistant.log): " LOG_FILE
# Validate credentials
if validate_credentials "$TADO_USERNAME" "$TADO_PASSWORD"; then
# Append the settings for each account to the env file
{
echo "export TADO_USERNAME_$i=$TADO_USERNAME"
echo "export TADO_PASSWORD_$i=$TADO_PASSWORD"
echo "export CHECKING_INTERVAL_$i=${CHECKING_INTERVAL:-15}"
echo "export MAX_OPEN_WINDOW_DURATION_$i=${MAX_OPEN_WINDOW_DURATION:-}"
echo "export ENABLE_LOG_$i=${ENABLE_LOG:-false}"
echo "export LOG_FILE_$i=${LOG_FILE:-/var/log/tado-assistant.log}"
} >> /etc/tado-assistant.env
i=$((i+1)) # Move to next account only if validation succeeds
else
echo "Validation failed for account $i. Please re-enter the details."
fi
done
chmod 644 /etc/tado-assistant.env
}
# 3. Set up as Service
setup_service() {
echo "Setting up the service..."
SCRIPT_PATH="/usr/local/bin/tado-assistant.sh"
cp "$(dirname "$0")/tado-assistant.sh" "$SCRIPT_PATH"
chmod +x $SCRIPT_PATH
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
SERVICE_CONTENT="[Unit]
Description=Tado Assistant Service
[Service]
ExecStart=$SCRIPT_PATH
User=$(whoami)
Restart=always
[Install]
WantedBy=multi-user.target"
echo "$SERVICE_CONTENT" | sudo tee /etc/systemd/system/tado-assistant.service > /dev/null
sudo systemctl enable tado-assistant.service
sudo systemctl restart tado-assistant.service
elif [[ "$OSTYPE" == "darwin"* ]]; then
LAUNCHD_CONTENT="<?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.user.tadoassistant</string>
<key>ProgramArguments</key>
<array>
<string>$SCRIPT_PATH</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>ThrottleInterval</key>
<integer>10</integer>
</dict>
</plist>"
echo "$LAUNCHD_CONTENT" > ~/Library/LaunchAgents/com.user.tadoassistant.plist
launchctl load ~/Library/LaunchAgents/com.user.tadoassistant.plist
fi
}
# 4. Validate credentials
validate_credentials() {
local username=$1
local password=$2
local response
local error_message
if ! response=$(curl -s -X POST "https://auth.tado.com/oauth/token" \
-d "client_id=public-api-preview" \
-d "client_secret=4HJGRffVR8xb3XdEUQpjgZ1VplJi6Xgw" \
-d "grant_type=password" \
--data-urlencode "password=${password}" \
-d "scope=home.user" \
--data-urlencode "username=${username}"); then
echo "Error connecting to the API."
return 1
fi
TOKEN=$(echo "$response" | jq -r '.access_token')
error_message=$(echo "$response" | jq -r '.error_description // empty')
if [ "$TOKEN" == "null" ] || [ -z "$TOKEN" ]; then
echo "Login error for user $username. ${error_message:-Check the username/password!}"
return 1
fi
return 0
}
# 5. Update the script
update_script() {
echo "Checking for updates..."
# Navigate to the directory of the script
cd "$(dirname "$0")" || exit
local force_update=0
if [[ "$1" == "--force" ]]; then
force_update=1
fi
if [[ $force_update -eq 1 ]]; then
echo "Force updating. Discarding any local changes..."
git reset --hard
git clean -fd
else
# Stash any local changes to avoid conflicts
git stash --include-untracked
fi
# Pull the latest changes from the remote repository
git pull --ff-only || {
echo "Error: Update failed. Trying to resolve..."
# In case of failure, try a hard reset to the latest remote commit
git fetch origin
if ! git reset --hard origin/"$(git rev-parse --abbrev-ref HEAD)"; then
echo "Error: Update failed and automatic resolution failed."
exit 1
fi
}
if [[ $force_update -eq 0 ]]; then
# Reapply stashed changes, if any
git stash pop
fi
echo "Script updated successfully!"
# Recheck dependencies
install_dependencies
# Replace the service script with the updated version
echo "Updating the script used by the service..."
cp tado-assistant.sh /usr/local/bin/tado-assistant.sh
chmod +x /usr/local/bin/tado-assistant.sh
# Restart the service based on the OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "Starting the Tado Assistant service on Linux..."
sudo systemctl start tado-assistant.service
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Starting the Tado Assistant service on macOS..."
launchctl unload ~/Library/LaunchAgents/com.user.tadoassistant.plist
launchctl load ~/Library/LaunchAgents/com.user.tadoassistant.plist
else
echo "Unsupported OS for service management."
exit 1
fi
}
# Check if the script is run with the --update or --force-update flag
if [[ "$1" == "--update" ]] || [[ "$1" == "--force-update" ]]; then
# Call the update function with or without the force option
update_script "$1"
exit 0
fi
install_dependencies
set_env_variables
validate_credentials
setup_service
echo "Tado Assistant has been successfully installed and started!"