1- #! /bin/bash
2-
3- # ==== CONFIGURATION ====
4- GITHUB_USER=" tomer-w"
5- GITHUB_REPO=" ha-victron-mqtt"
6- TARGET_SUBFOLDER=" custom_components/victron_mqtt"
7- DEST_FOLDER=" /config/custom_components/victron_mqtt"
8- # ========================
9- # Usage:
10- # Run the script without arguments to update the integration to the latest release.
11- # Use the --restart flag to restart Home Assistant after updating.
12- # Use the --main flag to force using main branch even if releases exist.
13- # Use the --version <tag> flag to download a specific version (e.g., v1.0.0).
14- # Use the --list-versions flag to list all available versions.
15- # Examples:
16- # bash update_integration.sh --restart
17- # bash update_integration.sh --main
18- # bash update_integration.sh --main --restart
19- # bash update_integration.sh --version v1.0.0
20- # bash update_integration.sh --version v1.0.0 --restart
21- # bash update_integration.sh --list-versions
22-
23- RESTART_HA=false
24- USE_MAIN=false
25- SPECIFIC_VERSION=" "
26-
27- # Check for jq; if missing, warn and skip printing commit info when using main
28- if ! command -v jq > /dev/null 2>&1 ; then
29- echo " ⚠️ 'jq' not found in PATH. Commit info from GitHub will be skipped. To enable it, install jq."
30- JQ_AVAILABLE=false
31- else
32- JQ_AVAILABLE=true
33- fi
34-
35- # Parse command line arguments
36- for arg in " $@ " ; do
37- case $arg in
38- --restart)
39- RESTART_HA=true
40- ;;
41- --main)
42- USE_MAIN=true
43- ;;
44- --version)
45- # Next argument is the version
46- SPECIFIC_VERSION=" ${@: $((OPTIND+1)): 1} "
47- shift
48- ;;
49- --list-versions)
50- echo " 📋 Fetching available versions from GitHub..."
51- if ! command -v jq > /dev/null 2>&1 ; then
52- echo " ❌ 'jq' is required to list versions. Please install jq."
53- exit 1
54- fi
55- RELEASES=$( curl -s " https://api.github.com/repos/$GITHUB_USER /$GITHUB_REPO /releases" | jq -r ' .[].tag_name' | head -20)
56- if [ -z " $RELEASES " ]; then
57- echo " ❌ Could not fetch releases from GitHub."
58- exit 1
59- fi
60- echo " Available versions:"
61- echo " $RELEASES "
62- echo " "
63- echo " 💡 Usage: bash update_integration.sh --version <version-tag>"
64- exit 0
65- ;;
66- esac
67- done
68-
69- # Validate that --version and --main are not used together
70- if [ -n " $SPECIFIC_VERSION " ] && [ " $USE_MAIN " = true ]; then
71- echo " ❌ Error: Cannot use --version and --main flags together."
72- exit 1
73- fi
74-
75- # Fetch latest release tag via GitHub API (unless --main flag is used or --version is specified)
76- if [ -n " $SPECIFIC_VERSION " ]; then
77- echo " 📌 Using specific version: $SPECIFIC_VERSION "
78- LATEST_TAG=" $SPECIFIC_VERSION "
79- elif [ " $USE_MAIN " = true ]; then
80- echo " 🔀 Using main branch as requested (--main flag)"
81- LATEST_TAG=" null"
82- else
83- LATEST_TAG=$( curl -s " https://api.github.com/repos/$GITHUB_USER /$GITHUB_REPO /releases/latest" | jq -r ' .tag_name // empty' )
84- fi
85-
86- if [ " $LATEST_TAG " == " null" ] || [ -z " $LATEST_TAG " ]; then
87- if [ " $USE_MAIN " = true ]; then
88- echo " 📥 Downloading from main branch"
89- else
90- echo " ⚠️ No releases found. Falling back to main branch."
91- fi
92- ZIP_URL=" https://github.com/$GITHUB_USER /$GITHUB_REPO /archive/refs/heads/main.zip"
93- TMP_FOLDER=" $GITHUB_REPO -main"
94- # If we're downloading from main, fetch and print the latest commit info
95- if [ " $JQ_AVAILABLE " = true ]; then
96- echo " 🔎 Fetching latest commit info for main..."
97- # Query the GitHub commits API for the main branch
98- COMMIT_INFO=$( curl -s " https://api.github.com/repos/$GITHUB_USER /$GITHUB_REPO /commits/main" )
99- if [ -n " $COMMIT_INFO " ] && [ " $( echo " $COMMIT_INFO " | jq -r ' .sha // empty' ) " != " " ]; then
100- COMMIT_SHA=$( echo " $COMMIT_INFO " | jq -r ' .sha' )
101- # Show only the first line of the commit message
102- COMMIT_MESSAGE=$( echo " $COMMIT_INFO " | jq -r ' .commit.message' | head -n1 | tr -d ' \r' )
103- COMMIT_AUTHOR=$( echo " $COMMIT_INFO " | jq -r ' .commit.author.name' )
104- COMMIT_DATE=$( echo " $COMMIT_INFO " | jq -r ' .commit.author.date' )
105- echo " 🧾 Latest commit on main: $COMMIT_SHA "
106- echo " 📝 Message: $COMMIT_MESSAGE "
107- echo " 👤 Author: $COMMIT_AUTHOR "
108- echo " 📅 Date: $COMMIT_DATE "
109- else
110- echo " ⚠️ Could not fetch latest commit info from GitHub API."
111- fi
112- else
113- echo " ℹ️ Skipping commit info because 'jq' is not available."
114- fi
115- else
116- echo " ⬇️ Found version: $LATEST_TAG "
117- ZIP_URL=" https://github.com/$GITHUB_USER /$GITHUB_REPO /archive/refs/tags/$LATEST_TAG .zip"
118- # Remove 'v' prefix from tag for folder name
119- TAG_WITHOUT_V=" ${LATEST_TAG# v} "
120- TMP_FOLDER=" $GITHUB_REPO -$TAG_WITHOUT_V "
121- fi
122-
123- # Create temp dir
124- TMP_DIR=$( mktemp -d)
125- echo " TMP_DIR=$TMP_DIR "
126- cd " $TMP_DIR " || exit 1
127-
128- # Download and extract
129- echo " 📦 Downloading from $ZIP_URL "
130- wget -q " $ZIP_URL " -O latest.zip || curl -L " $ZIP_URL " -o latest.zip
131- unzip -q latest.zip
132-
133- # Check if the source directory exists
134- SOURCE_DIR=" $TMP_FOLDER /$TARGET_SUBFOLDER "
135- if [ ! -d " $SOURCE_DIR " ]; then
136- echo " ❌ Source directory not found: $SOURCE_DIR "
137- echo " Available directories in $TMP_FOLDER :"
138- ls -la " $TMP_FOLDER /"
139- exit 1
140- fi
141-
142- # Copy the integration folder to destination
143- mkdir -p " $DEST_FOLDER "
144- cp -r " $TMP_FOLDER /$TARGET_SUBFOLDER /" * " $DEST_FOLDER /"
145- chmod +x " $DEST_FOLDER /" update_integration.sh
146- echo " ✅ Integration updated at $DEST_FOLDER "
147-
148- # Cleanup
149- cd /
150- rm -rf " $TMP_DIR "
151-
152- # Restart Home Assistant if requested
153- if [ " $RESTART_HA " = true ]; then
154- echo " 🧪 Validating Home Assistant configuration..."
155- if ha core check; then
156- echo " ✅ Configuration is valid."
157- echo " 🔁 Restarting Home Assistant..."
158- ha core restart
159- echo " ✅ Restart command issued."
160- else
161- echo " ❌ Configuration is invalid. Skipping restart."
162- exit 1
163- fi
1+ #! /bin/bash
2+
3+ # ==== CONFIGURATION ====
4+ GITHUB_USER=" tomer-w"
5+ GITHUB_REPO=" ha-victron-mqtt"
6+ TARGET_SUBFOLDER=" custom_components/victron_mqtt"
7+ DEST_FOLDER=" /config/custom_components/victron_mqtt"
8+ # ========================
9+ # Usage:
10+ # Run the script without arguments to update the integration to the latest release.
11+ # Use the --restart flag to restart Home Assistant after updating.
12+ # Use the --main flag to force using main branch even if releases exist.
13+ # Use the --version <tag> flag to download a specific version (e.g., v1.0.0).
14+ # Use the --list-versions flag to list all available versions.
15+ # Examples:
16+ # bash update_integration.sh --restart
17+ # bash update_integration.sh --main
18+ # bash update_integration.sh --main --restart
19+ # bash update_integration.sh --version v1.0.0
20+ # bash update_integration.sh --version v1.0.0 --restart
21+ # bash update_integration.sh --list-versions
22+
23+ RESTART_HA=false
24+ USE_MAIN=false
25+ SPECIFIC_VERSION=" "
26+
27+ # Check for jq; if missing, warn and skip printing commit info when using main
28+ if ! command -v jq > /dev/null 2>&1 ; then
29+ echo " ⚠️ 'jq' not found in PATH. Commit info from GitHub will be skipped. To enable it, install jq."
30+ JQ_AVAILABLE=false
31+ else
32+ JQ_AVAILABLE=true
33+ fi
34+
35+ # Parse command line arguments
36+ for arg in " $@ " ; do
37+ case $arg in
38+ --restart)
39+ RESTART_HA=true
40+ ;;
41+ --main)
42+ USE_MAIN=true
43+ ;;
44+ --version)
45+ # Next argument is the version
46+ SPECIFIC_VERSION=" ${@: $((OPTIND+1)): 1} "
47+ shift
48+ ;;
49+ --list-versions)
50+ echo " 📋 Fetching available versions from GitHub..."
51+ if ! command -v jq > /dev/null 2>&1 ; then
52+ echo " ❌ 'jq' is required to list versions. Please install jq."
53+ exit 1
54+ fi
55+ RELEASES=$( curl -s " https://api.github.com/repos/$GITHUB_USER /$GITHUB_REPO /releases" | jq -r ' .[].tag_name' | head -20)
56+ if [ -z " $RELEASES " ]; then
57+ echo " ❌ Could not fetch releases from GitHub."
58+ exit 1
59+ fi
60+ echo " Available versions:"
61+ echo " $RELEASES "
62+ echo " "
63+ echo " 💡 Usage: bash update_integration.sh --version <version-tag>"
64+ exit 0
65+ ;;
66+ esac
67+ done
68+
69+ # Validate that --version and --main are not used together
70+ if [ -n " $SPECIFIC_VERSION " ] && [ " $USE_MAIN " = true ]; then
71+ echo " ❌ Error: Cannot use --version and --main flags together."
72+ exit 1
73+ fi
74+
75+ # Fetch latest release tag via GitHub API (unless --main flag is used or --version is specified)
76+ if [ -n " $SPECIFIC_VERSION " ]; then
77+ echo " 📌 Using specific version: $SPECIFIC_VERSION "
78+ LATEST_TAG=" $SPECIFIC_VERSION "
79+ elif [ " $USE_MAIN " = true ]; then
80+ echo " 🔀 Using main branch as requested (--main flag)"
81+ LATEST_TAG=" null"
82+ else
83+ LATEST_TAG=$( curl -s " https://api.github.com/repos/$GITHUB_USER /$GITHUB_REPO /releases/latest" | jq -r ' .tag_name // empty' )
84+ fi
85+
86+ if [ " $LATEST_TAG " == " null" ] || [ -z " $LATEST_TAG " ]; then
87+ if [ " $USE_MAIN " = true ]; then
88+ echo " 📥 Downloading from main branch"
89+ else
90+ echo " ⚠️ No releases found. Falling back to main branch."
91+ fi
92+ ZIP_URL=" https://github.com/$GITHUB_USER /$GITHUB_REPO /archive/refs/heads/main.zip"
93+ TMP_FOLDER=" $GITHUB_REPO -main"
94+ # If we're downloading from main, fetch and print the latest commit info
95+ if [ " $JQ_AVAILABLE " = true ]; then
96+ echo " 🔎 Fetching latest commit info for main..."
97+ # Query the GitHub commits API for the main branch
98+ COMMIT_INFO=$( curl -s " https://api.github.com/repos/$GITHUB_USER /$GITHUB_REPO /commits/main" )
99+ if [ -n " $COMMIT_INFO " ] && [ " $( echo " $COMMIT_INFO " | jq -r ' .sha // empty' ) " != " " ]; then
100+ COMMIT_SHA=$( echo " $COMMIT_INFO " | jq -r ' .sha' )
101+ # Show only the first line of the commit message
102+ COMMIT_MESSAGE=$( echo " $COMMIT_INFO " | jq -r ' .commit.message' | head -n1 | tr -d ' \r' )
103+ COMMIT_AUTHOR=$( echo " $COMMIT_INFO " | jq -r ' .commit.author.name' )
104+ COMMIT_DATE=$( echo " $COMMIT_INFO " | jq -r ' .commit.author.date' )
105+ echo " 🧾 Latest commit on main: $COMMIT_SHA "
106+ echo " 📝 Message: $COMMIT_MESSAGE "
107+ echo " 👤 Author: $COMMIT_AUTHOR "
108+ echo " 📅 Date: $COMMIT_DATE "
109+ else
110+ echo " ⚠️ Could not fetch latest commit info from GitHub API."
111+ fi
112+ else
113+ echo " ℹ️ Skipping commit info because 'jq' is not available."
114+ fi
115+ else
116+ echo " ⬇️ Found version: $LATEST_TAG "
117+ ZIP_URL=" https://github.com/$GITHUB_USER /$GITHUB_REPO /archive/refs/tags/$LATEST_TAG .zip"
118+ # Remove 'v' prefix from tag for folder name
119+ TAG_WITHOUT_V=" ${LATEST_TAG# v} "
120+ TMP_FOLDER=" $GITHUB_REPO -$TAG_WITHOUT_V "
121+ fi
122+
123+ # Create temp dir
124+ TMP_DIR=$( mktemp -d)
125+ echo " TMP_DIR=$TMP_DIR "
126+ cd " $TMP_DIR " || exit 1
127+
128+ # Download and extract
129+ echo " 📦 Downloading from $ZIP_URL "
130+ wget -q " $ZIP_URL " -O latest.zip || curl -L " $ZIP_URL " -o latest.zip
131+ unzip -q latest.zip
132+
133+ # Check if the source directory exists
134+ SOURCE_DIR=" $TMP_FOLDER /$TARGET_SUBFOLDER "
135+ if [ ! -d " $SOURCE_DIR " ]; then
136+ echo " ❌ Source directory not found: $SOURCE_DIR "
137+ echo " Available directories in $TMP_FOLDER :"
138+ ls -la " $TMP_FOLDER /"
139+ exit 1
140+ fi
141+
142+ # Copy the integration folder to destination
143+ mkdir -p " $DEST_FOLDER "
144+ cp -r " $TMP_FOLDER /$TARGET_SUBFOLDER /" * " $DEST_FOLDER /"
145+ chmod +x " $DEST_FOLDER /" update_integration.sh
146+ echo " ✅ Integration updated at $DEST_FOLDER "
147+
148+ # Cleanup
149+ cd /
150+ rm -rf " $TMP_DIR "
151+
152+ # Restart Home Assistant if requested
153+ if [ " $RESTART_HA " = true ]; then
154+ echo " 🧪 Validating Home Assistant configuration..."
155+ if ha core check; then
156+ echo " ✅ Configuration is valid."
157+ echo " 🔁 Restarting Home Assistant..."
158+ ha core restart
159+ echo " ✅ Restart command issued."
160+ else
161+ echo " ❌ Configuration is invalid. Skipping restart."
162+ exit 1
163+ fi
164164fi
0 commit comments