From 6598f3618d628fea13978b5b8674fcd93c16de15 Mon Sep 17 00:00:00 2001 From: michaelloercher Date: Sun, 15 Dec 2024 07:30:26 +0100 Subject: [PATCH] Adding esphome dashboard install and uninstall feature --- functions/menu.bash | 6 ++ functions/packages.bash | 157 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) diff --git a/functions/menu.bash b/functions/menu.bash index 54a3a331f..a58103fed 100644 --- a/functions/menu.bash +++ b/functions/menu.bash @@ -132,6 +132,9 @@ show_main_menu() { "2D | Install EVCC" "Deploy Electric Vehicle Charge Controller" \ " | Remove EVCC" "Uninstall EVCC" \ " | Setup EVCC" "Setup EVCC from command line (German only)" \ + "2E | Install ESPHome dashboard" "Deploy ESPHome dashboard" \ + " | Remove ESPHome dashboard" "Uninstall ESPHome dashboard" \ + 3>&1 1>&2 2>&3) RET=$? if [ $RET -eq 1 ] || [ $RET -eq 255 ]; then return 0; fi @@ -157,6 +160,9 @@ show_main_menu() { 2D\ *) install_evcc "install";; *Remove\ EVCC*) install_evcc "remove";; *Setup\ EVCC*) setup_evcc;; + 2E\ *) install_esphomedashboard "install";; + *Remove\ ESPHome dashboard*) install_esphomedashboard "remove";; + *Setup\ ESPHome dashboard*) setup_evcc;; "") return 0 ;; *) whiptail --msgbox "An unsupported option was selected (probably a programming error):\\n \"$choice2\"" 8 80 ;; esac diff --git a/functions/packages.bash b/functions/packages.bash index 702e0036b..d1d677c9e 100644 --- a/functions/packages.bash +++ b/functions/packages.bash @@ -798,3 +798,160 @@ setup_evcc() { echo -n "$(timestamp) [openHABian] Created EVCC config, restarting ... " if cond_redirect systemctl restart evcc.service; then echo "OK"; else echo "FAILED"; fi } + +## Function for (un)installing ESPhome dashboard +## The function must be invoked UNATTENDED. +## Valid arguments: "install" or "remove" +## +## install_esphomedashboard(String action) +## +## +install_esphomedashboard() { + local port=6052 + local installText="This will install ESPhome dashboard\\nUse the web interface on port $port to access ESPhome dashboard web interface." + local removeText="This will remove ESPhome dashboard" + + if [[ $1 == "remove" ]]; then + if [[ -n $INTERACTIVE ]]; then + whiptail --title "ESPhome dashboard removal" --msgbox "$removeText" 7 80 + fi + echo -n "$(timestamp) [openHABian] Starting ESPHome Dashboard uninstallation..." + + # Function to check the success of the last executed command + check_success() { + if [ $? -ne 0 ]; then + echo -n "$(timestamp) [openHABian] Error: $1" + exit 1 + fi + } + + # Ensure the script is run with sudo + if [ "$EUID" -ne 0 ]; then + echo -n "$(timestamp) [openHABian] Please run this script as root or with sudo." + exit 1 + fi + + # Stop the ESPHome Dashboard service + echo -n "$(timestamp) [openHABian] Stopping the ESPHome Dashboard service..." + systemctl stop esphome-dashboard.service + check_success "Failed to stop ESPHome Dashboard service." + + # Disable the ESPHome Dashboard service + echo -n "$(timestamp) [openHABian] Disabling the ESPHome Dashboard service..." + systemctl disable esphome-dashboard.service + check_success "Failed to disable ESPHome Dashboard service." + + # Remove the ESPHome Dashboard service file + echo -n "$(timestamp) [openHABian] Removing the ESPHome Dashboard systemd service file..." + rm -f /etc/systemd/system/esphome-dashboard.service + check_success "Failed to remove systemd service file." + + # Reload systemd daemon + echo -n "$(timestamp) [openHABian] Reloading systemd daemon..." + systemctl daemon-reload + check_success "Failed to reload systemd daemon." + + # Remove the ESPHome installation directory + ESPHOME_DIR="/opt/esphomedashboard" + echo -n "$(timestamp) [openHABian] Removing ESPHome directory at $ESPHOME_DIR..." + rm -rf "$ESPHOME_DIR" + check_success "Failed to remove $ESPHOME_DIR." + + # Final cleanup + echo -n "$(timestamp) [openHABian] Uninstallation complete!" + echo -n "$(timestamp) [openHABian] ESPHome Dashboard has been completely removed from the system." + return; + fi + + if [[ $1 != "install" ]]; then + if [[ -n $INTERACTIVE ]]; then + whiptail --title "ESPhome dashboard installation" --msgbox "$installText" 8 80 + fi + echo -n "$(timestamp) [openHABian] Starting ESPHome Dashboard setup..." + + # Function to check the success of the last executed command + check_success() { + if [ $? -ne 0 ]; then + echo -n "$(timestamp) [openHABian] Error: $1" + exit 1 + fi + } + + # Ensure the script is run with sudo + if [ "$EUID" -ne 0 ]; then + echo -n "$(timestamp) [openHABian] Please run this script as root or with sudo." + exit 1 + fi + + # Update system packages + echo -n "$(timestamp) [openHABian] Updating system packages..." + apt update && apt upgrade -y + check_success "Failed to update system packages." + + # Install Python 3 and pip + echo -n "$(timestamp) [openHABian] Installing Python 3 and pip..." + apt install -y python3 python3-pip python3-venv + check_success "Failed to install Python 3 and pip." + + # Create the /opt/esphomedashboard directory and set permissions + ESPHOME_DIR="/opt/esphomedashboard" + echo -n "$(timestamp) [openHABian] Creating directory at $ESPHOME_DIR..." + mkdir -p "$ESPHOME_DIR/config" + check_success "Failed to create $ESPHOME_DIR and its config directory." + + # Set ownership to the current non-root user + USER=$(logname) + chown -R "$USER:$USER" "$ESPHOME_DIR" + check_success "Failed to set ownership of $ESPHOME_DIR to $USER." + + # Switch to the ESPHome directory and set up a virtual environment + echo -n "$(timestamp) [openHABian] Setting up a virtual environment in $ESPHOME_DIR..." + cd "$ESPHOME_DIR" || exit + sudo -u "$USER" python3 -m venv venv + check_success "Failed to create a Python virtual environment." + + # Activate the virtual environment and install ESPHome + echo -n "$(timestamp) [openHABian] Activating the virtual environment and installing ESPHome..." + sudo -u "$USER" bash -c "source venv/bin/activate && pip install esphome" + check_success "Failed to install ESPHome." + + # Create the ESPHome Dashboard systemd service + echo -n "$(timestamp) [openHABian] Creating a systemd service for ESPHome Dashboard..." + cat > /etc/systemd/system/esphome-dashboard.service <:6052" + return ; + fi + +} + +