Update homeassistant-integration.md

This commit is contained in:
Alireza Jahanshahlou 2025-10-12 17:32:34 +03:30 committed by GitHub
parent 35d51f38d7
commit 5fcec20166
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,186 +1,186 @@
# 小智ESP32-开源服务端与HomeAssistant集成指南
# Xiaozhi ESP32-Open Source Server and HomeAssistant Integration Guide
[TOC]
-----
## 简介
## Introduction
本文档将指导您如何将ESP32设备与HomeAssistant进行集成。
This document will guide you on how to integrate your ESP32 device with HomeAssistant.
## 前提条件
Prerequisites
- 已安装并配置好`HomeAssistant`
- 本次我选择的模型是:免费的ChatGLM,它支持functioncall函数调用
- HomeAssistant installed and configured
- The model I chose this time is: free ChatGLM, which supports functioncall function call
## 开始前的操作(必要)
## Pre-start operations (required)
### 1. 获取HA的网络网络地址信息
### 1. Get HA's network address information
请访问你Home Assistant的网络地址,例如,我的HA的地址是192.168.4.7,端口则是默认的8123,则在浏览器打开
Please visit your Home Assistant network address, for example, my HA address is 192.168.4.7, the port is the default 8123, then open it in the browser
```
http://192.168.4.7:8123
```
> 手动查询 HA 的 IP 地址方法**(仅限小智esp32-server和HA部署在同一个网络设备[例如同一个wifi]下)**:
> Manually query the IP address of HA** (only applicable when Xiaozhi esp32-server and HA are deployed on the same network device [such as the same wifi])**:
>
> 1. 进入 Home Assistant(前端)。
> 1. Enter Home Assistant (frontend).
>
> 2. 点击左下角 **设置(Settings)** → **系统(System)** → **网络(Network)**。
2. Click **Settings** → **System** → **Network** in the lower left corner.
>
> 3. 滑到最底部`Home Assistant 网址(Home Assistant website)`区域,在`本地网络(local network)`中,点击`眼睛`按钮,可以看到当前使用的 IP 地址(如 `192.168.1.10`)和网络接口。点击`复制连接(copy link)`可以直接复制。
3. Scroll down to the Home Assistant website area at the bottom. Under Local network, click the eye button to see the current IP address (e.g., 192.168.1.10) and network interface. Click Copy link to copy the link.
>
> ![image-20250504051716417](images/image-ha-integration-01.png)
> ![image-20250504051716417](images/image-ha-integration-01.png)
或,您已经设置了直接可以访问的Home Assistant的OAuth地址,您也可以在浏览器内直接访问
Alternatively, if you have set up a Home Assistant OAuth address that you can access directly, you can also access it directly in your browser.
```
http://homeassistant.local:8123
```
### 2. 登录`Home Assistant`拿到开发密钥
### 2. Log in to `Home Assistant` and get the development key
登录`HomeAssistant`,点击`左下角头像 -> 个人`,切换`安全`导航栏,划到底部`长期访问令牌`生成api_key,并复制保存,后续的方法都需要使用这个api key且仅出现一次(小tips: 您可以保存生成的二维码图像,后续可以扫描二维码再此提取api key)。
Log in to HomeAssistant, click your profile picture in the lower left corner -> Personal, switch to the Security navigation bar, scroll to the bottom of the page to find the Long-term access token to generate an api_key, and copy and save it. This api key will only appear once in all subsequent methods (tip: you can save the generated QR code image and scan it later to retrieve the api key).
## 方法1:小智社区共建的HA调用功能
## Method 1: HA call function built by Xiaozhi community
### 功能描述
### Functional Description
- 如您后续需要增加新的设备,该方法需要手动重启`xiaozhi-esp32-server服务端`以此更新设备信息**(重要**)。
- If you need to add new devices later, this method requires manually restarting the `xiaozhi-esp32-server` to update the device information** (Important**).
- 需要您确保已经在HomeAssistant中集成`Xiaomi Home`,并将米家的设备导入进`HomeAssistant`。
- You need to make sure you have integrated Xiaomi Home into HomeAssistant and imported your Xiaomi devices into HomeAssistant.
- 需要您确保`xiaozhi-esp32-server智控台`能正常使用。
- You need to ensure that the `xiaozhi-esp32-server intelligent console` can be used normally.
- 我的`xiaozhi-esp32-server智控台`和`HomeAssistant`部署在同一台机器的另一个端口,版本是`0.3.10`
- My `xiaozhi-esp32-server intelligent control console` and `HomeAssistant` are deployed on another port of the same machine, and the version is `0.3.10`
```
http://192.168.4.7:8002
```
### 配置步骤
### Configuration steps
#### 1. 登录`HomeAssistant`整理需要控制的设备清单
#### 1. Log in to `HomeAssistant` and organize the list of devices that need to be controlled
登录`HomeAssistant`,点击`左下角的设置`,然后进入`设备与服务`,再点击顶部的`实体`。
Log in to HomeAssistant, click Settings in the lower left corner, then go to Devices & Services, and then click Entities at the top.
然后在实体中搜索你相关控制的开关,结果出来后,在列表中,点击其中一个结果,这是会出现一个开关的界面。
Then search for the switch you want to control in the entity. When the results come out, click one of the results in the list, and a switch interface will appear.
在开关的界面,我们尝试点击开关,看看是开发会随着我们的点击开/关。如果能操作,说明是正常联网的。
On the switch interface, try clicking the switch to see if the development will turn on/off with our click. If it works, it means the network is working properly.
接着在开关面板找到设置按钮,点击后,可以查看这个开关的`实体标识符`。
Then find the settings button in the switch panel and click it to view the `Entity Identifier` of this switch.
我们打开一个记事本,按照这样格式整理一条数据:
We open a notepad and organize a piece of data in this format:
位置+英文逗号+设备名称+英文逗号+`实体标识符`+英文分号
Location + comma + device name + comma + `entity identifier` + semicolon
例如,我在公司,我有一个玩具灯,他的标识符是switch.cuco_cn_460494544_cp1_on_p_2_1,那么就这个写这一条数据
For example, I have a toy lamp in my company, and its identifier is switch.cuco_cn_460494544_cp1_on_p_2_1, so I write this data
```
公司,玩具灯,switch.cuco_cn_460494544_cp1_on_p_2_1;
Company,toy lamp,switch.cuco_cn_460494544_cp1_on_p_2_1;
```
当然最后我可能要操作两个灯,我的最终的结果是:
Of course, I may have to operate two lights in the end, and my final result is:
```
公司,玩具灯,switch.cuco_cn_460494544_cp1_on_p_2_1;
公司,台灯,switch.iot_cn_831898993_socn1_on_p_2_1;
Company,toy lamp,switch.cuco_cn_460494544_cp1_on_p_2_1;
company,desk lamp,switch.iot_cn_831898993_socn1_on_p_2_1;
```
这段字符,我们称为“设备清单字符”需要保存好,等一下有用。
This string, which we call "device list string", needs to be saved as it will be useful later.
#### 2. 登录`智控台`
#### 2. Log in to the Smart Control Panel
![image-20250504051716417](images/image-ha-integration-06.png)
使用管理员账号,登录`智控台`。在`智能体管理`,找到你的智能体,再点击`配置角色`。
Log in to the Smart Console using an administrator account. In Agent Management, find your agent and click Configure Role.
将意图识别设置成`函数调用`或`LLM意图识别`。这时你会看到右侧有一个`编辑功能`。点击`编辑功能`按钮,会弹出`功能管理`的框。
Set intent recognition to `Function Call` or `LLM Intent Recognition`. You will see an `Edit Function` button on the right. Click the `Edit Function` button and the `Function Management` dialog box will pop up.
在`功能管理`的框里,你需要勾选`HomeAssistant设备状态查询`和`HomeAssistant设备状态修改`。
In the `Feature Management` box, you need to check `HomeAssistant device status query` and `HomeAssistant device status modification`.
勾选后,在`已选功能`点击`HomeAssistant设备状态查询`,然后在`参数配置`里配置你的`HomeAssistant`地址、密钥、设备清单字符。
After checking, click `HomeAssistant Device Status Query` in `Selected Functions`, and then configure your `HomeAssistant` address, key, and device list string in `Parameter Configuration`.
编辑好后,点击`保存配置`,这时`功能管理`的框会隐藏,这时你再点击保存智能体配置。
After editing, click `Save Configuration`, then the `Function Management` box will be hidden, and you can click Save Agent Configuration again.
保存成功后,即可唤醒设备操作。
After successful saving, you can wake up the device for operation.
#### 3. 唤醒设别进行控制
#### 3. Wake up the device for control
尝试和esp32说,“打开XXX灯”
Try saying to the ESP32, "Turn on XXX light"
## 方法2:小智将Home Assistant的语音助手作为LLM工具
## Method 2: Xiaozhi uses Home Assistant's voice assistant as an LLM tool
### 功能描述
### Functional Description
- 该方法有一个比较严重的缺点——**该方法无法使用小智开源生态的function_call插件功能的能力**,因为使用Home Assistant作为小智的LLM工具会将意图识别能力转让给Home Assistant。但是**这个方法是能体验到原生的Home Assistant操作功能,且小智的聊天能力不变**。如实在介意可以使用同样是Home Assistant支持的[方法3](##方法3:使用Home Assistant的MCP服务(推荐)),能够最大程度体验到Home Assistant的功能。
This method has a significant drawback: it doesn't utilize the "function_call" plugin functionality within Xiaozhi's open-source ecosystem. Using Home Assistant as Xiaozhi's LLM tool transfers intent recognition capabilities to Home Assistant. However, this method allows you to experience native Home Assistant functionality without sacrificing Xiaozhi's chat capabilities. If this is a concern, you can use [Method 3](##Method 3: Using Home Assistant's MCP Service (Recommended)), which is also supported by Home Assistant and maximizes the functionality of Home Assistant.
### 配置步骤:
### Configuration steps:
#### 1. 配置Home Assistant的大模型语音助手。
#### 1. Configure Home Assistant's large model voice assistant.
**需要您提前配置好Home Assistant的语音助手或大模型工具。**
**You need to configure Home Assistant voice assistant or big model tool in advance.**
#### 2. 获取Home Assistant的语言助手的Agent ID.
#### 2. Get the Agent ID of Home Assistant's voice assistant.
1. 进入Home Assistant页面内。左侧点击`开发者助手`。
2. 在打开的`开发者助手`内,点击`动作`选项卡(如图示操作1),在页面内的选项栏`动作`中,找到或输入`conversation.process(对话-处理)`并选择`对话(conversation): 处理`(如图示操作2)。
1. Enter Home Assistant. Click Developer Assistant on the left.
2. In the Developer Assistant, click the Actions tab (see step 1). In the Actions section, find or enter conversation.process and select Conversation: Process (see step 2).
![image-20250504043539343](images/image-ha-integration-02.png)
3. 在页面内勾选`代理(agent)`选项,在变成常亮的`对话代理(conversation agent)`内选择您步骤一配置好的语音助手名称,如图示,我这边配置好的是`ZhipuAi`并选择。
3. Check the "Agent" option on the page. In the "Conversation Agent" box that lights up, select the voice assistant you configured in step 1. As shown in the picture, I configured "ZhipuAi" and selected it.
![image-20250504043854760](images/image-ha-integration-03.png)
4. 选中后,点击表单左下方的`进入YAML模式`。
4. After selecting, click `Enter YAML mode` in the lower left corner of the form.
![image-20250504043951126](images/image-ha-integration-04.png)
5. 复制其中的agent-id的值,例如图示中我的是`01JP2DYMBDF7F4ZA2DMCF2AGX2`(仅供参考)。
5. Copy the agent-id value. For example, mine is `01JP2DYMBDF7F4ZA2DMCF2AGX2` (for reference only).
![image-20250504044046466](images/image-ha-integration-05.png)
6. 切换到小智开源服务端`xiaozhi-esp32-server`的`config.yaml`文件内,在LLM配置中,找到Home Assistant,设置您的Home Assistant的网络地址,Api key和刚刚查询到的agent_id。
7. 修改`config.yaml`文件内的`selected_module`属性的`LLM`为`HomeAssistant`,`Intent`为`nointent`。
8. 重启小智开源服务端`xiaozhi-esp32-server`即可正常使用。
6. Switch to the `config.yaml` file of the Xiaozhi open source server `xiaozhi-esp32-server`. In the LLM configuration, find Home Assistant and set your Home Assistant network address, API key, and the agent_id you just queried.
7. Change the `LLM` of the `selected_module` property in the `config.yaml` file to `HomeAssistant` and `Intent` to `nointent`.
8. Restart the Xiaozhi open source server `xiaozhi-esp32-server` and it will work normally.
## 方法3:使用Home Assistant的MCP服务(推荐)
## Method 3: Use Home Assistant's MCP service (recommended)
### 功能描述
### Functional Description
- 需要您提前在Home Assistant内集成并安装好HA集成——[Model Context Protocol Server](https://www.home-assistant.io/integrations/mcp_server/)。
- You need to integrate and install the HA integration [Model Context Protocol Server](https://www.home-assistant.io/integrations/mcp_server/) in Home Assistant in advance.
- 这个方法与方法2都是HA官方提供的解决方法,与方法2不同的是,您可以正常使用小智开源服务端`xiaozhi-esp32-server`的开源共建的插件,同时允许您随意使用任何一个支持function_call功能的LLM大模型。
- This method and method 2 are both official solutions provided by HA. Unlike method 2, you can use the open source plug-in of Xiaozhi's open source server `xiaozhi-esp32-server` normally, and you can also use any LLM model that supports the function_call function.
### 配置步骤
### Configuration steps
#### 1. 安装Home Assistant的MCP服务集成。
#### 1. Install Home Assistant's MCP service integration.
集成官方网址——[Model Context Protocol Server](https://www.home-assistant.io/integrations/mcp_server/)。。
Integration official website - [Model Context Protocol Server](https://www.home-assistant.io/integrations/mcp_server/).
或跟随以下手动操作。
Or follow the manual steps below.
> - 前往Home Assistant页面的**[设置 > 设备和服务(Settings > Devices & Services.)](https://my.home-assistant.io/redirect/integrations)**。
Go to Settings > Devices & Services. on your Home Assistant page.
>
> - 在右下角,选择 **[添加集成(Add Integration)](https://my.home-assistant.io/redirect/config_flow_start?domain=mcp_server)**按钮。
In the lower right corner, select the [Add Integration](https://my.home-assistant.io/redirect/config_flow_start?domain=mcp_server) button.
>
> - 从列表中选择**模型上下文协议服务器(Model Context Protocol Server)**。
> - Select **Model Context Protocol Server** from the list.
>
> - 按照屏幕上的说明完成设置。
> - Follow the on-screen instructions to complete the setup.
#### 2. 配置小智开源服务端MCP配置信息
#### 2. Configure MCP configuration information for Xiaozhi Open Source Server
进入`data`目录,找到`.mcp_server_settings.json`文件。
Go to the `data` directory and find the `.mcp_server_settings.json` file.
如果你的`data`目录下没有`.mcp_server_settings.json`文件,
- 请把在`xiaozhi-server`文件夹根目录的`mcp_server_settings.json`文件复制到`data`目录下,并重命名为`.mcp_server_settings.json`
- 或[下载这个文件](https://github.com/xinnan-tech/xiaozhi-esp32-server/blob/main/main/xiaozhi-server/mcp_server_settings.json),下载到`data`目录下,并重命名为`.mcp_server_settings.json`
If there is no `.mcp_server_settings.json` file in your `data` directory,
- Please copy the `mcp_server_settings.json` file in the root directory of the `xiaozhi-server` folder to the `data` directory and rename it to `.mcp_server_settings.json`
- Or [download this file](https://github.com/xinnan-tech/xiaozhi-esp32-server/blob/main/main/xiaozhi-server/mcp_server_settings.json), download it to the `data` directory, and rename it to `.mcp_server_settings.json`
修改`"mcpServers"`里的这部分的内容:
Modify the following content in `"mcpServers`:
```json
"Home Assistant": {
@ -194,14 +194,14 @@ http://homeassistant.local:8123
},
```
注意:
Notice:
1. **替换配置:**
- 替换`args`内的`YOUR_HA_HOST`为您的HA服务地址,如果你的服务地址已经包含了https/http字样(例如`http://192.168.1.101:8123`),则只需要填入`192.168.1.101:8123`即可。
- 将`env`内`API_ACCESS_TOKEN`的`YOUR_API_ACCESS_TOKEN`替换成您之前获取到的开发密钥api key。
2. **如果你添加配置是在`"mcpServers"`的括号内后续没有新的`mcpServers`的配置时,需要把最后的逗号`,`移除**,否则可能会解析失败。
1. **Replace configuration:**
- Replace `YOUR_HA_HOST` in `args` with your HA service address. If your service address already contains https/http (for example, `http://192.168.1.101:8123`), just fill in `192.168.1.101:8123`.
- Replace `YOUR_API_ACCESS_TOKEN` in `API_ACCESS_TOKEN` in `env` with the development key API key you obtained earlier.
2. **If you add a configuration within the brackets of `"mcpServers"` and there is no subsequent `mcpServers` configuration, you need to remove the final comma `,`**, otherwise parsing may fail.
**最后效果参考以下(参考如下)**:
**The final effect is as follows (reference as follows)**:
```json
"mcpServers": {
@ -217,10 +217,10 @@ http://homeassistant.local:8123
}
```
#### 3. 配置小智开源服务端的系统配置
#### 3. Configure the system configuration of Xiaozhi open source server
1. **选择任意一款支持function_call的LLM大模型作为小智的LLM聊天助手(但不要选择Home Assistant作为LLM工具)**,本次我选择的模型是:免费的ChatGLM,它支持functioncall函数调用,但部分时候调用不太稳定,如果像追求稳定建议把LLM设置成:DoubaoLLM,使用的具体model_name是:doubao-1-5-pro-32k-250115。
1. **Choose any LLM model that supports function_call as Xiaozhi's LLM chat assistant (but do not choose Home Assistant as the LLM tool)**. This time I chose the free model: ChatGLM, which supports functioncall function calls, but the calls are sometimes unstable. If you want stability, I recommend setting the LLM to: DoubaoLLM, and the specific model_name used is: doubao-1-5-pro-32k-250115.
2. 切换到小智开源服务端`xiaozhi-esp32-server`的`config.yaml`文件内,设置您的LLM大模型配置,并且将`selected_module`配置的`Intent`调整为`function_call`。
2. Switch to the `config.yaml` file of the Xiaozhi open source server `xiaozhi-esp32-server`, set your LLM large model configuration, and adjust the `Intent` of the `selected_module` configuration to `function_call`.
3. 重启小智开源服务端`xiaozhi-esp32-server`即可正常使用。
3. Restart the Xiaozhi open source server `xiaozhi-esp32-server` and it will work normally.