#!/usr/bin/env bash # Homeassistant core readonly toolName="homeassistant" readonly toolService="home-assistant" # Already defined by sequencer.sh, but may be overwritten #readonly seq_configName="${sq_scriptName:?}.cfg" #readonly seq_configTemplate="${seq_origin:?}/${sq_configName:?}.example" sq_aptOpt= sq_config=0 seq_config() { ## or to use sequencer api with global config file: if ! initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then # End if no configuration file exists dry || return 1 fi ## Apt cmdline option to suppress user interaction interactive || sq_aptOpt="-y" ## Return of non zero value will abort the sequence return 0 } step_4_info() { echo 'Create home-assistant venv'; } step_4_alias() { echo 'venv'; } step_4_options() { echo '[PYTHON BINARY]'; } step_4() { shift local pyBin="python3" [ -d "${sc_haVenv}" ] && die "${toolName} venv already exists" [ -f "${1:-}" ] && pyBin="${1}" exe mkdir -p "${sc_haVenv:?}" exe chown "${sc_haUser}:" "${sc_haVenv}" runAsHa createVenv "${pyBin}" "${sc_haVenv}" } # createVenv createVenv() { cd "${2:?}" && "${1:?}" -m venv . } step_20_info() { echo "Upgrade pip"; } step_20_alias() { echo 'upgradepip'; } step_20() { runAsHa updatePy "${sc_haVenv}" pip endReturn "Could not upgrade pip" } step_21_info() { echo "Upgrade $toolName core installation"; } step_21_alias() { echo "upgrade"; } step_21() { exe service "${toolService}" stop endReturn "Could not stop ${toolName}" runAsHa updatePy "${sc_haVenv}" homeassistant "${sc_haDeps[@]}" endReturn "Could not upgrade ${toolName}" exe service "${toolService}" start } # updateHa [OPTIONAL PIP PACKAGE ...] updatePy() { source "${1:?}/bin/activate" shift pip3 install --upgrade "$@" } step_30_info() { echo 'Upgrade venv to new python version'; } step_30_options() { echo ''; } step_30_alias() { echo 'upgradevenv'; } step_30() { shift local pyVer="${1:-}" local buDate="$(date +%Y%m%d-%H%M%S)" endIfEmpty pyVer "No python version given" exe service "${toolService}" stop endReturn "Could not stop ${toolName}" exe mv "${sc_haVenv}" "${sc_haVenv}_bu_${buDate}" 2>/dev/null || true local pyBin="$(command -v "python${pyVer}")" step venv "${pyBin}" } step_31_info() { echo "Install ${toolName} in venv"; } step_31() { runAsHa installHa "${sc_haVenv}" "${sc_haDeps[@]}" } installHa() { cd "${1:?}" && source "bin/activate" || return 1 shift python3 -m pip install wheel pip3 install homeassistant "$@" } step_32_info() { echo "Start $toolName in new venv"; } step_32() { exe service "${toolService}" start } # runAsHa [ARGS...] # Run given function as home-assistant user inside the virtual environment runAsHa() { local func="${1:?}" shift exe sudo -Hu "${sc_haUser}" bash -c "$(declare -f "${func}"); ${func} "'"$@"' "" "$@" } step_50_alias() { echo 'notes'; } step_50() { color green cat <