Skip to content

Commit 73ea87c

Browse files
authored
fix ESPHome install script (#2059)
* dynamic Config Path + Menü update Signed-off-by: Patrick Götz <goetz.patrick@gmx.de>
1 parent 2bebf97 commit 73ea87c

6 files changed

Lines changed: 228 additions & 109 deletions

File tree

docs/openhabian.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ Each of these are included as a part of `openhabian-config` menu option 20.
254254
- [OWServer](https://owfs.org) - 1-Wire system of Dallas/Maxim
255255
- [FIND](https://www.internalpositioning.com/) - Framework for Internal Navigation and Discovery
256256
- Mi Flora MQTT daemon
257+
- [ESPHome Device Builder](https://esphome.io/guides/getting_started_hassio.html#device-builder-interface) - Webinterface for easy configuration, installation and updating of ESPHome devices at [http://openhabian:6052](http://openhabian:6052) <br/>
258+
The ESPHome configuration files are secured by the openHABbian backup function. <br/>
259+
**Please note that automatic updates are not implemented in OpenHABian. <br/> &rarr; to do this, use the openhbaian-config update function in menu 20**
257260
- Grott Proxy server for [Growatt binding](https://www.openhab.org/addons/bindings/growatt/#grott-application-installation-and-setup)
258261

259262
## First boot configuration

functions/menu.bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ show_main_menu() {
131131
"2D | Install EVCC" "Deploy Electric Vehicle Charge Controller" \
132132
" | Remove EVCC" "Uninstall EVCC" \
133133
" | Setup EVCC" "Setup EVCC from command line (German only)" \
134-
"2E | Install ESPHome dashboard" "Deploy ESPHome dashboard" \
135-
" | Remove ESPHome dashboard" "Uninstall ESPHome dashboard" \
134+
"2E | Install ESPHome " "Deploy / Update ESPHome Device Builder" \
135+
" | Remove ESPHome " "Uninstall ESPHome Device Builder" \
136136
"2F | Install Grott" "Install Grott Proxy server (for Growatt binding)" \
137137
" | Remove Grott" "Uninstall Grott Proxy server (for Growatt binding)" \
138138
3>&1 1>&2 2>&3)
@@ -160,8 +160,8 @@ show_main_menu() {
160160
2D\ *) install_evcc "install";;
161161
*Remove\ EVCC*) install_evcc "remove";;
162162
*Setup\ EVCC*) setup_evcc;;
163-
2E\ *) install_esphomedashboard "install";;
164-
*Remove\ ESPHome\ dashboard*) install_esphomedashboard "remove";;
163+
2E\ *) setup_esphome_device_builder "install";;
164+
*Remove\ ESPHome*) setup_esphome_device_builder "remove";;
165165
2F\ *) install_grott "install";;
166166
*Remove\ Grott*) install_grott "remove";;
167167
"") return 0 ;;

functions/packages.bash

Lines changed: 170 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -799,128 +799,206 @@ setup_evcc() {
799799
if cond_redirect systemctl restart evcc.service; then echo "OK"; else echo "FAILED"; fi
800800
}
801801

802-
## Function for (un)installing ESPhome dashboard
803-
## The function must be invoked UNATTENDED.
802+
## Function for (un)installing / update ESPHome Device Builder
803+
## The function can be invoked INTERACTIVE only.
804804
## Valid arguments: "install" or "remove"
805805
##
806-
## install_esphomedashboard(String action)
807-
##
808-
##
809-
install_esphomedashboard() {
806+
## setup_esphome_device_builder(String install|remove)
807+
##
808+
809+
setup_esphome_device_builder() {
810+
811+
# Variables
812+
local esphomeDir="/opt/esphome_device_builder"
813+
local esphomeConfigDir="/etc/openhab/ESPHome"
814+
local serviceTemplate="${BASEDIR:-/opt/openhabian}/includes/esphome-device-builder.service.template"
815+
local setupMode="$1"
810816
local port=6052
811-
local installText="This will install ESPhome dashboard\nUse the web interface on port $port to access ESPhome dashboard web interface."
812-
local removeText="This will remove ESPhome dashboard"
813-
814-
ESPHOME_DIR="/opt/esphomedashboard"
815-
SERVICE_TEMPLATE="${BASEDIR:-/opt/openhabian}/includes/esphome-dashboard.service.template" # Update with the actual path
816817

817-
if [[ $1 == "remove" ]]; then
818-
if [[ -n $INTERACTIVE ]]; then
819-
whiptail --title "ESPhome dashboard removal" --msgbox "$removeText" 7 80
818+
# Whiptail / Console messages
819+
local whiptailTitle="ESPHome Device Builder - Setup"
820+
local installStartText="No ESPHome Device Builder service detectet --> start installation"
821+
local installEndText="ESPHome Device Builder has been completely installed"
822+
local updateStartText="ESPHome Device Builder service detectet --> start update"
823+
local updateEndText="ESPHome Device Builder has been completely updated"
824+
local uninstallStartText="Start uninstalling the ESPHome Device Builder"
825+
local uninstallEndText="ESPHome Device Builder has been completely uninstalled"
826+
local errorText="An Error occured!\nFor Details please have a look at the shell messages"
827+
local portText="Access the webinterface at http://<your-ip>:$port"
828+
829+
830+
echo "$(timestamp) [openHABian] ##########################################################################################################"
831+
echo "$(timestamp) [openHABian] ESPHome Setup"
832+
833+
# This Precheck is neccesary to decide if install or update routine is neccesary
834+
if [ "$setupMode" = "install" ]; then
835+
echo "$(timestamp) [openHABian] The option installation / update was selected"
836+
echo "$(timestamp) [openHABian] Check if the esphome-device-builder.service is already running..."
837+
if systemctl is-active --quiet esphome-device-builder.service; then
838+
setupMode="update"
820839
fi
821-
echo "$(timestamp) [openHABian] Starting ESPHome Dashboard uninstallation..."
822-
823-
# Stop the ESPHome Dashboard service
824-
echo -n "$(timestamp) [openHABian] Stopping the ESPHome Dashboard service... "
825-
if ! cond_redirect systemctl stop esphome-dashboard.service; then echo "FAILED (stop service)"; return 1; fi
840+
fi
826841

827-
# Disable the ESPHome Dashboard service
828-
echo "$(timestamp) [openHABian] Disabling the ESPHome Dashboard service..."
829-
if ! cond_redirect systemctl disable esphome-dashboard.service; then
830-
echo "$(timestamp) [openHABian] Error: Failed to disable ESPHome Dashboard service."
831-
return
842+
if [ "$setupMode" = "install" ]; then
843+
echo "$(timestamp) [openHABian] $installStartText";
844+
echo "$(timestamp) [openHABian] Check if Python 3 and pip are already installed..."
845+
if ! dpkg -s python3-venv &>/dev/null; then
846+
echo "$(timestamp) [openHABian] Installing Python 3 and pip..."
847+
if ! cond_redirect apt install -y python3-venv; then
848+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to install Python 3 and pip.${COL_DEF}"
849+
return 1
850+
fi
851+
else
852+
echo "$(timestamp) [openHABian] Python 3 and pip are already available --> skip installation"
853+
fi
854+
855+
echo "$(timestamp) [openHABian] Creating directory at $esphomeDir and set permissions"
856+
if ! mkdir -p "$esphomeDir"; then
857+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to create $esphomeDir${COL_DEF}"
858+
return 1
832859
fi
833-
834-
# Remove the ESPHome Dashboard service file
835-
echo "$(timestamp) [openHABian] Removing the ESPHome Dashboard systemd service file..."
836-
if ! cond_redirect rm -f /etc/systemd/system/esphome-dashboard.service; then
837-
echo "$(timestamp) [openHABian] Error: Failed to remove systemd service file."
838-
return
860+
if ! chown -R "$LOGNAME:$LOGNAME" "$esphomeDir"; then
861+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to set ownership of $esphomeDir to $USER.${COL_DEF}"
862+
return 1
839863
fi
840-
841-
# Reload systemd daemon
842-
echo "$(timestamp) [openHABian] Reloading systemd daemon..."
843-
if ! cond_redirect systemctl daemon-reload; then
844-
echo "$(timestamp) [openHABian] Error: Failed to reload systemd daemon."
845-
return
864+
865+
echo "$(timestamp) [openHABian] Creating directory at $esphomeConfigDir and set permissions"
866+
if ! mkdir -p "$esphomeConfigDir"; then
867+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to create $esphomeConfigDir${COL_DEF}"
868+
return 1
846869
fi
847-
848-
# Remove the ESPHome installation directory
849-
echo "$(timestamp) [openHABian] Removing ESPHome directory at $ESPHOME_DIR..."
850-
if ! rm -rf "$ESPHOME_DIR"; then
851-
echo "$(timestamp) [openHABian] Error: Failed to remove $ESPHOME_DIR."
852-
return
870+
if ! chown -R "$LOGNAME:$LOGNAME" "$esphomeConfigDir"; then
871+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to set ownership of $esphomeConfigDir to $USER.${COL_DEF}"
872+
return 1
873+
fi
874+
875+
echo "$(timestamp) [openHABian] Setting up a virtual environment ($esphomeDir) and install ESPHome Device Builder"
876+
if ! python3 -m venv venv "$esphomeDir/venv"; then
877+
echo "$(timestamp) [openHABian] ${COL_RED}Error: Failed to create a Python virtual environment ($esphomeDir).${COL_DEF}"
878+
return 1
879+
fi
880+
881+
echo "$(timestamp) [openHABian] Activating the virtual environment."
882+
# the following shellcheck is neccesary because of error SC1091
883+
# shellcheck source=/dev/null
884+
if ! source "$esphomeDir/venv/bin/activate"; then
885+
echo "$(timestamp) [openHABian] ${COL_RED}Error: Failed to activate the Python virtual environment.${COL_DEF}"
886+
return 1
887+
fi
888+
889+
echo "$(timestamp) [openHABian] installing ESPHome Device Builder. This could take a few minutes!"
890+
if ! pip3 install esphome; then
891+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to install ESPHome Device Builder.${COL_DEF}"
892+
return 1
893+
fi
894+
895+
echo "$(timestamp) [openHABian] Installing systemd service file..."
896+
if ! SILENT=1 cond_redirect install -m 755 "$serviceTemplate" /etc/systemd/system/esphome-device-builder.service; then
897+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to install systemd service file.${COL_DEF}"
898+
return 1
853899
fi
854900

855-
echo "$(timestamp) [openHABian] Uninstallation complete!"
856-
return
857-
fi
858-
859-
if [[ $1 == "install" ]]; then
901+
echo "$(timestamp) [openHABian] modifying systemd service file..."
902+
# Use + as separator in sed instead of / because in the path are / included
903+
if ! sed -i "s+<username>+$LOGNAME+g; s+<esphome-directory>+$esphomeDir+g; s+<esphome-config-directory>+$esphomeConfigDir+g; s+# dynamically replaced in script++g" /etc/systemd/system/esphome-device-builder.service; then
904+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to modify systemd service file.${COL_DEF}"
905+
return 1
906+
fi
907+
908+
echo "$(timestamp) [openHABian] Reloading systemd daemon and starting the ESPHome Device Builder service..."
909+
if ! SILENT=1 cond_redirect systemctl daemon-reload; then
910+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to reload systemd daemon.${COL_DEF}"
911+
return 1
912+
fi
913+
914+
echo "$(timestamp) [openHABian] Enabling and starting the ESPHome Device Builder service..."
915+
if ! SILENT=1 cond_redirect systemctl enable --now esphome-device-builder.service; then
916+
echo -e"$(timestamp) [openHABian] ${COL_RED}Error: Failed to enable and start ESPHome Device Builder service.${COL_DEF}"
917+
return 1
918+
fi
919+
920+
echo -e "$(timestamp) [openHABian] ${COL_GREEN}$installEndText${COL_DEF}"
921+
echo -e "$(timestamp) [openHABian] ${COL_GREEN}$portText${COL_DEF}";
860922
if [[ -n $INTERACTIVE ]]; then
861-
whiptail --title "ESPhome dashboard installation" --msgbox "$installText" 8 80
923+
whiptail --title "$whiptailTitle" --msgbox "$installEndText\n$portText" 8 60
862924
fi
863-
echo "$(timestamp) [openHABian] Starting ESPHome Dashboard setup..."
864-
865-
866-
# Install Python 3 and pip
867-
echo "$(timestamp) [openHABian] Installing Python 3 and pip..."
868-
if ! cond_redirect apt install -y python3-venv; then
869-
echo "$(timestamp) [openHABian] Error: Failed to install Python 3 and pip."
870-
return
925+
926+
elif [ "$setupMode" = "update" ] ; then
927+
echo "$(timestamp) [openHABian] $updateStartText";
928+
echo "$(timestamp) [openHABian] Activating the virtual environment."
929+
# the following shellcheck is neccesary because of error SC1091
930+
# shellcheck source=/dev/null
931+
if ! source "$esphomeDir/venv/bin/activate"; then
932+
echo "$(timestamp) [openHABian] ${COL_RED}Error: Failed to activate thr Python virtual environment.${COL_DEF}"
933+
return 1
871934
fi
872935

873-
# Create the /opt/esphomedashboard directory and set permissions
874-
echo "$(timestamp) [openHABian] Creating directory at $ESPHOME_DIR..."
875-
if ! mkdir -p "$ESPHOME_DIR/config"; then
876-
echo "$(timestamp) [openHABian] Error: Failed to create $ESPHOME_DIR and its config directory."
877-
return
936+
echo "$(timestamp) [openHABian] updating ESPHome Device Builder..."
937+
if ! pip3 install esphome -U; then
938+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to update ESPHome Device Builder.${COL_DEF}"
939+
return 1
878940
fi
879941

880-
USER=$(logname)
881-
if ! chown -R "$USER:$USER" "$ESPHOME_DIR"; then
882-
echo "$(timestamp) [openHABian] Error: Failed to set ownership of $ESPHOME_DIR to $USER."
883-
return
942+
echo -e "$(timestamp) [openHABian] ${COL_GREEN}$updateEndText${COL_DEF}"
943+
echo -e "$(timestamp) [openHABian] ${COL_GREEN}$portText${COL_DEF}";
944+
if [[ -n $INTERACTIVE ]]; then
945+
whiptail --title "$whiptailTitle" --msgbox "$updateEndText\n$portText" 8 60
884946
fi
947+
948+
elif [ "$setupMode" = "remove" ] ; then
949+
echo "$(timestamp) [openHABian] $uninstallStartText"
950+
951+
# Check if the esphome-device-builder.service is active. If YES stop and disable the service
952+
# This check is neccesary to prevent a failure after an unsucsessful instalation
953+
if systemctl is-active --quiet esphome-device-builder.service; then
954+
echo "$(timestamp) [openHABian] Stopping the ESPHome Device Builder service."
955+
if ! SILENT=1 cond_redirect systemctl stop esphome-device-builder.service; then
956+
echo "$(timestamp) [openHABian] ${COL_RED}Error: Failed to stop ESPHome Device Builder service.${COL_DEF}"
957+
return 1
958+
fi
885959

886-
# Set up a virtual environment and install ESPHome
887-
echo "$(timestamp) [openHABian] Setting up a virtual environment in $ESPHOME_DIR..."
888-
if ! sudo -u "$USER" python3 -m venv "$ESPHOME_DIR/venv"; then
889-
echo "$(timestamp) [openHABian] Error: Failed to create a Python virtual environment."
890-
return
960+
echo "$(timestamp) [openHABian] Disabling the ESPHome Device Builder service."
961+
if ! SILENT=1 cond_redirect systemctl disable esphome-device-builder.service; then
962+
echo "$(timestamp) [openHABian] ${COL_RED}Error: Failed to disable ESPHome Device Builder service.${COL_DEF}"
963+
return 1
964+
fi
891965
fi
892966

893-
echo "$(timestamp) [openHABian] Activating the virtual environment and installing ESPHome..."
894-
if ! sudo -u "$USER" bash -c "source venv/bin/activate && pip install esphome"; then
895-
echo "$(timestamp) [openHABian] Error: Failed to install ESPHome."
896-
return
967+
echo "$(timestamp) [openHABian] Removing the ESPHome Device Builder systemd service file."
968+
if ! SILENT=1 cond_redirect rm -f /etc/systemd/system/esphome-device-builder.service; then
969+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to remove the ESPHome Device Builder systemd service file.${COL_DEF}"
970+
return 1
897971
fi
898972

899-
# Copy the systemd service file
900-
echo "$(timestamp) [openHABian] Installing systemd service file..."
901-
if ! cond_redirect install -m 755 "$SERVICE_TEMPLATE" /etc/systemd/system/esphome-dashboard.service; then
902-
echo "$(timestamp) [openHABian] Error: Failed to install systemd service file."
903-
return
973+
echo "$(timestamp) [openHABian] Reloading systemd daemon."
974+
if ! SILENT=1 cond_redirect systemctl daemon-reload; then
975+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to reload systemd daemon.${COL_DEF}"
976+
return 1
904977
fi
905978

906-
# Reload systemd and enable/start the service
907-
echo "$(timestamp) [openHABian] Reloading systemd daemon and starting the ESPHome Dashboard service..."
908-
if ! cond_redirect systemctl daemon-reload; then
909-
echo "$(timestamp) [openHABian] Error: Failed to reload systemd daemon."
910-
return
979+
echo "$(timestamp) [openHABian] Removing ESPHome Device Builder directory at $esphomeDir."
980+
if ! rm -rf "$esphomeDir"; then
981+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to remove ESPHome Device Builder directory at $esphomeDir.${COL_DEF}"
982+
return 1
911983
fi
912984

913-
# Enable and start the ESPHome Dashboard service
914-
echo "$(timestamp) [openHABian] Enabling and starting the ESPHome Dashboard service..."
915-
if ! cond_redirect systemctl enable --now esphome-dashboard.service; then
916-
echo "$(timestamp) [openHABian] Error: Failed to enable and start ESPHome Dashboard service."
917-
return
985+
echo "$(timestamp) [openHABian] Removing ESPHome Device Builder build directory at $esphomeConfigDir..."
986+
if ! (rm -rf "$esphomeConfigDir/.esphome/" && rm -f "$esphomeConfigDir/.gitignore"); then
987+
echo -e "$(timestamp) [openHABian] ${COL_RED}Error: Failed to remove ESPHome Device Builder build folders at $esphomeConfigDir.${COL_DEF}"
988+
return 1
918989
fi
990+
echo "$(timestamp) [openHABian] ESPHome Device Builder config files are still available: $esphomeConfigDir"
919991

920-
echo "$(timestamp) [openHABian] ESPHome Dashboard setup complete!"
921-
echo "$(timestamp) [openHABian] Access your ESPHome Dashboard at http://<your-ip>:6052"
922-
return
992+
echo -e "$(timestamp) [openHABian] ${COL_GREEN}ESPHome Device Builder uninstallation complete!${COL_DEF}"
993+
if [[ -n $INTERACTIVE ]]; then
994+
whiptail --title "$whiptailTitle" --msgbox "$uninstallEndText" 8 60
995+
fi
996+
else
997+
echo "$(timestamp) [openHABian] ${COL_RED}An unknown parameter was sent by menu.bash${COL_DEF}"
998+
return 1
923999
fi
1000+
1001+
echo "$(timestamp) [openHABian] ##########################################################################################################"
9241002
}
9251003

9261004
## Function for (un)installing Grott proxy server on the current system

functions/packages.bats

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ teardown_file() {
6666
echo -e "# ${COL_GREEN}$(timestamp) [openHABian] 1wire installation successful.${COL_DEF}" >&3
6767
}
6868

69+
@test "destructive-setup_esphome_device_builder" {
70+
## Confirm ESPHome Device builder install completes without error
71+
echo -e "# ${COL_CYAN}$(timestamp) [openHABian] ESPHome Device builder installation starting...${COL_DEF}" >&3
72+
run setup_esphome_device_builder "install" 3>&-
73+
if [ "$status" -ne 0 ]; then echo "$output" >&3; fi
74+
[ "$status" -eq 0 ]
75+
echo -e "# ${COL_GREEN}$(timestamp) [openHABian] ESPHome Device builder installation successful.${COL_DEF}" >&3
76+
77+
## Confirm ESPHome Device builder service is running
78+
echo -e "# ${COL_CYAN}$(timestamp) [openHABian] Checking if ESPHome Device builder service is running after instalation...${COL_DEF}" >&3
79+
run systemctl is-active --quiet esphome-device-builder.service
80+
if [ "$status" -ne 0 ]; then echo "$output" >&3; fi
81+
[ "$status" -eq 0 ]
82+
echo -e "# ${COL_GREEN}$(timestamp) [openHABian] ESPHome Device builder service is running after instalation.${COL_DEF}" >&3
83+
84+
## Confirm ESPHome Device builder update completes without error
85+
## Test ist the same as for installation, but the service is already runngin and so th script decided to update the ESPHome Device builder
86+
echo -e "# ${COL_CYAN}$(timestamp) [openHABian] ESPHome Device builder update starting...${COL_DEF}" >&3
87+
run setup_esphome_device_builder "install" 3>&-
88+
if [ "$status" -ne 0 ]; then echo "$output" >&3; fi
89+
[ "$status" -eq 0 ]
90+
echo -e "# ${COL_GREEN}$(timestamp) [openHABian] ESPHome Device builder update successful.${COL_DEF}" >&3
91+
92+
## Confirm ESPHome Device builder service is running
93+
echo -e "# ${COL_CYAN}$(timestamp) [openHABian] Checking if ESPHome Device builder service is running after update...${COL_DEF}" >&3
94+
run systemctl is-active --quiet esphome-device-builder.service
95+
if [ "$status" -ne 0 ]; then echo "$output" >&3; fi
96+
[ "$status" -eq 0 ]
97+
echo -e "# ${COL_GREEN}$(timestamp) [openHABian] ESPHome Device builder service is running after update.${COL_DEF}" >&3
98+
99+
## Confirm ESPHome Device builder uninstall completes without error
100+
echo -e "# ${COL_CYAN}$(timestamp) [openHABian] ESPHome Device builder uninstallation starting...${COL_DEF}" >&3
101+
run setup_esphome_device_builder "remove" 3>&-
102+
if [ "$status" -ne 0 ]; then echo "$output" >&3; fi
103+
[ "$status" -eq 0 ]
104+
echo -e "# ${COL_GREEN}$(timestamp) [openHABian] ESPHome Device builder uninstallation successful.${COL_DEF}" >&3
105+
}
106+
69107
@test "destructive-grott_install" {
70108
## Confirm Grott Proxy install completes without error
71109
echo -e "# ${COL_CYAN}$(timestamp) [openHABian] Grott Proxy installation starting...${COL_DEF}" >&3

0 commit comments

Comments
 (0)