樹莓派安裝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

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM