Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
Last active November 24, 2019 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaneclaes/ca3e592d962b91a27917bdca08c011af to your computer and use it in GitHub Desktop.
Save zaneclaes/ca3e592d962b91a27917bdca08c011af to your computer and use it in GitHub Desktop.
Startup script for magic spellbook home automation with a Raspberry Pi kiosk and home-assistant.io
#!/bin/bash
#
# ARG_POSITIONAL_SINGLE([home-assistant-address],[root URL of home assistant])
# ARG_OPTIONAL_BOOLEAN([tvservice],[],[tvservice on],[on])
# ARG_OPTIONAL_BOOLEAN([usb],[],[USB on],[on])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.8.1 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
# Generated online by https://argbash.io/generate
die()
{
local _ret=$2
test -n "$_ret" || _ret=1
test "$_PRINT_HELP" = yes && print_help >&2
echo "$1" >&2
exit ${_ret}
}
begins_with_short_option()
{
local first_option all_short_options=''
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_tvservice="on"
_arg_usb="on"
print_help()
{
printf 'Usage: %s [--(no-)tvservice] [--(no-)usb] <home-assistant-address>\n' "$0"
printf '\t%s\n' "<home-assistant-address>: root URL of home assistant"
printf '\t%s\n' "--tvservice, --no-tvservice: tvservice on (on by default)"
printf '\t%s\n' "--usb, --no-usb: USB on (on by default)"
}
parse_commandline()
{
_positionals_count=0
while test $# -gt 0
do
_key="$1"
case "$_key" in
--no-tvservice|--tvservice)
_arg_tvservice="on"
test "${1:0:5}" = "--no-" && _arg_tvservice="off"
;;
--no-usb|--usb)
_arg_usb="on"
test "${1:0:5}" = "--no-" && _arg_usb="off"
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals_count=$((_positionals_count + 1))
;;
esac
shift
done
}
handle_passed_args_count()
{
local _required_args_string="'home-assistant-address'"
test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
test "${_positionals_count}" -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
}
assign_positional_args()
{
local _positional_name _shift_for=$1
_positional_names="_arg_home_assistant_address "
shift "$_shift_for"
for _positional_name in ${_positional_names}
do
test $# -gt 0 || break
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
shift
done
}
parse_commandline "$@"
handle_passed_args_count
assign_positional_args 1 "${_positionals[@]}"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash
# Trick Chromium into thinking everything exited cleanly
sed -i 's/"exited_cleanly": false/"exited_cleanly": true/' ~/.config/chromium/Default/Preferences
# Start unclutter (hide mouse)
unclutter -idle 0.5 -root &
# Turn off USB/ethernet (for security & power saving)
if [[ "$_arg_usb" == "off" ]]; then
usb_flag="0"
else
usb_flag="1"
fi
echo "Turning USB/ethernet $_arg_usb"
echo "${usb_flag}" | sudo tee /sys/devices/platform/soc/3f980000.usb/buspower >/dev/null
# Turn off HDMI
if [[ "$_arg_tvservice" == "off" ]]; then
echo "Turning TV service off."
sudo tvservice --off
fi
# Determine the room-name from the hostname
ROOM=$(hostname | sed -e "s/spellbook-//g")
# Start chromium
URL="$_arg_home_assistant_address/lovelace/${ROOM:-}?kiosk"
echo "Opening chromium to $URL"
/usr/bin/chromium-browser --noerrdialogs --disable-infobars --ignore-certificate-errors --kiosk "--app=$URL" &
# ] <-- needed because of Argbash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment