After recently upgrading to Home Assistant 2022.2, I noticed that the service was no longer starting automatically within my FreeBSD jail (the underlying OS for TrueNAS). Investigating further, I could see that the service was failing to start because the --pid-file & --daemon parameters were no longer supported. Looks like it's time to rewrite the startup script 🙂

Now that Home Assistant no longer supports creating its own daemon, it's time to turn to the daemon utility to provide that functionality. The utility spawns the underlying hass (python) process and stores the PID so that everything can be closed down correctly.

After a bit of playing around, I ended up with the following code. When I started things up again, it still continued to fail, now I'm getting an error that my OS isn't supported. Trawling through docs, I found that there is a new parameter --ignore-os-check to disable this. Added this to the script, and everything is back up and running 🙂

#!/bin/sh
# PROVIDE: hass
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add these lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# hass_enable (bool):      Set to NO by default.
#                          Set it to YES to enable home-assistant.
# hass_user (user):        Set to hass by default.
# hass_group (group):      Set to hass by default.
# hass_config_dir (path):  Set to /usr/local/etc/home-assistant/ by default.
. /etc/rc.subr
name=hass
rcvar=${name}_enable
load_rc_config $name
: ${hass_enable:=NO}
: ${hass_user:=hass}
: ${hass_group:=hass}
: ${hass_config_dir:=/usr/local/etc/home-assistant/}
logfile="/var/log/${name}_daemon.log"
pidfile="/var/run/${name}_daemon.pid"
pidfile_child="/var/run/${name}.pid"
hass_command="/usr/local/bin/hass -v --ignore-os-check --config $hass_config_dir"
command="/usr/sbin/daemon"
start_precmd=${name}_start_precmd
start_postcmd=${name}_start_postcmd
stop_postcmd=${name}_stop_postcmd
status_cmd=${name}_status
hass_start_precmd() {
  install -g "${hass_group}" -m 664 -o ${hass_user} -- /dev/null "${logfile}"
  install -g "${hass_group}" -m 664 -o ${hass_user} -- /dev/null "${pidfile}"
  install -g "${hass_group}" -m 664 -o ${hass_user} -- /dev/null "${pidfile_child}"
  rc_flags="-r -f -o ${logfile} -P ${pidfile} -p ${pidfile_child} ${hass_command} ${rc_flags}"
}
hass_start_postcmd() {
	sleep 1 ; run_rc_command status
}
hass_stop_postcmd() {
  if [ -e "${pidfile_child}" ]; then
    echo "Stopping ${name} process."
    kill -s TERM `cat ${pidfile_child}`
  fi  
  rm -f -- "${pidfile}"
  rm -f -- "${pidfile_child}"
}
hass_status() {
  if [ -n "${rc_pid}" ]; then
	echo "${name} is run as as pid ${rc_pid}."
  else
	echo "${name} is not running."
	return 1
  fi
}
run_rc_command "$1"