impress_sig_mesh_hacs/custom_components/sigmesh_gateway/config_flow.py
impressionyang 19774a4a9f fix: 修复平台加载和 OptionsFlow 错误
主要修复:
1. 将平台文件从 platforms/ 移到集成根目录
   - sensor.py, binary_sensor.py, switch.py, light.py, device_tracker.py
2. 修复 OptionsFlow config_entry 属性错误
   - 移除手动设置的 config_entry
3. 添加集成图标 icons/icon.png
4. manifest.json 添加 integration_type: device

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 09:39:35 +08:00

96 lines
2.9 KiB
Python

"""SigMesh Gateway 配置流程."""
from __future__ import annotations
from typing import Any
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 选项流程."""
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),
}
),
)