旧手机部署homeassistant

榨干旧手机性能,部署homeassistant做智能家居控制系统,填坑级教程

一、准备工作

旧手机一部,下载安装termux,termux相关配置见 旧手机做web服务器+文件服务器+下载机+语音助手 ,以下过程如果没有科学环境,没什么基础可能需要两三个小时。

二、安装python

安装python并安装所需依赖

pkg install python tur-repo clang  openssl libffi
# 更新已安装依赖
pkg upgrade 

三、安装homeassistant

  1. 安装

    # 建立虚拟环境
    python -m venv hass
    # 进入虚拟环境
    source hass/bin/activate
    # 安装
    pip install homeassistant==2023.6.3
    

    虚拟环境不建也可以,但是如果还需要跑其他项目,需要不同的python版本,或者把Termux中的python升级了都可能会影响hass的运行。

  2. 问题pip

    如果存在以下错误(lru 失败):

     Running setup.py install for lru-dict ... error
      error: subprocess-exited-with-error
    
      × Running setup.py install for lru-dict did not run successfully.
      │ exit code: 1
      ╰─> [14 lines of output]
          running install
          /data/data/com.termux/files/home/hass/lib/python3.10/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
            warnings.warn(
          running build
          running build_ext
          building 'lru' extension
          creating build
          creating build/temp.linux-aarch64-cpython-310
    
    • 运行一下命令安装即可:

      pip3.10 install git+https://github.com/amitdev/lru-dict@5013406c409a0a143a315146df388281bfb2172d
      

四、运行

  1. 带log运行

    hass -v
    
  2. 问题解决

    • 错误一(numpy编译失败):

      Unable to install package PyTurboJPEG==1.6.7: error: subprocess-exited-with-error
      
      × Building wheel for numpy (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [264 lines of output]
      Running from numpy source directory.
      setup.py:86: DeprecationWarning:
      
      `numpy.distutils` is deprecated since NumPy 1.23.0, as a result
      of the deprecation of `distutils` itself. It will be removed for
      Python >= 3.12. For older Python versions it will remain present.
      It is recommended to use `setuptools < 60.0` for those Python versions.
      For more details, see:
      https://numpy.org/devdocs/reference/distutils_status_migration.html
      ......
      SystemError: One of the required function to build numpy is not available (the list is ['sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'fabs', 'floor', 'ceil', 'sqrt', 'log10', 'log', 'exp', 'asin', 'acos', 'atan', 'fmod', 'modf', 'frexp', 'ldexp']).
      [end of output]
      

      直接ctrl+c结束运行,然后去官方的 git仓库 看下所需numpy版本,手动安装,比如我的是1.23.2

      MATHLIB="m" pip3.10 install numpy==1.23.2
      
    • 错误二(ha-av安装失败)

      Unable to install package ha-av==10.1.0:
      ......
      pkg-config could not find libraries ['avformat', 'avcodec', 'avdevice', 'avutil', 'avfilter', 'swscale', 'swresample']
      

      结束运行,先安装ffmpeg之后再安装ha-va

      pkg install ffmpeg
      
      pip install ha-av==10.1.0
      

注:如果控制台输出内容不是install 或者build,也不是error,记得打开浏览器访问你设备的http://ip:8123进行访问,,,他的日志中没有过于明确的启动成功提示!!!

五、配置

页面成功访问,就离成功不远了,以下是配置过程中可能遇到的一些问题

  • 错误一 :(创建时区信息时报错)

    Valid options can be found here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones for dictionary value @ data[’time_zone']

    # 执行,安装时区库
    pip install tzdata
    
  • 错误二:创建用户时报错(编译pycares库错误)

    ERROR (SyncWorker_1) [homeassistant.util.package] Unable to install package radios==0.1.1

    deps/c-ares/src/lib/ares_getnameinfo.c:300:15: error: call to undeclared function ‘getservbyport_r’; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] if (getservbyport_r(port, proto, &se, (void )tmpbuf, ^ deps/c-ares/src/lib/ares_getnameinfo.c:300:15: note: did you mean ‘getservbyport’? /data/data/com.termux/files/usr/include/netdb.h:262:17: note: ‘getservbyport’ declared here struct servent getservbyport(int __port_in_network_order, const char* __proto); ^ 1 error generated. error: command ‘/data/data/com.termux/files/usr/bin/aarch64-linux-android-clang’ failed with exit code 1

  • 解决方法:下载源码,修改配置后手动编译安装

     # 切换到home目录,便于查找
     cd ~
     # 下载源码
     wget https://github.com/saghul/pycares/archive/refs/tags/pycares-4.3.0.zip
     # 解压
     unzip pycares-4.3.0.zip
     # 进入目录找到配置文件
     cd pycares-pycares-4.3.0/deps/build-config/config_android/
     # 修改配置,注释//#define HAVE_GETSERVBYPORT_R 1这一行(前面加//注释),在131行左右
     vim ares_config.h
     # 返回该源码文件的主目录,执行python3.11 setup.py install命令编译安装,
     cd ../../../
     python setup.py install
    

    如果install报错:找不到文件,则还需要更新setuptools

    clang-16: error: no such file or directory: ‘deps/c-ares/src/lib/ares__addrinfo2hostent.c’ clang-16: error: no input files

    # 直接安装,若提示已存在,请卸载重装
    pip install setuptools
    

结束

  1. 再次hass -v运行,若正常启动,页面多点一点没什么输出什么错误,就可以进行自定义配置了,具体配置涉及到的东西很多,这里就不介绍了,推荐一些我参考的文章:

  2. # 后台运行homeassistant
    nohup hass > hass.log 2>&1 &
    # 清下缓存(非必要)
    pip cache purge
    
  3. # 退出虚拟环境
    deactivate
    
  • 我也第一次整这玩意儿,踩了不少坑,有问题欢迎评论区一块儿交流