From b4643fa4085a713c9b958439148449b7a8c2fbcb Mon Sep 17 00:00:00 2001 From: impressionyang Date: Thu, 16 Apr 2026 16:25:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=94=A8=E4=BA=8E=E6=8E=92=E6=9F=A5=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. provisioning.py: 添加 start_scanning 调用日志 2. config_flow.py: 添加 coordinator 调用日志 3. serial_reader.py: 添加命令发送和接收的原始数据日志 --- .../sigmesh_gateway/config_flow.py | 4 +++ .../sigmesh_gateway/provisioning.py | 3 +++ .../sigmesh_gateway/serial_reader.py | 11 ++++++-- docs/配网使用指南.md | 26 ++++++++++++++++--- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/custom_components/sigmesh_gateway/config_flow.py b/custom_components/sigmesh_gateway/config_flow.py index a83aae6..ae55ce8 100644 --- a/custom_components/sigmesh_gateway/config_flow.py +++ b/custom_components/sigmesh_gateway/config_flow.py @@ -235,10 +235,14 @@ class SigMeshGatewayOptionsFlow(config_entries.OptionsFlow): if user_input is not None: # 调用配网管理器开始扫描 try: + _LOGGER.info("开始调用 start_scanning,DOMAIN 数据:%s", list(self.hass.data.get(DOMAIN, {}).keys())) for coordinator in self.hass.data.get(DOMAIN, {}).values(): if hasattr(coordinator, 'start_scanning'): + _LOGGER.info("调用 coordinator.start_scanning()") await coordinator.start_scanning() + _LOGGER.info("coordinator.start_scanning() 调用完成") except Exception as e: + _LOGGER.error("start_scanning 调用失败:%s", e, exc_info=True) self._errors["base"] = str(e) return await self.async_step_prov_action() return self.async_create_entry(title="", data={}) diff --git a/custom_components/sigmesh_gateway/provisioning.py b/custom_components/sigmesh_gateway/provisioning.py index bbb9ded..0cb0b81 100644 --- a/custom_components/sigmesh_gateway/provisioning.py +++ b/custom_components/sigmesh_gateway/provisioning.py @@ -130,6 +130,8 @@ class ProvisioningManager: async def start_scanning(self) -> None: """开始扫描设备.""" + _LOGGER.debug("start_scanning 被调用,当前状态:%s", self._state.value) + # 如果已经在扫描中,先重置状态(允许用户重新启动扫描) if self._state == ProvState.SCANNING: _LOGGER.info("扫描已在进行中,重置扫描状态...") @@ -148,6 +150,7 @@ class ProvisioningManager: # 发送扫描命令:AT+PROV=SCAN # 网关会开始扫描周围的配网设备,设备响应后通过串口上报 try: + _LOGGER.debug("准备发送扫描命令:AT+PROV=SCAN") await self.serial_reader.write_command("AT+PROV=SCAN") _LOGGER.info("已发送扫描命令,等待设备响应...") except Exception as e: diff --git a/custom_components/sigmesh_gateway/serial_reader.py b/custom_components/sigmesh_gateway/serial_reader.py index 9651d8c..f67b86a 100644 --- a/custom_components/sigmesh_gateway/serial_reader.py +++ b/custom_components/sigmesh_gateway/serial_reader.py @@ -272,6 +272,7 @@ class SerialReader: try: line = line_bytes.decode("utf-8").strip() _LOGGER.debug("串口接收:%s", line) + _LOGGER.debug("串口接收原始数据:%s", line_bytes.hex().upper()) self._parse_event_line(line) except UnicodeDecodeError as e: _LOGGER.warning("解码失败:%s, 错误:%s", line_bytes, e) @@ -299,8 +300,14 @@ class SerialReader: async def write_command(self, command: str) -> int: """写入 AT 命令.""" cmd_bytes = f"{command}\r\n".encode() - _LOGGER.debug("发送命令:%s", command) - return await self.write(cmd_bytes) + _LOGGER.debug("发送命令:%s (原始数据:%s)", command, cmd_bytes.hex()) + try: + result = await self.write(cmd_bytes) + _LOGGER.debug("命令发送成功,发送了 %d 字节", result) + return result + except Exception as e: + _LOGGER.error("命令发送失败:%s", e) + raise def list_serial_ports() -> list[dict[str, str]]: diff --git a/docs/配网使用指南.md b/docs/配网使用指南.md index 34dd694..d87574b 100644 --- a/docs/配网使用指南.md +++ b/docs/配网使用指南.md @@ -4,24 +4,44 @@ 本指南介绍如何使用 SigMesh Gateway 集成的配网功能,将 Bluetooth Mesh 设备添加到网络中。 +## 重要说明:配网参数来源 + +**配网参数(Network Key, App Key, Network ID, IV Index)是通过 danglo 工具设置到网关的,而不是从网关读取的。** + +- **首次使用网关**:必须先用 danglo 组网工具设置配网参数 +- **HA 配置时**:输入与 danglo 工具中相同的参数(或使用默认值) +- **原因**:网关芯片(Nordic nRF52840)写入参数后无法通过命令读取,需用户自行记录 + ## 配网流程 ### 第一步:首次配置 -添加集成时,需要配置以下配网参数: +#### 1. 使用 danglo 工具配置网关 + +添加集成前,先用亿佰特官方 danglo 组网工具配置网关: + +1. 打开 danglo 工具,连接 E104-BT12USP 网关 +2. 进入 **PROV** 选项卡 +3. 设置配网参数并点击 **PROV** 写入网关 + +> **提示**:建议记录这些参数,后续 HA 配置和其他网关需要使用相同参数 + +#### 2. 在 HA 中配置集成 + +添加集成时,需要配置以下参数: 1. **串口配置** - 选择串口设备(如 `/dev/ttyUSB0`) - 设置波特率(默认 115200) -2. **配网参数**(首次使用需要) +2. **配网参数**(必须与 danglo 工具中一致) - **Network Key**: 16 字节网络密钥(32 字符十六进制) - **App Key**: 16 字节应用密钥(32 字符十六进制) - **Network ID**: 2 字节网络 ID(4 字符十六进制) - **IV Index**: 4 字节 IV 索引(整数) - **组地址起始值**: 组地址范围起始(默认 0xC000) -> **注意**: 如果网关已经配置过,可以使用默认值(全 0)。 +> **注意**: 如果网关已经配置过且参数已知,可以在 HA 配置时直接输入;如果不确定,可以使用默认值(全 0),但需确保网关中的参数与 HA 配置一致 ### 第二步:扫描设备