Troy Prelog a4e6ff1e7a Update Freenas docs (#11585)
* updated rc.d script

Add extra_commands

	check_config - checks config using `hass --script check_config`
	upgrade - stops HA / upgrade / checks config / starts HA (only starts if config check passes)
	test - simple test to check directories / activate venv / check version of python and homeassistant
	restart - (modified) check_config / stop HA /start HA (only restart if config check passes)

NOTE: All extra_commands REQUIRE bash to be installed
	`pkg install bash`

I also removed the check_config from the pre_start function because because it will prevent HA from starting
if the configuration is missing (Like a clean install from example). NO BUENO!

Another case to consider haveing no configuration even after initial install is troubleshooting or testing.
For instance with rc.d script createing a fresh config is simple.

Let's suppose my working config is at
	`/home/hass/homeassistant`

Now to get a clean configuration I can just do this
	service homeassistant stop
	sysrc sysrc homeassistant_config_dir="/home/hass/ha_test_config"
	service homeassistant start

That's it! Configuration wise, it's a clean install. To switch back to working config I just
	service homeassistant stop
        sysrc sysrc homeassistant_config_dir="/home/hass/homeassistant"
        service homeassistant start

Awesome right?! But that doesn't work if check_config fails during pre_start

* add pkgs

These are not required to install HA but they are quickly missed once you start to actual use it. Let's just avoid
some fustration from the start. I don't think this list should be all inclusive but these basic things seem to be
frequently needed from the start.

	autoconf |
	gmake    | - looking at you Z-Wave, Stream, IKEA Tradfri
	pkgconf  |

	bash - Give me bash or give me death! Seriously, it makes life easier. There's not alot of *BSD focus
		around HA. I only use *BSD because of FreeNAS -- Typically (with the exception of jails) FreeNAS is webui.
		I'm ok with Linux cli and it's very similiar to FreeNAS but not the same. It's not bash. I don't think it
		needs to be for the root user either. But having bash installed and used by the HA user makes it easier
		to follow along with exising documentation for other virtualenv type installs when trying to further expand
		your HA installation

* give me bash or give me death!

Most people won't notice a differene but we'll know in our hearts we did the right thing.

* fix typo

I'm lucky I can spell my name

* whitespace

* use venv

Use the built-in venv instead of virtualenv which must be installed seperate.
2020-01-05 10:29:16 +01:00

9.2 KiB

title, description
title description
Installation on FreeNAS 11.2 Installation of Home Assistant on your FreeNAS.

FreeNAS is a free and open-source network-attached storage (NAS) software based on FreeBSD and the OpenZFS file system. It is licensed under the terms of the BSD License and runs on commodity x86-64 hardware.

This has been tested on FreeNAS 11.2 and should also work on FreeBSD 11.x as well. These instructions assume you already have a running and accessible jail. For more information on creating a jail read the official FreeNAS User Guide regarding Jails. Once you have the jail available, follow the steps below. Directories used follow standard BSD conventions but can be adjusted as you wish.

Enter the Home Assistant jail. If you don't know which name you have given the jail, you can use the iocage list command to check.

# If the jail is called 'HomeAssistant'
iocage exec HomeAssistant

Install the suggested packages:

pkg update
pkg upgrade
pkg install -y autoconf bash ca_root_nss gmake pkgconf python37 py37-sqlite3

Create the user and group that Home Assistant will run as. The user/group ID of 8123 can be replaced if this is already in use in your environment.

pw groupadd -n homeassistant -g 8123
echo 'homeassistant:8123:8123::::::/usr/local/bin/bash:' | adduser -f -

Create the installation directory:

mkdir -p /usr/local/share/homeassistant
chown -R homeassistant:homeassistant /usr/local/share/homeassistant

Create the virtualenv and install Home Assistant itself:

su homeassistant
cd /usr/local/share/homeassistant
python3.7 -m venv .
source ./bin/activate
pip3 install --upgrade pip
pip3 install homeassistant

While still in the venv, start Home Assistant to populate the configuration directory.

hass --open-ui

Wait until you see:

(MainThread) [homeassistant.core] Starting Home Assistant

Then escape and exit the venv.

deactivate
exit

Create the directory and the rc.d script for the system-level service that enables Home Assistant to start when the jail starts.

mkdir /usr/local/etc/rc.d/

Then create a file at /usr/local/etc/rc.d/homeassistant and insert the content below:

vi /usr/local/etc/rc.d/homeassistant
#!/bin/sh
#
# Based upon work by tprelog at https://github.com/tprelog/iocage-homeassistant/blob/11.3-RELEASE/overlay/usr/local/etc/rc.d/homeassistant
#
# PROVIDE: homeassistant
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# homeassistant_user: The user account used to run the homeassistant daemon.
#		This is optional, however do not specifically set this to an
#		empty string as this will cause the daemon to run as root.
#		Default: homeassistant
# homeassistant_group: The group account used to run the homeassistant daemon.
#		This is optional, however do not specifically set this to an
#		empty string as this will cause the daemon to run with group wheel.
#		Default: homeassistant
#
# homeassistant_venv: Directory where homeassistant virtualenv is installed.
#       Default:  "/usr/local/share/homeassistant"
#       Change:   `sysrc homeassistant_venv="/srv/homeassistant"`
#       UnChange: `sysrc -x homeassistant_venv`
#
# homeassistant_config_dir: Directory where homeassistant config is located.
#       Default:  "/home/homeassistant/.homeassistant"
#       Change:   `sysrc homeassistant_config_dir="/home/hass/homeassistant"`
#       UnChange: `sysrc -x homeassistant_config_dir`

# -------------------------------------------------------
# Copy this file to '/usr/local/etc/rc.d/homeassistant'
# `chmod +x /usr/local/etc/rc.d/homeassistant`
# `sysrc homeassistant_enable=yes`
# `service homeassistant start`
# -------------------------------------------------------

. /etc/rc.subr
name=homeassistant
rcvar=${name}_enable

pidfile_child="/var/run/${name}.pid"
pidfile="/var/run/${name}_daemon.pid"

load_rc_config ${name}
: ${homeassistant_enable:="NO"}
: ${homeassistant_user:="homeassistant"}
: ${homeassistant_group:="homeassistant"}
: ${homeassistant_config_dir:="/home/homeassistant/.homeassistant"}
: ${homeassistant_venv:="/usr/local/share/homeassistant"}

command="/usr/sbin/daemon"
extra_commands="check_config restart test upgrade"

start_precmd=${name}_precmd
homeassistant_precmd() {
    rc_flags="-f -o ${logfile} -P ${pidfile} -p ${pidfile_child} ${homeassistant_venv}/bin/hass --config ${homeassistant_config_dir} ${rc_flags}"
    [ ! -e "${pidfile_child}" ] && install -g ${homeassistant_group} -o ${homeassistant_user} -- /dev/null "${pidfile_child}"
    [ ! -e "${pidfile}" ] && install -g ${homeassistant_group} -o ${homeassistant_user} -- /dev/null "${pidfile}"
    [ -e "${logfile}" ] && rm -f -- "${logfile}"
    install -g ${homeassistant_group} -o ${homeassistant_user} -- /dev/null "${logfile}"
    if [ ! -d "${homeassistant_config_dir}" ]; then
      install -d -g ${homeassistant_group} -o ${homeassistant_user} -m 775 -- "${homeassistant_config_dir}"
    fi
}

stop_postcmd=${name}_postcmd
homeassistant_postcmd() {
    rm -f -- "${pidfile}"
    rm -f -- "${pidfile_child}"
}

upgrade_cmd="${name}_upgrade"
homeassistant_upgrade() {
    service ${name} stop
    su ${homeassistant_user} -c '
      source ${@}/bin/activate || exit 1
      pip3 install --upgrade homeassistant
      deactivate
    ' _ ${homeassistant_venv} || exit 1
    [ $? == 0 ] && homeassistant_check_config && service ${name} start
}

check_config_cmd="${name}_check_config"
homeassistant_check_config() {
    [ ! -e "${homeassistant_config_dir}/configuration.yaml" ] && return 0
    echo "Performing check on Home Assistant configuration:"
    #eval "${homeassistant_venv}/bin/hass --config ${homeassistant_config_dir} --script check_config"
    su ${homeassistant_user} -c '
      source ${1}/bin/activate || exit 2
      hass --config ${2} --script check_config || exit 3
      deactivate
    ' _ ${homeassistant_venv} ${homeassistant_config_dir}
}

restart_cmd="${name}_restart"
homeassistant_restart() {
    homeassistant_check_config || exit 1
    echo "Restarting Home Assistant"
    service ${name} stop
    service ${name} start
}

test_cmd="${name}_test"
homeassistant_test() {
    echo -e "\nTesting virtualenv...\n"
    [ ! -d "${homeassistant_venv}" ] && echo -e " NO DIRECTORY: ${homeassistant_venv}\n" && exit
    [ ! -f "${homeassistant_venv}/bin/activate" ] && echo -e " NO FILE: ${homeassistant_venv}/bin/activate\n" && exit

    ## switch users / activate virtualenv / get version
    su "${homeassistant_user}" -c '
      source ${1}/bin/activate || exit 2
      echo " $(python --version)" || exit 3
      echo " Home Assistant $(pip3 show homeassistant | grep Version | cut -d" " -f2)" || exit 4
      deactivate
    ' _ ${homeassistant_venv}

    [ $? != 0 ] && echo "exit $?"
}

load_rc_config ${name}
run_rc_command "$1"

Make the rc.d script executable:

chmod +x /usr/local/etc/rc.d/homeassistant

Configure the service to start on boot and start the Home Assistant service:

sysrc homeassistant_enable="YES"
service homeassistant start

You can also restart the jail to ensure that Home Assistant starts on boot.

USB Z-wave sticks may give dmesg warnings similar to "data interface 1, has no CM over data, has no break". This doesn't impact the function of the Z-Wave stick in Home Assistant. Just make sure the proper /dev/cu* is used in the Home Assistant configuration.yaml file.

Adding support for Z-wave stick

The following two packages need to be installed in the jail

pkg install gmake
pkg install libudev-devd

Then you can install the zwave package

su homeassistant
cd /usr/local/share/homeassistant
source ./bin/activate.csh
pip3 install homeassistant-pyozw==0.1.7
deactivate
exit

Stop the hass Jail

sudo iocage stop HomeAssistant

Edit the devfs rules on the Freenas Host

vi /etc/devfs.rules

Add the following lines

[devfsrules_jail_allow_usb=7]
add path 'cu\*' mode 0660 group 8123 unhide

Reload devfs

sudo service devfs restart

Edit the ruleset used by the jail in the Freenas GUI by going to Jails -> hass -> Edit -> Jail Properties -> devfs_ruleset Set it to 7

Start the hass jail

sudo iocage start HomeAssistant

Connect to the hass jail and verify that you see the modem devices

sudo iocage console HomeAssistant
ls /dev/cu*

This should output the following

/dev/cuau0      /dev/cuaU0

Add the zwave config to your configuration.yaml and restart HA

vi /home/homeassistant/.homeassistant/configuration.yaml
zwave:
  usb_path: /dev/cuaU0
  polling_interval: 10000
service homeassistant restart

Updating

Before updating, read the changelog to see what has changed and how it affects your Home Assistant instance. Enter the jail using iocage exec <jailname>. Stop the Home Assistant service:

service homeassistant stop

Then, enter the venv:

su homeassistant
cd /usr/local/share/homeassistant
source ./bin/activate

Upgrade Home Assistant:

pip3 install homeassistant --upgrade

Log out of the homeassistant user and start Home Assistant:

exit
service homeassistant start