Skip to content

Commit 66ab254

Browse files
authored
Refactor: ESPHome to uv based install (#5413)
* Refactor: ESPHome to uv based install * fix curl
1 parent daad486 commit 66ab254

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

ct/esphome.sh

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,57 @@ function update_script() {
2525
check_container_resources
2626
if [[ ! -f /etc/systemd/system/esphomeDashboard.service ]]; then
2727
msg_error "No ${APP} Installation Found!"
28-
exit
28+
exit 1
2929
fi
30-
msg_info "Stopping ESPHome"
30+
31+
msg_info "Stopping ${APP}"
3132
systemctl stop esphomeDashboard
32-
msg_ok "Stopped ESPHome"
33+
msg_ok "Stopped ${APP}"
34+
35+
VENV_PATH="/opt/esphome/.venv"
36+
ESPHOME_BIN="${VENV_PATH}/bin/esphome"
37+
export PYTHON_VERSION="3.12"
38+
39+
if [[ ! -d "$VENV_PATH" || ! -x "$ESPHOME_BIN" ]]; then
40+
PYTHON_VERSION="3.12" setup_uv
41+
msg_info "Migrating to uv/venv"
42+
rm -rf "$VENV_PATH"
43+
mkdir -p /opt/esphome
44+
cd /opt/esphome
45+
$STD uv venv "$VENV_PATH"
46+
$STD "$VENV_PATH/bin/python" -m ensurepip --upgrade
47+
$STD "$VENV_PATH/bin/python" -m pip install --upgrade pip
48+
$STD "$VENV_PATH/bin/python" -m pip install esphome tornado esptool
49+
msg_ok "Migrated to uv/venv"
50+
else
51+
msg_info "Updating ESPHome"
52+
PYTHON_VERSION="3.12" setup_uv
53+
$STD "$VENV_PATH/bin/python" -m pip install --upgrade esphome tornado esptool
54+
msg_ok "Updated ESPHome"
55+
fi
56+
SERVICE_FILE="/etc/systemd/system/esphomeDashboard.service"
57+
if ! grep -q "${VENV_PATH}/bin/esphome" "$SERVICE_FILE"; then
58+
msg_info "Updating systemd service"
59+
cat <<EOF >"$SERVICE_FILE"
60+
[Unit]
61+
Description=ESPHome Dashboard
62+
After=network.target
63+
64+
[Service]
65+
ExecStart=${VENV_PATH}/bin/esphome dashboard /root/config/
66+
Restart=always
67+
User=root
3368
34-
msg_info "Updating ESPHome"
35-
if [[ -d /srv/esphome ]]; then
36-
$STD source /srv/esphome/bin/activate
69+
[Install]
70+
WantedBy=multi-user.target
71+
EOF
72+
$STD systemctl daemon-reload
73+
msg_ok "Updated systemd service"
3774
fi
38-
$STD pip3 install -U esphome
39-
msg_ok "Updated ESPHome"
4075

41-
msg_info "Starting ESPHome"
76+
msg_info "Starting ${APP}"
4277
systemctl start esphomeDashboard
43-
msg_ok "Started ESPHome"
78+
msg_ok "Started ${APP}"
4479
msg_ok "Updated Successfully"
4580
exit
4681
}
@@ -52,4 +87,4 @@ description
5287
msg_ok "Completed Successfully!\n"
5388
echo -e "${CREATING}${GN}${APP} setup has been successfully initialized!${CL}"
5489
echo -e "${INFO}${YW} Access it using the following URL:${CL}"
55-
echo -e "${TAB}${GATEWAY}${BGN}http://${IP}:6052${CL}"
90+
echo -e "${TAB}${GATEWAY}${BGN}http://${IP}:6052${CL}"

install/esphome-install.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,34 @@ msg_info "Installing Dependencies"
1717
$STD apt-get install -y git
1818
msg_ok "Installed Dependencies"
1919

20-
msg_info "Setup Python3"
21-
$STD apt-get install -y \
22-
python3 \
23-
python3-dev \
24-
python3-pip \
25-
python3-venv
26-
rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED
27-
msg_ok "Setup Python3"
28-
29-
msg_info "Installing ESPHome"
30-
mkdir /root/config
31-
$STD pip install esphome tornado esptool
32-
msg_ok "Installed ESPHome"
20+
PYTHON_VERSION="3.12" setup_uv
21+
22+
msg_info "Setting up Virtual Environment"
23+
mkdir -p /opt/esphome
24+
mkdir -p /root/config
25+
cd /opt/esphome
26+
$STD uv venv /opt/esphome/.venv
27+
$STD /opt/esphome/.venv/bin/python -m ensurepip --upgrade
28+
$STD /opt/esphome/.venv/bin/python -m pip install --upgrade pip
29+
$STD /opt/esphome/.venv/bin/python -m pip install esphome tornado esptool
30+
msg_ok "Setup and Installed ESPHome"
3331

3432
msg_info "Creating Service"
33+
mkdir -p /root/config
3534
cat <<EOF >/etc/systemd/system/esphomeDashboard.service
3635
[Unit]
3736
Description=ESPHome Dashboard
3837
After=network.target
3938
4039
[Service]
41-
ExecStart=/usr/local/bin/esphome dashboard /root/config/
40+
ExecStart=/opt/esphome/.venv/bin/esphome dashboard /root/config/
4241
Restart=always
4342
User=root
4443
4544
[Install]
4645
WantedBy=multi-user.target
4746
EOF
47+
4848
systemctl enable -q --now esphomeDashboard
4949
msg_ok "Created Service"
5050

0 commit comments

Comments
 (0)