HomeAssistant入门指南:开启物联网之旅

文章目录

  • 一、什么是home-assistant?
  • 1.核心架构
  • 2.集成架构
  • 二、在树梅派5上安装home-assistant
  • 三、接入HACS
  • 四、接入米家
  • 1.对比下趋势
  • 2.手动安装插件
  • 3.配置方式
  • 五、接入公牛
  • 1.手动安装插件
  • 2.配置方式
  • 六、接入海尔
  • 1.手动安装插件
  • 2.配置方式
  • 七、接入国家电网

  • 一、什么是home-assistant?

    Open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts.
    一款将本地控制与隐私置于首位的开源家庭自动化解决方案,由全球各地的 DIY 爱好者与技术极客玩家社区共同驱动。

    1.核心架构

    Home Assistant 核心由四个主要部分组成。除此之外,它还包含许多辅助类用于处理常见场景,例如提供实体或处理位置信息。

  • 事件总线(Event Bus):负责协调事件的触发与监听 —— 这是 Home Assistant 的核心命脉。
  • 状态机(State Machine):跟踪各类实体的状态,并在状态变更时触发 state_changed 事件。
  • 服务注册表(Service Registry):在事件总线上监听 call_service 事件,并允许其他代码注册服务操作。
  • 定时器(Timer):每秒在事件总线上发送一次 time_changed 事件。

    来源:https://developers.home-assistant.io/docs/architecture/core
  • 2.集成架构

    Home Assistant Core 可以通过 集成(integrations) 进行扩展。每个集成负责 Home Assistant 中的一个特定 领域(domain)(如灯光、传感器、媒体设备等)。集成能够监听或触发事件、提供操作功能,并维护实体状态。
    集成由两部分组成:组件(component)(基础逻辑)和 平台(platforms)(与其他集成交互的模块)。集成使用 Python 编写,能够充分利用 Python 的所有优势。开箱即用的 Home Assistant 提供了大量内置集成,覆盖常见的智能家居设备和服务。

    来源:https://developers.home-assistant.io/docs/architecture_components

    二、在树梅派5上安装home-assistant

    https://www.home-assistant.io/installation/
    https://github.com/home-assistant/operating-system/releases
    目前是在树梅派安装docker运行的home-assistant,后面打算采用HA OS。

    docker-compose up -d
    
    version: '3'
    services:
      # https://www.home-assistant.io/installation/linux#docker-compose
      homeassistant:
        container_name: homeassistant
        image: "homeassistant/home-assistant"
        environment:
          - TZ=Asia/Shanghai
        ports:
          - "8123:8123"
        volumes:
          - /root/homeassistant:/config
          - /etc/localtime:/etc/localtime:ro
          - /etc/hosts:/etc/hosts:ro
          - /run/dbus:/run/dbus:ro
        restart: unless-stopped
        privileged: true
        network_mode: host
    

    三、接入HACS

    Home Assistant 社区商店 ( HACS ) 是一种自定义集成,它提供了一个 UI 来管理Home Assistant中的自定义元素。

    HACS可以做什么:
    帮助您发现新的自定义元素。
    帮助您下载新的自定义元素。
    管理(更新/删除)自定义元素。
    发布您自己的自定义元素存储库并创建存储库或问题跟踪器的快捷方式。

    docker exec -it <name of the container running homeassistant> bash
    
    wget -O - https://get.hacs.xyz | bash -
    

    https://hacs.xyz/docs/use/download/download/#to-download-hacs-container

    四、接入米家

    好久没更新了:https://github.com/ha0y/xiaomi_miot_raw
    最近还在更新:https://github.com/al-one/hass-xiaomi-miot
    官方强势来袭:https://github.com/XiaoMi/ha_xiaomi_home

    1.对比下趋势

    https://ossinsight.io/analyze/XiaoMi/ha_xiaomi_home?vs=al-one%2Fhass-xiaomi-miot#overview
    还是建议用小米官方的

    2.手动安装插件

    git clone https://github.com/XiaoMi/ha_xiaomi_home
    cp -r ha_xiaomi_home/custom_components/xiaomi_home /root/homeassistant/custom_components
    

    3.配置方式

    安装完成后,重启 Home Assistant。待 Home Assistant 启动后,在「设置」菜单中点击「设备与服务」选项,在新界面中选择「添加集成」,搜索「xiaomi home」,按照提示操作即可。

    五、接入公牛

    1.手动安装插件

    https://github.com/stevenjoezhang/hass-iotbull

    git clone https://github.com/stevenjoezhang/hass-iotbull
    cp -r hass-iotbull/custom_components/bull /root/homeassistant/custom_components
    

    2.配置方式

    安装完成后,重启 Home Assistant。待 Home Assistant 启动后,在「设置」菜单中点击「设备与服务」选项,在新界面中选择「添加集成」,搜索「Bull IoT」,按照提示操作即可。

    六、接入海尔

    1.手动安装插件

    git clone https://github.com/banto6/haier
    cp -r haier/custom_components/haier /root/homeassistant/custom_components
    

    2.配置方式

    通过Stream抓包,搜索关键词:assign,点击查看json即可

    https://zhuanlan.zhihu.com/p/697084466

    七、接入国家电网

    这种感觉不太好:https://github.com/ARC-MX/sgcc_electricity_new/blob/master/ha_addons_doc/Add-on%E6%95%99%E7%A8%8B.md

    还是老本行,用docker吧

    services:
      sgcc_electricity_app:
        env_file:
          - .env
        image: registry.cn-hangzhou.aliyuncs.com/arcw/sgcc_electricity:latest # for use docker.io: arcw/sgcc_electricity:latest
        container_name: sgcc_electricity
        network_mode: "host"
        environment:
          - SET_CONTAINER_TIMEZONE=true
          - CONTAINER_TIMEZONE=Asia/Shanghai
        restart: unless-stopped
        volumes:
          - ./:/data        # if you want to read homeassistant.db, homeassistant.db is in the container at /data/
        command: python3 main.py
        init: true
    

    .env如下:

    ### 以下项都需要修改
    ## 国网登录信息
    # 修改为自己的登录账号
    PHONE_NUMBER="xxx" 
    # 修改为自己的登录密码
    PASSWORD="xxxx" 
    # 排除指定用户ID,如果出现一些不想检测的ID或者有些充电、发电帐号、可以使用这个环境变量,如果有多个就用","分隔,","之间不要有空格
    IGNORE_USER_ID=xxxxxxx,xxxxxxx,xxxxxxx
    
    # SQLite 数据库配置
    # or False 不启用数据库储存每日用电量数据。
    ENABLE_DATABASE_STORAGE=False
    # 数据库名,默认为homeassistant
    DB_NAME="homeassistant.db"
    
    ## homeassistant配置
    # 改为你的localhost为你的homeassistant地址
    HASS_URL="http://localhost:8123/" 
    # homeassistant的长期令牌
    HASS_TOKEN="eyxxxxx"
    
    ## selenium运行参数
    # 任务开始时间,24小时制,例如"07:00”则为每天早上7点执行,第一次启动程序如果时间晚于早上7点则会立即执行一次,每隔12小时执行一次。
    JOB_START_TIME="07:00"
    # 每次操作等待时间,推荐设定范围为[2,30],该值表示每次点击网页后所要等待数据加载的时间,如果出现“no such element”诸如此类的错误可适当调大该值,如果硬件性能较好可以适当调小该值
    RETRY_WAIT_TIME_OFFSET_UNIT=15
    
    
    ## 记录的天数, 仅支持填写 7 或 30
    # 国网原本可以记录 30 天,现在不开通智能缴费只能查询 7 天造成错误
    DATA_RETENTION_DAYS=7
    
    ## 余额提醒
    # 是否缴费提醒
    RECHARGE_NOTIFY=False
    # 余额
    BALANCE=5.0
    # pushplus token 如果有多个就用","分隔,","之间不要有空格
    PUSHPLUS_TOKEN=xxxxxxx,xxxxxxx,xxxxxxx
    

    作者:杜莱恩特Durant

    物联沃分享整理
    物联沃-IOTWORD物联网 » HomeAssistant入门指南:开启物联网之旅

    发表回复