impress_sig_mesh_hacs/custom_components/sigmesh_gateway/config_flow.py
impressionyang 6a66c9b474 初始提交:SigMesh Gateway HACS 集成
项目结构:
- custom_components/sigmesh_gateway/ - Home Assistant 集成
  - serial_reader.py - 串口读取器
  - protocol_parser.py - 协议解析器
  - coordinator.py - 数据协调器
  - platforms/ - 传感器/开关/灯光/设备追踪实体

文档:
- PRD.md - 产品需求文档
- README.md - 用户使用指南
- 可行性分析.md - 技术可行性分析
- 参数配置表.md - 配置参数记录
- 调试检查清单.md - 问题排查指南

功能特性:
- 串口通信 (115200 波特率)
- Bluetooth Mesh 协议解析
- 支持 200+ 设备接入
- UI 配置界面
- 多平台实体支持

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 18:20:48 +08:00

98 lines
3.0 KiB
Python

"""SigMesh Gateway 配置流程."""
from __future__ import annotations
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import selector
from .const import (
CONF_SERIAL_DEVICE,
DEFAULT_BAUDRATE,
DEFAULT_NAME,
DOMAIN,
)
class SigMeshGatewayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""SigMesh Gateway 配置流程."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, any] | None = None
) -> FlowResult:
"""处理用户配置步骤."""
errors = {}
if user_input is not None:
# TODO: 验证串口连接
return self.async_create_entry(
title=user_input.get(CONF_SERIAL_DEVICE, DEFAULT_NAME),
data=user_input,
)
# 获取可用串口列表
try:
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
port_list = [
selector.SelectOptionDict(value=p.device, label=f"{p.device} - {p.description}")
for p in ports
]
except Exception:
port_list = [
selector.SelectOptionDict(value="/dev/ttyUSB0", label="/dev/ttyUSB0"),
selector.SelectOptionDict(value="/dev/ttyUSB1", label="/dev/ttyUSB1"),
selector.SelectOptionDict(value="/dev/ttyACM0", label="/dev/ttyACM0"),
]
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_SERIAL_DEVICE, default="/dev/ttyUSB0"): selector.SelectSelector(
selector.SelectSelectorConfig(options=port_list),
),
vol.Required("baudrate", default=DEFAULT_BAUDRATE): vol.Coerce(int),
}
),
errors=errors,
)
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> SigMeshGatewayOptionsFlow:
"""获取选项流程."""
return SigMeshGatewayOptionsFlow(config_entry)
class SigMeshGatewayOptionsFlow(config_entries.OptionsFlow):
"""SigMesh Gateway 选项流程."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""初始化选项流程."""
self.config_entry = config_entry
async def async_step_init(self, user_input: dict[str, any] | None = None) -> FlowResult:
"""管理选项."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
"poll_interval",
default=self.config_entry.options.get("poll_interval", 30),
): vol.Coerce(int),
}
),
)