Home Assistant 集成 OpenWrt 项目教程
1. 项目的目录结构及介绍
hass_openwrt/
├── custom_components/
│ └── openwrt/
│ ├── __init__.py
│ ├── sensor.py
│ ├── switch.py
│ ├── binary_sensor.py
│ ├── services.py
│ └── config_flow.py
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
└── hacs.json
custom_components/openwrt/
: 包含所有与 OpenWrt 集成的核心文件。__init__.py
: 初始化文件,负责注册组件。sensor.py
: 定义传感器实体。switch.py
: 定义开关实体。binary_sensor.py
: 定义二进制传感器实体。services.py
: 定义服务。config_flow.py
: 配置流文件,用于配置向导。
.gitignore
: Git 忽略文件。CHANGELOG.md
: 变更日志。LICENSE
: 许可证文件。README.md
: 项目说明文档。hacs.json
: HACS 配置文件。
2. 项目的启动文件介绍
项目的启动文件是 custom_components/openwrt/__init__.py
。该文件负责初始化 OpenWrt 集成,并注册传感器、开关和二进制传感器等实体。
# custom_components/openwrt/__init__.py
import logging
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
_LOGGER.info("Setting up OpenWrt integration")
# 注册传感器、开关和二进制传感器等实体
hass.helpers.discovery.load_platform('sensor', 'openwrt', {}, config)
hass.helpers.discovery.load_platform('switch', 'openwrt', {}, config)
hass.helpers.discovery.load_platform('binary_sensor', 'openwrt', {}, config)
return True
3. 项目的配置文件介绍
项目的配置文件主要是 configuration.yaml
文件,用于配置 Home Assistant 集成 OpenWrt 的相关参数。
# configuration.yaml
openwrt:
host: '192.168.1.1'
username: 'admin'
password: 'your_password'
sensors:
- type: 'clients'
- type: 'mesh_peers'
switches:
- type: 'wps'
binary_sensors:
- type: 'connectivity'
host
: OpenWrt 设备的 IP 地址。username
: 登录用户名。password
: 登录密码。sensors
: 配置传感器类型。switches
: 配置开关类型。binary_sensors
: 配置二进制传感器类型。
以上是 Home Assistant 集成 OpenWrt 项目的详细教程,涵盖了项目的目录结构、启动文件和配置文件的介绍。希望对您有所帮助!