-
Notifications
You must be signed in to change notification settings - Fork 3
/
smart_config.sh
206 lines (194 loc) · 7.92 KB
/
smart_config.sh
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
#!/bin/bash
# wget -O - https://raw.githubusercontent.com/jumping2000/ha-packages/main/smart_config.sh | bash
###########################################################
# #
# AUTO_DOWNLOAD #
# Copyright @ 2023 github.com/jumping2000 #
# All rights reserved #
# Do not copy, distribute, or modify without permission #
# #
###########################################################
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"
"$HOME/.homeassistant"
"/usr/share/hassio/homeassistant"
)
declare elettrodomestici=("Washing_Machine / Lavatrice" "Dryer / Asciugatrice" "Dishwasher / Lavastoviglie" "Oven / Forno")
declare temp_dir="temp_dir"
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
}
function copy_folder() {
local source_path="$1"
local dest_path="$2"
local file_name=$(basename "$source_path")
# Se il file o la cartella di destinazione esiste, chiedi all'utente se vuole sovrascriverlo
if [ -e "$dest_path/$file_name" ]; then
while true; do
read -p "Vuoi sovrascrivere i file del pack Elettrodomestici contenuti in $file_name? (Sì/No): " overwrite
case $overwrite in
[Ss]* ) cp -rf "$source_path" "$dest_path"; break;;
[Nn]* ) return;;
* ) warn "Per favore, rispondi Sì o No.";;
esac
done
fi
cp -rn "$source_path" "$dest_path"
}
function replace_string() {
local stringa_old="$1"
local stringa_new="$2"
local file="$3"
if [ ! -e "$file" ]; then
warn "Il file $file non esiste."
return 1
fi
sed -i "s/$stringa_old/$stringa_new/g" "$file"
info "Sostituzione dei tag completata nel file $file."
}
function rename_files() {
local appliance="$1"
local dir="$2"
local output_file
# Loop through files in the directory and copy if search_string is found
for file in "$dir"/*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
if [[ "$filename" == *keys* ]] && [[ "$filename" == *"$appliance"* ]]; then
cp "$file" "$dir/keys.txt"
info "Copied '$filename' to 'keys.txt' - Ready to use file"
elif [[ "$filename" != *keys* ]] && [[ "$filename" == *"$appliance"* ]]; then
output_file="${filename%.ya__}.yaml"
cp "$file" "$dir/$output_file"
info "Copied '$file' to '$output_file' - Ready to use file"
fi
fi
done
}
checkRequirement "wget"
checkRequirement "unzip"
checkRequirement "sed"
checkRequirement "awk"
checkRequirement "arch"
echo "Automatic download and configuration of packages and cards v1.2 - Copyright @ 2023 github.com/jumping2000. All rights reserved"
# Verifica che sia stato fornito un argomento
if [ $# -eq 0 ]; then
echo "Utilizzo: $0 [download] oppure $0 [config]"
exit 1
fi
# Leggi l'argomento dalla linea di comando
option="$1"
case "$option" in
"download")
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 "$temp_dir" ]; then
warn "Remove prevoious temporary dir: $temp_dir"
rm -rf "$temp_dir"
fi
if [ ! -d "$temp_dir" ]; then
mkdir -p "$temp_dir"
fi
###
while true; do
read -p "Do you want to download Elettrodomestici Smart 2023? / Vuoi scaricare il pacchetto Elettrodomestici Smart 2023 (Sì/No): " download_pack
case $download_pack in
[Ss]* )
info "Downloading Elettrodomestici Smart 2023"
wget "https://github.com/jumping2000/ha-packages/releases/latest/download/elettrodomestici_2023.zip"
info "Unpacking Elettrodomestici Smart 2023..."
unzip "$haPath/elettrodomestici_2023.zip" -d "$temp_dir" >/dev/null 2>&1
# Copia i file e le cartelle dalla directory temporanea alla directory di destinazione
for item in "$temp_dir"/*; do
if [ -d "$item" ]; then
copy_folder "$item" "$haPath"
elif [ -f "$item" ]; then
copy_folder "$item" "$haPath"
fi
done
echo
info "Removing Elettrodomestici Smart 2023 old zip file..."
rm -f "$haPath/elettrodomestici_2023.zip"
rm -rf "$temp_dir"
info "Download files complete / Copia file completata."
break
;;
[Nn]* )
info "Non effettuo il download"
break
;;
* )
warn "Per favore, rispondi Sì o No."
;;
esac
done
echo "Choose a package: / Scegli un package da installare dalla lista:"
select choice in "${elettrodomestici[@]}"; do
case "$REPLY" in
1|2|3|4)
selected_appliance="$choice"
break
;;
*)
warn "Scelta non valida. Seleziona le opzioni da 1 a 4."
;;
esac
done
info "Hai scelto: $selected_appliance"
# Scegli sensore energia
read -p "[REQUIRED] Enter energy sensor for the chosen appliance / [OBBLIGATORIO] Inserisci il sensore di energia (kWh) per l'elettrodomestico scelto: " energy_sensor
info "Il sensore inserito è: $energy_sensor"
result=$(echo "$selected_appliance" | awk '{print $1}' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')
echo
#rinomino le keys
rename_files "$result" "$haPath/packages"
#rinomino il package di interesse"
rename_files "$result" "$haPath/packages/elettrodomestici"
replace_string "TAG_02" "$energy_sensor" "$haPath/packages/elettrodomestici/$result.yaml"
info "Now you can restart Home Assistant / Prima parte della configurazione finita, riavvia Home Assistant e continua con la configurazione"
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
;;
"config")
if [[ $(arch) == "aarch"* ]]; then
info "Running on a Raspberry Pi"
else
info "Running on an x86-based machine."
fi
chmod +x /config/packages/autoconfig/auto_config.py
python /config/packages/autoconfig/auto_config.py -v
;;
*)
echo "Opzione non valida"
exit 1
;;
esac