- 将 dict[str, any] 改为 dict[str, Any] - 添加 typing.Any 导入 这会导致 500 Internal Server Error Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
100 lines
3.1 KiB
Python
100 lines
3.1 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 选项流程."""
|
|
|
|
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),
|
|
}
|
|
),
|
|
)
|