Home Assistant 配置项目教程
1. 项目的目录结构及介绍
home-assistant-config/
├── automations/
├── blueprints/
├── custom_components/
├── groups/
├── home-assistant.log
├── home-assistant_v2.db
├── images/
├── scripts/
├── scenes/
├── secrets.yaml
├── sensor.yaml
├── switch.yaml
├── ui-lovelace.yaml
└── configuration.yaml
- automations/: 存放自动化脚本的目录。
- blueprints/: 存放蓝图配置的目录。
- custom_components/: 存放自定义组件的目录。
- groups/: 存放组配置的目录。
- home-assistant.log: Home Assistant 的日志文件。
- home-assistant_v2.db: Home Assistant 的数据库文件。
- images/: 存放图片资源的目录。
- scripts/: 存放脚本配置的目录。
- scenes/: 存放场景配置的目录。
- secrets.yaml: 存放敏感信息的配置文件。
- sensor.yaml: 传感器配置文件。
- switch.yaml: 开关配置文件。
- ui-lovelace.yaml: Lovelace UI 配置文件。
- configuration.yaml: 主配置文件。
2. 项目的启动文件介绍
Home Assistant 的启动文件是 configuration.yaml
。这个文件是 Home Assistant 的核心配置文件,包含了所有组件、自动化、脚本等的配置。启动 Home Assistant 时,系统会读取这个文件并根据其中的配置加载相应的组件和服务。
3. 项目的配置文件介绍
configuration.yaml
configuration.yaml
是 Home Assistant 的主配置文件,包含了所有组件、自动化、脚本等的配置。以下是一些常见的配置项:
homeassistant:
name: Home
latitude: 37.8136
longitude: -122.4766
elevation: 10
unit_system: metric
time_zone: America/Los_Angeles
# 示例:添加一个传感器
sensor:
- platform: template
sensors:
living_room_temperature:
friendly_name: "Living Room Temperature"
unit_of_measurement: "°C"
value_template: "22"
# 示例:添加一个自动化
automation:
- alias: "Turn on lights at sunset"
trigger:
- platform: sun
event: sunset
action:
- service: light.turn_on
entity_id: light.living_room
secrets.yaml
secrets.yaml
文件用于存放敏感信息,如 API 密钥、密码等。这些信息不会直接暴露在 configuration.yaml
中,而是通过引用 secrets.yaml
中的变量来使用。
# secrets.yaml 示例
api_key: "your_api_key_here"
wifi_password: "your_wifi_password_here"
在 configuration.yaml
中引用 secrets.yaml
中的变量:
# configuration.yaml 示例
api_key: !secret api_key
wifi_password: !secret wifi_password
ui-lovelace.yaml
ui-lovelace.yaml
是 Lovelace UI 的配置文件,用于定义 Home Assistant 的前端界面。以下是一个简单的示例:
title: Home
views:
- title: Living Room
cards:
- type: entities
title: Lights
entities:
- entity: light.living_room
- entity: switch.living_room_fan
通过这些配置文件,你可以自定义 Home Assistant 的行为和界面,以满足你的需求。