Skip to content

Instantly share code, notes, and snippets.

@pimlie
Last active May 8, 2025 16:21
Show Gist options
  • Save pimlie/7f5881004498c0cd370eea377e0626ff to your computer and use it in GitHub Desktop.
Save pimlie/7f5881004498c0cd370eea377e0626ff to your computer and use it in GitHub Desktop.
Zigbee2MQTT allows users to cache device images locally, but does this by downloading images for your currently configured files. This is half annoying as you have to remember to manually localise the device images after adding a new device, but it also allows Koenk/Github/Microsoft to track what zigbee devices users have running locally. This s…
#!/usr/bin/env bash
# Url from: https://github.com/nurikk/zigbee2mqtt-frontend/blob/dev/src/components/device-image/index.tsx
device_image_url="https://www.zigbee2mqtt.io/images/devices/"
# Zip file containing full repo https://github.com/Koenkk/zigbee2mqtt.io
zigbee2mqttio_url="https://github.com/Koenkk/zigbee2mqtt.io/archive/refs/heads/master.zip"
# Path to download above zip file to, relative from data folder
repo_zip_path="master.zip"
set -e
fatal() {
log "$@" >&2
exit 1
}
log() {
if [ "$quiet" -ne 1 ]; then
echo "$@"
fi
}
logn() {
if [ "$quiet" -ne 1 ]; then
echo -n "$@"
fi
}
usage() {
fatal "Usage $0 [-q|-v] CONTAINER_NAME"
}
quiet=0
docker_stdout="/dev/null"
docker_stderr="/dev/null"
while getopts "qv" opt; do
case "$opt" in
v)
docker_stdout="/dev/stdout"
docker_stderr="/dev/stderr"
shift
;;
q)
quiet=1
shift
;;
*)
usage
;;
esac
done
container_name="$1"
if [ -z "$container_name" ]; then
usage
fi
docker_exec() {
docker exec "$container_name" "$@" >"$docker_stdout" 2>"$docker_stderr"
}
docker_value() {
docker exec "$container_name" "$@" 2>"$docker_stderr"
}
if ! docker ps >/dev/null 2>&1; then
fatal "Make sure docker is running and current user has docker access"
fi
if [ "$(docker inspect -f '{{.State.Running}}' "$container_name")" != true ]; then
fatal "Container with name $container_name is not running, start it first"
fi
logn "Checking zigbee2mqtt-frontend device image path"
mapfile -t files < <(docker_value grep -lr "$device_image_url" /app/node_modules/zigbee2mqtt-frontend)
log " -> done, found ${#files[@]} files to update"
if [ "${#files[@]}" -gt 0 ]; then
log "Replacing device image url in zigbee2mqtt-frontend"
for file in "${files[@]}"; do
if [ -z "$file" ]; then
continue
fi
logn "In file: $file"
docker_exec sed -i "s~$device_image_url~/device_icons/~" "$file"
log " -> done"
done
fi
data_folder="$(docker_exec sh -c 'echo $ZIGBEE2MQTT_DATA')"
data_folder="${data_folder:-/app/data}"
temp_download_file="${data_folder}/${repo_zip_path}"
logn "Downloading full zigbee2mqtt.io master branch zip"
docker_exec wget -O "$temp_download_file" "$zigbee2mqttio_url"
log " -> done"
logn "Unzipping download file"
docker_exec unzip -o -d "${data_folder}" "$temp_download_file"
log " -> done"
logn "Removing old device icons"
docker_exec rm -Rf "${data_folder}/device_icons"
log " -> done"
logn "Moving new device icons"
docker_exec mv "${data_folder}/zigbee2mqtt.io-master/public/images/devices" "${data_folder}/device_icons"
log " -> done"
logn "Removing downloaded files"
docker_exec rm -Rf "${data_folder}/zigbee2mqtt.io-master" "$temp_download_file"
log " -> done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment