Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding esphome dashboard install and uninstall feature #1932

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions functions/menu.bash
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ 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
Expand All @@ -157,6 +159,8 @@ 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";;
"") return 0 ;;
*) whiptail --msgbox "An unsupported option was selected (probably a programming error):\\n \"$choice2\"" 8 80 ;;
esac
Expand Down
135 changes: 135 additions & 0 deletions functions/packages.bash
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,138 @@ 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"

ESPHOME_DIR="/opt/esphomedashboard"
SERVICE_TEMPLATE="../includes/esphome-dashboard.service.template" # Update with the actual path
miloit marked this conversation as resolved.
Show resolved Hide resolved

if [[ $1 == "remove" ]]; then
if [[ -n $INTERACTIVE ]]; then
whiptail --title "ESPhome dashboard removal" --msgbox "$removeText" 7 80
fi
echo "$(timestamp) [openHABian] Starting ESPHome Dashboard uninstallation..."

# Stop the ESPHome Dashboard service
echo "$(timestamp) [openHABian] Stopping the ESPHome Dashboard service..."
if ! systemctl stop esphome-dashboard.service; then
echo "$(timestamp) [openHABian] Error: Failed to stop ESPHome Dashboard service."
return
fi
miloit marked this conversation as resolved.
Show resolved Hide resolved

# Disable the ESPHome Dashboard service
echo "$(timestamp) [openHABian] Disabling the ESPHome Dashboard service..."
if ! systemctl disable esphome-dashboard.service; then
echo "$(timestamp) [openHABian] Error: Failed to disable ESPHome Dashboard service."
return
fi

# Remove the ESPHome Dashboard service file
echo "$(timestamp) [openHABian] Removing the ESPHome Dashboard systemd service file..."
if ! rm -f /etc/systemd/system/esphome-dashboard.service; then
echo "$(timestamp) [openHABian] Error: Failed to remove systemd service file."
return
fi

# Reload systemd daemon
echo "$(timestamp) [openHABian] Reloading systemd daemon..."
if ! systemctl daemon-reload; then
echo "$(timestamp) [openHABian] Error: Failed to reload systemd daemon."
return
fi

# Remove the ESPHome installation directory
echo "$(timestamp) [openHABian] Removing ESPHome directory at $ESPHOME_DIR..."
if ! rm -rf "$ESPHOME_DIR"; then
echo "$(timestamp) [openHABian] Error: Failed to remove $ESPHOME_DIR."
return
fi

echo "$(timestamp) [openHABian] Uninstallation complete!"
return
fi

if [[ $1 == "install" ]]; then
if [[ -n $INTERACTIVE ]]; then
whiptail --title "ESPhome dashboard installation" --msgbox "$installText" 8 80
fi
echo "$(timestamp) [openHABian] Starting ESPHome Dashboard setup..."

# Ensure the script is run with sudo
if [ "$EUID" -ne 0 ]; then
echo "$(timestamp) [openHABian] Please run this script as root or with sudo."
return
fi
miloit marked this conversation as resolved.
Show resolved Hide resolved

# Install Python 3 and pip
echo "$(timestamp) [openHABian] Installing Python 3 and pip..."
if ! apt install -y python3-venv; then
echo "$(timestamp) [openHABian] Error: Failed to install Python 3 and pip."
return
fi

# Create the /opt/esphomedashboard directory and set permissions
echo "$(timestamp) [openHABian] Creating directory at $ESPHOME_DIR..."
if ! mkdir -p "$ESPHOME_DIR/config"; then
echo "$(timestamp) [openHABian] Error: Failed to create $ESPHOME_DIR and its config directory."
return
fi

USER=$(logname)
miloit marked this conversation as resolved.
Show resolved Hide resolved
if ! chown -R "$USER:$USER" "$ESPHOME_DIR"; then
echo "$(timestamp) [openHABian] Error: Failed to set ownership of $ESPHOME_DIR to $USER."
return
fi

# Set up a virtual environment and install ESPHome
echo "$(timestamp) [openHABian] Setting up a virtual environment in $ESPHOME_DIR..."
cd "$ESPHOME_DIR" || return
miloit marked this conversation as resolved.
Show resolved Hide resolved
if ! sudo -u "$USER" python3 -m venv "$ESPHOME_DIR/venv"; then
echo "$(timestamp) [openHABian] Error: Failed to create a Python virtual environment."
return
fi

echo "$(timestamp) [openHABian] Activating the virtual environment and installing ESPHome..."
if ! sudo -u "$USER" bash -c "source venv/bin/activate && pip install esphome"; then
echo "$(timestamp) [openHABian] Error: Failed to install ESPHome."
return
fi

# Copy the systemd service file
echo "$(timestamp) [openHABian] Copying systemd service file..."
if ! cp "$SERVICE_TEMPLATE" /etc/systemd/system/esphome-dashboard.service; then
miloit marked this conversation as resolved.
Show resolved Hide resolved
echo "$(timestamp) [openHABian] Error: Failed to copy systemd service file."
return
fi

# Reload systemd and enable/start the service
echo "$(timestamp) [openHABian] Reloading systemd daemon and starting the ESPHome Dashboard service..."
if ! systemctl daemon-reload; then
echo "$(timestamp) [openHABian] Error: Failed to reload systemd daemon."
return
fi

# Enable and start the ESPHome Dashboard service
echo "$(timestamp) [openHABian] Enabling and starting the ESPHome Dashboard service..."
if ! systemctl enable --now esphome-dashboard.service; then
echo "$(timestamp) [openHABian] Error: Failed to enable and start ESPHome Dashboard service."
return
fi

echo "$(timestamp) [openHABian] ESPHome Dashboard setup complete!"
echo "$(timestamp) [openHABian] Access your ESPHome Dashboard at http://<your-ip>:6052"
return
fi
}


13 changes: 13 additions & 0 deletions includes/esphome-dashboard.service.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=ESPHome Dashboard
After=network.target

[Service]
WorkingDirectory=/opt/esphomedashboard
ExecStart=/opt/esphomedashboard/venv/bin/esphome dashboard /opt/esphomedashboard/config --port 6052
Restart=always
User=<replace-with-username> # Dynamically replaced in script
Environment="PATH=/opt/esphomedashboard/venv/bin"

[Install]
WantedBy=multi-user.target
Loading