斐讯N1安装Home Assistant

默认分类 · 2022-04-03

其实种草树莓派很久了,搭Home Assistant,把小米和斐讯智能硬件连起来,省的米家+斐讯N个app来回切换

终于发现N1能装Linux了,耗电低,边挖矿边当盒子边当私人云盘边当树莓派,相当靠谱

微信图片_20180702181751.png

先看上篇,N1降级、刷官改、装Ubuntu,再正式开始

smzdm上有一篇,hassbian上有N篇,试了的多个都没成功,最后用如下这个成功了,当然也结合了别的教程,致敬原贴
https://bbs.hassbian.com/forum.php?mod=viewthread&tid=3956&highlight=ubuntu%2B16.04

自启动参考这个
https://post.smzdm.com/p/553097/

一、安装准备和安装

最新home assistant需要python3.5.3 以上,Ubuntu 16.04版系统自带是3.5.1,先升级3.6.5
我是在root下安装的,最后又移到了home下,所以新安装可以试试在home下安装

可以先查看版本,升级过程中记得查询版本,

python --version
python3 --version
pip --version
pip3 --version

我升级某个过程中出错查询发现之前之前升级的还没有显示最新的,重启再查询就ok了,但是Linux Deploy里的系统没法用命令行重启似乎,可以用遥控操作Linux Deploy停止再启动,其实最简单的办法是ssh登陆N1命令行重启,是N1不是Ubuntu,ip地址一样,但是官改2.0的ssh端口是22,用户名密码root/toor

重启N1命令

/system/xbin/reboot

正式开始

sudo apt-get install -y wget
sudo apt-get install make 
sudo apt-get install -y gcc

安装zlib,这里zlib解压文件似乎不同,注意

sudo wget http://www.zlib.net/zlib-1.2.11.tar.gz
sudo tar -xzvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
sudo ./configure
sudo make all
sudo make install
sudo make clean
sudo make distclean
cd ..

继续

sudo apt-get update
sudo apt-get install -y build-essential python-dev python-setuptools python-pip python-smbus
sudo apt-get install -y build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install -y zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install -y libssl-dev openssl
sudo apt-get install libffi-dev

安装pythone3.6.5

sudo wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
sudo tar -xzvf Python-3.6.5.tgz
cd Python-3.6.5
sudo ./configure
sudo make all
sudo make install
sudo make clean
sudo make distclean
cd ..

升级pip

sudo pip install --upgrade pip
sudo pip3 install --upgrade pip

升级完毕后再查询一次,确认版本如下

root@localhost:~# python --version
Python 2.7.11+
root@localhost:~# python3 --version
Python 3.6.5
root@localhost:~# pip --version
pip 10.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
root@localhost:~# pip3 --version
pip 10.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

安装虚拟环境:

sudo apt-get install -y python3-venv

Python 虚拟环境安装
Home Assistant 官方推荐使用 Python 虚拟环境安装 Home Assistant 以避免影响生产环境。

创建虚拟环境:

sudo python3 -m venv homeassistant

打开虚拟环境:

cd homeassistant

激活虚拟环境:

source bin/activate

安装 Home Assistant:

sudo python3 -m pip install --upgrade homeassistant

启动 Home Assistant 并打开网页:

hass --open-ui

等待几分钟让后台下载依赖,之后你便可以从局域网内任意终端浏览器访问 http://N1的 IP 地址:8123,进入 Home Asssitant

退出虚拟环境用如下命令:

deactivate

如果需要编辑 configuration.yaml 文件安装nano,前面已经安装过

sudo apt-get install -y nano

再进入虚拟环境

sudo nano ../.homeassistant/configuration.yaml

编辑好,Ctr+X, Y, Enter

下面是添加homekit插件教程,因为用安卓手机,所以自己没实测homekit安装部分,摘出来以后测试

添加homekit 插件,

sudo apt-get install libavahi-compat-libdnssd-dev

在configuration.yaml 文件添加homekit配置:

homekit:
  auto_start: true  #启动 HA 后自动启动 homekit 服务,默认开启
  port: 51827  #运行端口,默认51827
  filter:  #设备过滤
    include_domains:  #指定包含设备类型
    include_entities:  #指定包含设备
    exclude_domains:  #指定排除设备类型
    exclude_entities:  #指定排除设备
  entity_config:  #特殊设备设置
    alarm_control_panel:  #警报面板
      code:  #密码,这个密码不!是!PIN!码!

二、启动服务和开机自启动

第一步完成后虽然每次hass --open-ui都能访问,但是退出后就不能访问,所以要设置后台启动

按照前面说的教程,访问权限问题一直不ok,所以把homeassistant文件夹和.homeassistant文件夹移动到home下就OK了

mv -R /root/homeassistnat /home/samzz/
mv -R /root/.homeassistnat /home/samzz/homeassistant/

看各种教程和安装插件(斐讯啥的)都需要留意安装这里的安装目录

项目文档里查看自启动配置 https://home-assistant.io/docs/autostart/init.d/ 虚拟环境的在最后一段PYTHON VIRTUAL ENVIRONMENT

创建hass-daemon文件

sudo touch /etc/init.d/hass-daemon
sudo nano /etc/init.d/hass-daemon

把上面文档里的虚拟环境启动配置部分粘贴进去,记得要修改目录,修改 RUN_AS 和 PRE_EXEC 这两个值,直接贴上我修改后的,标记#这里、这里、这里

#!/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: [url=https://gist.github.com/naholyr/4275302#file-new-service-sh]https://gist.github.com/naholyr/4275302#file-new-service-sh[/url]
# 这里、这里、这里
PRE_EXEC="source /home/samzz/homeassistant/bin/activate;"
RUN_AS="homeassistant"
PID_FILE="/var/run/hass.pid"
# 这里、这里、这里
CONFIG_DIR="/home/samzz/homeassistant/.homeassistant"
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
 
start() {
  if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$PRE_EXEC hass $FLAGS $REDIRECT;"
  su -c "$CMD" $RUN_AS
  echo 'Service started' >&2
}
 
stop() {
    if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill $(cat "$PID_FILE")
  while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
  echo 'Service stopped' >&2
}
 
install() {
    echo "Installing Home Assistant Daemon (hass-daemon)"
    echo "999999" > $PID_FILE
    chown $RUN_AS $PID_FILE
    mkdir -p $CONFIG_DIR
    chown $RUN_AS $CONFIG_DIR
}
 
uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -fv "$PID_FILE"
    echo "Notice: The config directory has not been removed"
    echo $CONFIG_DIR
    update-rc.d -f hass-daemon remove
    rm -fv "$0"
    echo "Home Assistant Daemon has been removed. Home Assistant is still installed."
  fi
}
 
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  install)
    install
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|install|uninstall}"
esac

保存配置,然后执行以下

sudo chmod +x /etc/init.d/hass-daemon
sudo update-rc.d hass-daemon defaults
sudo service hass-daemon install

尝试手动启动服务

sudo /etc/init.d/hass-daemon start

或者这样

sudo service hass-daemon start

不加最后的start就知道都有啥命令可以使用了:start|stop|restart等

sudo service hass-daemon
Usage: /etc/init.d/hass-daemon {start|stop|restart|install|uninstall}

查看日志

tail /home/samzz/homeassistant/.homeassistant/home-assistant.log

添加开机自启动

sudo nano /etc/rc.local

最下方exit 0前添加如下

service hass-daemon start

OK,Home Assistant安装完毕并且自动启动

转自http://samzz.ml/index.php/archives/51/

N1 homeassistant
Theme Jasmine by Kent Liao