Skip to content

Instantly share code, notes, and snippets.

@dale3h
Last active September 24, 2018 18:39
Show Gist options
  • Save dale3h/8cf94b896f30a30b86d17b49554ff66d to your computer and use it in GitHub Desktop.
Save dale3h/8cf94b896f30a30b86d17b49554ff66d to your computer and use it in GitHub Desktop.
Hass.io Local Add-on Reloader
#!/bin/bash
readonly BACKUP_DIR=/backup/speck
usage() {
echo "usage: $(basename "$0") addon_name" 2>&1
exit 1
}
addon_install() {
local addon="${1}"
hassio addons install --name "${addon}"
}
addon_uninstall() {
local addon="${1}"
hassio addons uninstall --name "${addon}"
}
addon_reload() {
hassio addons reload
}
addon_config_backup() {
local addon="${1}"
local backup_file
local result
local config
local ex_code
# Ensure backup directory exists
if [[ ! -d "${BACKUP_DIR}" ]]; then
mkdir -p "${BACKUP_DIR}"
fi
# Create backup file name
backup_file="${BACKUP_DIR}/${addon}.json"
# Get addon config from Hass.io
result=$(hassio addons info --name "${addon}")
ex_code="$?"
# Check for exit code
if [[ "${ex_code}" -ne 0 ]]; then
echo "fail (${result})"
return "${ex_code}"
fi
# Pull config from results
config=$(jq --raw-output ".data.options // empty" <<< "${result}")
# Test for empty config
if [[ -z "${config}" ]]; then
echo "fail (Empty config)"
return 1
fi
# Save config to file
echo "${config}" > "${backup_file}"
echo "ok"
}
addon_config_restore() {
local addon="${1}"
local backup_file="${BACKUP_DIR}/${addon}.json"
local config
local options
local ex_code
# Check if backup file exists
if [[ ! -f "${backup_file}" ]]; then
echo "fail (Missing backup file)"
return 1
fi
# Read backup file
config=$(jq --compact-output "." "${backup_file}")
options="{\"options\":${config}}"
# Restore config
curl -sS -X "POST" -H "X-HASSIO-KEY: ${HASSIO_TOKEN}" --data-binary "${options}" "http://hassio/addons/${addon}/options"
ex_code="$?"
echo
return "${ex_code}"
}
main() {
local addon="${1}"
if [[ -z "${addon}" ]]; then
usage
fi
echo -n "Backup config: "
addon_config_backup "${addon}"
echo -n "Uninstall ${addon}: "
addon_uninstall "${addon}" || echo
echo -n "Reload addons: "
addon_reload || exit 1
echo -n "Install ${addon}: "
addon_install "${addon}" || exit 1
echo -n "Restore config: "
addon_config_restore "${addon}"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "${@}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment