Skip to content

Commit dae8223

Browse files
authored
Merge pull request SensorsIot#774 from Paraphraser/20240721-esphome-old-menu
2024-07-21 Adds ESPHome - old-menu branch - PR 2 of 2
2 parents 2414be1 + 0c08c17 commit dae8223

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Assumptions:
2+
#
3+
# 1. The ESPhome container is running with the container-name "esphome".
4+
#
5+
# 2. The service definition for the ESPhome container includes:
6+
#
7+
# device_cgroup_rules:
8+
# - 'c 188:* rw'
9+
#
10+
# This clause permits the container to access any device with a major
11+
# number 188, which captures most USB-to-serial adapters that are
12+
# found on ESP32 dev boards or equivalent off-board adapters such as
13+
# those made by Future Technology Devices International (FTDI) and
14+
# Silicon Laboratories Incorporated. The major number 188 also shows
15+
# up in the UDEV rules below.
16+
#
17+
# 3. The ESP device to be managed is mounted and/or unmounted WHILE the
18+
# container is running. In other words, all bets are off if the host
19+
# system reboots or the container starts while the USB device is
20+
# connected. You will likely need to unplug/replug the device to
21+
# get the container back in sync.
22+
#
23+
# The rules do NOT check if the container is running and do NOT check
24+
# for errors. All that will happen is errors in the system log.
25+
#
26+
# Removing ESPhome from your stack does NOT remove this rules file. It
27+
# does not matter whether you accomplish removal by editing your compose
28+
# file or via the IOTstack menu, this rule will be left in place and it
29+
# will generate an error every time it fires in response to insertion
30+
# or removal of a matching USB device.
31+
#
32+
# It is perfectly safe to remove this rules file yourself:
33+
#
34+
# sudo rm /etc/udev/rules.d/88-tty-iotstack-esphome.rules
35+
#
36+
# That's all you have to do. UDEV is dynamic and, despite what you read
37+
# on the web, does NOT have to be restarted or reloaded.
38+
39+
# Upon insertion of a matching USB device, mount the same device inside the container
40+
ACTION=="add", \
41+
SUBSYSTEM=="tty", ENV{MAJOR}=="188", \
42+
RUN+="/usr/bin/docker exec esphome mknod %E{DEVNAME} c %M %m"
43+
44+
# Upon removal of a matching USB device, remove the same device inside the container
45+
ACTION=="remove", \
46+
SUBSYSTEM=="tty", ENV{MAJOR}=="188", \
47+
RUN+="/usr/bin/docker exec esphome rm -f %E{DEVNAME}"

.templates/esphome/build.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit 1
5+
6+
# assumes (like all build.sh scripts) that the working directory is
7+
# the docker-compose project directory (typically ~/IOTstack)
8+
PROJECT="$PWD"
9+
10+
# sanity check
11+
[ ! -d "$PROJECT/.templates" ] && echo "This script should be run from ~/IOTstack" && exit 1
12+
13+
# where is this script is running? The expected answer is:
14+
# «projectDirectory»/.templates/esphome
15+
WHERE="$(dirname "$(realpath "$0")")"
16+
17+
# the service name is implied from the last part of the template path
18+
SERVICE=$(basename "$WHERE")
19+
20+
# the rules file name is
21+
RULES_FILE="88-tty-iotstack-${SERVICE}.rules"
22+
23+
# the original rules file is here
24+
SOURCE_PATH="$WHERE/$RULES_FILE"
25+
26+
# the path to install the file is here
27+
TARGET_PATH="/etc/udev/rules.d/$RULES_FILE"
28+
29+
if [ ! -f "$TARGET_PATH" ] ; then
30+
if [ -f "$SOURCE_PATH" ] ; then
31+
sudo cp "$SOURCE_PATH" "$TARGET_PATH"
32+
sudo chmod 644 "$TARGET_PATH"
33+
fi
34+
fi
35+
36+
# define the path to the environment file
37+
ENV_FILE="$PROJECT/.env"
38+
39+
# ensure the file exists
40+
touch "$ENV_FILE"
41+
42+
# $1 = key
43+
# $2 = value
44+
setEnvironment() {
45+
if [ $(grep -c "^$1=" "$ENV_FILE") -eq 0 ] ; then
46+
echo "$1=$2" >>"$ENV_FILE"
47+
fi
48+
}
49+
50+
# generate random password
51+
PASSWORD=$(< /dev/urandom tr -dc A-Z-a-z-0-9 | head -c ${1:-16})
52+
53+
# conditionally set variables
54+
setEnvironment "ESPHOME_USERNAME" "$SERVICE"
55+
setEnvironment "ESPHOME_PASSWORD" "$PASSWORD"

.templates/esphome/service.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
esphome:
2+
container_name: esphome
3+
image: esphome/esphome
4+
restart: unless-stopped
5+
environment:
6+
- TZ=${TZ:-Etc/UTC}
7+
- USERNAME=${ESPHOME_USERNAME:-esphome}
8+
- PASSWORD=${ESPHOME_PASSWORD:?eg echo ESPHOME_PASSWORD=ChangeMe >>~/IOTstack/.env}
9+
network_mode: host
10+
x-ports:
11+
- "6052:6052"
12+
volumes:
13+
- ./volumes/esphome/config:/config
14+
device_cgroup_rules:
15+
- 'c 188:* rw'

menu.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ declare -A cont_array=(
6868
[mjpg-streamer]="MJPG Streamer"
6969
[home_assistant]="Home Assistant Container"
7070
[wordpress]="Web content management"
71+
[esphome]="ESPHome device provisioning"
7172
# add yours here
7273
)
7374

@@ -120,6 +121,7 @@ declare -a keylist=(
120121
"mjpg-streamer"
121122
"home_assistant"
122123
"wordpress"
124+
"esphome"
123125
# add yours here
124126
)
125127

0 commit comments

Comments
 (0)