树莓派安装Home Assistant并添加自启动脚本
家里买了一堆小米的产品,无奈自己和老婆用的都是苹果,个人体验米家的APP用起来不怎么样。
于是乎想起来手里还有块闲置很多年的树莓派2B,可以安装Home Assistant把小米硬件桥接到IOS系统的家庭应用中。
系统用的是树莓派官方最精简的Raspbian 32位,也就是Debian啦。
ps:运行起来内存占用小于150M
安装Home Assistant安装官网的指引即可,链接如下。
https://www.home-assistant.io/installation/raspberrypi
桥接小米生态需要安装xiaomi_miot_raw插件,过程很简单,插件可以到Github上搜索。
折腾完的浏览器界面如下:
使用IPad作为枢纽,然后共享给家庭成员即可。
iPhone 界面如下:
遇到问题就是Home Assistant服务关机后不能自启动。
于是网上查了下找到了下边的脚本。
1、安装 daemonize
sudo apt install daemonize
2、新建脚本
sudo nano /etc/init.d/hass-daemon
3、脚本内容如下
#!/bin/sh ### BEGIN INIT INFO # Provides: hass # Required-Start: $local_fs $network $named $time $syslog # Required-Stop: $local_fs $network $named $time $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Description: Home\ Assistant ### END INIT INFO # /etc/init.d Service Script for Home Assistant # Created with: https://gist.github.com/naholyr/4275302#file-new-service-sh PRE_EXEC="cd /srv/homeassistant; python3.9 -m venv .; source bin/activate;" # Typically /usr/bin/hass HASS_BIN="/srv/homeassistant/bin/hass" RUN_AS="homeassistant" PID_DIR="/var/run/hass" PID_FILE="$PID_DIR/hass.pid" CONFIG_DIR="/home/$RUN_AS/.homeassistant" LOG_DIR="/var/log/homeassistant" LOG_FILE="$LOG_DIR/home-assistant.log" FLAGS="-v --config $CONFIG_DIR --log-file $LOG_FILE --log-rotate-days 7" DAEMONIZE="daemonize -c /srv/homeassistant -e $LOG_FILE.stderr -o $LOG_FILE.stdout -p $PID_FILE -l $PID_FILE -v" start() { create_piddir if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then echo 'Service already running' >&2 return 1 fi echo -n 'Starting service… ' >&2 local CMD="$PRE_EXEC $DAEMONIZE $HASS_BIN $FLAGS" su -s /bin/bash -c "$CMD" $RUN_AS if [ $? -ne 0 ]; then echo "Failed" >&2 else echo 'Done' >&2 fi } stop() { if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then echo 'Service not running' >&2 return 1 fi echo -n 'Stopping service… ' >&2 kill $(cat "$PID_FILE") while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done; rm -f $PID_FILE echo 'Done' >&2 } install() { echo "Installing Home Assistant Daemon (hass-daemon)" update-rc.d hass-daemon defaults create_piddir mkdir -p $CONFIG_DIR chown $RUN_AS $CONFIG_DIR mkdir -p $LOG_DIR chown $RUN_AS $LOG_DIR } uninstall() { echo "Are you really sure you want to uninstall this service? The INIT script will" echo -n "also be deleted! That cannot be undone. [yes|No] " local SURE read SURE if [ "$SURE" = "yes" ]; then stop remove_piddir echo "Notice: The config directory has not been removed" echo $CONFIG_DIR echo "Notice: The log directory has not been removed" echo $LOG_DIR update-rc.d -f hass-daemon remove rm -fv "$0" echo "Home Assistant Daemon has been removed. Home Assistant is still installed." fi } create_piddir() { if [ ! -d "$PID_DIR" ]; then mkdir -p $PID_DIR chown $RUN_AS "$PID_DIR" fi } remove_piddir() { if [ -d "$PID_DIR" ]; then if [ -e "$PID_FILE" ]; then rm -fv "$PID_FILE" fi rmdir -v "$PID_DIR" fi } case "$1" in start) start ;; stop) stop ;; install) install ;; uninstall) uninstall ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart|install|uninstall}" esac
4、给脚本运行权限
sudo chmod +x /etc/init.d/hass-daemon
5、安装服务
sudo service hass-daemon install
6、重启系统看下是否生效了。
reboot