@@ -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
0 commit comments