- 修复 config_flow.py 中的 KeyError 风险 - 添加 MQTT broker 地址和端口验证 - 完善翻译文件,添加所有配置字段的中文翻译 - 修复 sensor.py 中的弃用 API 使用 - 改进错误处理,转换失败时设置 available 为 False - 添加 device_info 中的 sw_version 字段 - 清理 manifest.json 中不必要的 paho-mqtt 依赖 - 修正 README.md 中的路径错误 - 更新版本号到 1.0.3
84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
"""Config flow for Energy Monitor integration."""
|
|
import logging
|
|
from typing import Any
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
|
|
from . import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
# 配置数据模式
|
|
DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Optional(
|
|
"topic_prefix",
|
|
default="homeassistant/sensor"
|
|
): str,
|
|
vol.Required(
|
|
"mqtt_broker",
|
|
default="192.168.0.101"
|
|
): str,
|
|
vol.Optional(
|
|
"mqtt_port",
|
|
default=1883
|
|
): int,
|
|
}
|
|
)
|
|
|
|
|
|
class JackeryHomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for JackeryHome."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
"""Handle the initial step."""
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
# 验证 MQTT broker 地址
|
|
mqtt_broker = user_input.get("mqtt_broker", "").strip()
|
|
if not mqtt_broker:
|
|
errors["base"] = "mqtt_broker_required"
|
|
|
|
# 验证端口范围
|
|
try:
|
|
mqtt_port = int(user_input.get("mqtt_port", 1883))
|
|
if mqtt_port < 1 or mqtt_port > 65535:
|
|
errors["base"] = "invalid_port"
|
|
except (ValueError, TypeError):
|
|
errors["base"] = "invalid_port"
|
|
|
|
if not errors:
|
|
# 检查是否已经配置
|
|
await self.async_set_unique_id(DOMAIN)
|
|
self._abort_if_unique_id_configured()
|
|
|
|
_LOGGER.info(f"Creating JackeryHome config entry with topic_prefix: {user_input.get('topic_prefix', 'homeassistant/sensor')}")
|
|
|
|
return self.async_create_entry(
|
|
title="JackeryHome",
|
|
data=user_input,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=DATA_SCHEMA,
|
|
errors=errors,
|
|
description_placeholders={
|
|
"topic_prefix": "MQTT topic prefix (e.g., homeassistant/sensor)",
|
|
},
|
|
)
|
|
|
|
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
|
|
"""Import a config entry from configuration.yaml."""
|
|
return await self.async_step_user(import_config)
|
|
|